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.0 kB

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

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