You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.go 4.2 kB

7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package api
  2. import (
  3. "crypto/sha256"
  4. "encoding/hex"
  5. "fmt"
  6. "io"
  7. "mime/multipart"
  8. "net/http"
  9. "net/url"
  10. ul "net/url"
  11. "strings"
  12. "github.com/google/go-querystring/query"
  13. "gitlink.org.cn/cloudream/common/pkgs/iterator"
  14. "gitlink.org.cn/cloudream/common/sdks"
  15. "gitlink.org.cn/cloudream/common/utils/http2"
  16. "gitlink.org.cn/cloudream/common/utils/math2"
  17. "gitlink.org.cn/cloudream/common/utils/serder"
  18. "gitlink.org.cn/cloudream/jcs-pub/client/sdk/api"
  19. )
  20. func ParseJSONResponse[TBody any](resp *http.Response) (TBody, error) {
  21. var ret TBody
  22. contType := resp.Header.Get("Content-Type")
  23. if strings.Contains(contType, http2.ContentTypeJSON) {
  24. var err error
  25. if ret, err = serder.JSONToObjectStreamEx[TBody](resp.Body); err != nil {
  26. return ret, fmt.Errorf("parsing response: %w", err)
  27. }
  28. return ret, nil
  29. }
  30. cont, err := io.ReadAll(resp.Body)
  31. if err != nil {
  32. return ret, fmt.Errorf("unknow response content type: %s, status: %d", contType, resp.StatusCode)
  33. }
  34. strCont := string(cont)
  35. return ret, fmt.Errorf("unknow response content type: %s, status: %d, body(prefix): %s", contType, resp.StatusCode, strCont[:math2.Min(len(strCont), 200)])
  36. }
  37. func JSONAPI[Resp sdks.APIResponse, Req sdks.APIRequest](cfg *api.Config, cli *http.Client, req Req, resp Resp) (Resp, error) {
  38. param := req.MakeParam()
  39. v1EndPoint, err := url.JoinPath(cfg.EndPoint, "v1")
  40. if err != nil {
  41. return resp, err
  42. }
  43. httpReq, err := param.MakeRequest(v1EndPoint)
  44. if err != nil {
  45. return resp, err
  46. }
  47. httpResp, err := cli.Do(httpReq)
  48. if err != nil {
  49. return resp, err
  50. }
  51. err = resp.ParseResponse(httpResp)
  52. return resp, err
  53. }
  54. func JSONAPINoData[Req sdks.APIRequest](cfg *api.Config, cli *http.Client, req Req) error {
  55. param := req.MakeParam()
  56. v1EndPoint, err := url.JoinPath(cfg.EndPoint, "v1")
  57. if err != nil {
  58. return err
  59. }
  60. httpReq, err := param.MakeRequest(v1EndPoint)
  61. if err != nil {
  62. return err
  63. }
  64. resp, err := cli.Do(httpReq)
  65. if err != nil {
  66. return err
  67. }
  68. return sdks.ParseCodeDataJSONResponse(resp, any(nil))
  69. }
  70. func calcSha256(body sdks.RequestBody) string {
  71. hasher := sha256.New()
  72. switch body := body.(type) {
  73. case *sdks.StringBody:
  74. hasher.Write([]byte(body.Value))
  75. return hex.EncodeToString(hasher.Sum(nil))
  76. case *sdks.BytesBody:
  77. hasher.Write(body.Value)
  78. return hex.EncodeToString(hasher.Sum(nil))
  79. case *sdks.StreamBody:
  80. return ""
  81. default:
  82. hash := sha256.Sum256([]byte(""))
  83. return hex.EncodeToString(hash[:])
  84. }
  85. }
  86. func PostMultiPart(cfg *api.Config, cli *http.Client, url string, info any, files http2.MultiPartFileIterator) (*http.Response, error) {
  87. req, err := http.NewRequest(http.MethodPost, url, nil)
  88. if err != nil {
  89. return nil, err
  90. }
  91. pr, pw := io.Pipe()
  92. muWriter := multipart.NewWriter(pw)
  93. req.Header.Set("Content-Type", fmt.Sprintf("%s;boundary=%s", http2.ContentTypeMultiPart, muWriter.Boundary()))
  94. writeResult := make(chan error, 1)
  95. go func() {
  96. writeResult <- func() error {
  97. defer pw.Close()
  98. defer muWriter.Close()
  99. if info != nil {
  100. mp, err := query.Values(info)
  101. if err != nil {
  102. return fmt.Errorf("formValues object to map failed, err: %w", err)
  103. }
  104. for k, v := range mp {
  105. err := muWriter.WriteField(k, v[0])
  106. if err != nil {
  107. return fmt.Errorf("write form field failed, err: %w", err)
  108. }
  109. }
  110. }
  111. for {
  112. file, err := files.MoveNext()
  113. if err == iterator.ErrNoMoreItem {
  114. break
  115. }
  116. if err != nil {
  117. return fmt.Errorf("opening file: %w", err)
  118. }
  119. err = sendFileOnePart(muWriter, file.FieldName, file.FileName, file.File)
  120. file.File.Close()
  121. if err != nil {
  122. return err
  123. }
  124. }
  125. return nil
  126. }()
  127. }()
  128. req.Body = pr
  129. resp, err := cli.Do(req)
  130. if err != nil {
  131. return nil, err
  132. }
  133. writeErr := <-writeResult
  134. if writeErr != nil {
  135. return nil, writeErr
  136. }
  137. return resp, nil
  138. }
  139. func sendFileOnePart(muWriter *multipart.Writer, fieldName, fileName string, file io.ReadCloser) error {
  140. w, err := muWriter.CreateFormFile(fieldName, ul.PathEscape(fileName))
  141. if err != nil {
  142. return fmt.Errorf("create form file failed, err: %w", err)
  143. }
  144. _, err = io.Copy(w, file)
  145. return err
  146. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。