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.

object.go 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package storage
  2. import (
  3. "fmt"
  4. "io"
  5. "net/url"
  6. "strings"
  7. "gitlink.org.cn/cloudream/common/consts/errorcode"
  8. "gitlink.org.cn/cloudream/common/models"
  9. myhttp "gitlink.org.cn/cloudream/common/utils/http"
  10. "gitlink.org.cn/cloudream/common/utils/serder"
  11. )
  12. type ObjectDownloadReq struct {
  13. UserID int64 `json:"userID"`
  14. ObjectID int64 `json:"objectID"`
  15. }
  16. func (c *Client) ObjectDownload(req ObjectDownloadReq) (io.ReadCloser, error) {
  17. url, err := url.JoinPath(c.baseURL, "/object/download")
  18. if err != nil {
  19. return nil, err
  20. }
  21. resp, err := myhttp.GetJSON(url, myhttp.RequestParam{
  22. Query: req,
  23. })
  24. if err != nil {
  25. return nil, err
  26. }
  27. contType := resp.Header.Get("Content-Type")
  28. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  29. var codeResp response[any]
  30. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  31. return nil, fmt.Errorf("parsing response: %w", err)
  32. }
  33. return nil, codeResp.ToError()
  34. }
  35. if strings.Contains(contType, myhttp.ContentTypeOctetStream) {
  36. return resp.Body, nil
  37. }
  38. return nil, fmt.Errorf("unknow response content type: %s", contType)
  39. }
  40. type ObjectUploadReq struct {
  41. UserID int64 `json:"userID"`
  42. BucketID int64 `json:"bucketID"`
  43. FileSize int64 `json:"fileSize"`
  44. ObjectName string `json:"objectName"`
  45. Redundancy models.TypedRedundancyInfo `json:"redundancy"`
  46. File io.Reader `json:"-"`
  47. }
  48. type ObjectUploadResp struct {
  49. ObjectID int64 `json:"objectID,string"`
  50. }
  51. func (c *Client) ObjectUpload(req ObjectUploadReq) (*ObjectUploadResp, error) {
  52. url, err := url.JoinPath(c.baseURL, "/object/upload")
  53. if err != nil {
  54. return nil, err
  55. }
  56. infoJSON, err := serder.ObjectToJSON(req)
  57. if err != nil {
  58. return nil, fmt.Errorf("object info to json: %w", err)
  59. }
  60. resp, err := myhttp.PostMultiPart(url, myhttp.MultiPartRequestParam{
  61. Form: map[string]string{"info": string(infoJSON)},
  62. DataName: req.ObjectName,
  63. Data: req.File,
  64. })
  65. if err != nil {
  66. return nil, err
  67. }
  68. contType := resp.Header.Get("Content-Type")
  69. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  70. var codeResp response[ObjectUploadResp]
  71. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  72. return nil, fmt.Errorf("parsing response: %w", err)
  73. }
  74. if codeResp.Code == errorcode.OK {
  75. return &codeResp.Data, nil
  76. }
  77. return nil, codeResp.ToError()
  78. }
  79. return nil, fmt.Errorf("unknow response content type: %s", contType)
  80. }
  81. type ObjectDeleteReq struct {
  82. UserID int64 `json:"userID"`
  83. ObjectID int64 `json:"objectID"`
  84. }
  85. func (c *Client) ObjectDelete(req ObjectDeleteReq) error {
  86. url, err := url.JoinPath(c.baseURL, "/object/delete")
  87. if err != nil {
  88. return err
  89. }
  90. resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
  91. Body: req,
  92. })
  93. if err != nil {
  94. return err
  95. }
  96. contType := resp.Header.Get("Content-Type")
  97. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  98. var codeResp response[any]
  99. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  100. return fmt.Errorf("parsing response: %w", err)
  101. }
  102. if codeResp.Code == errorcode.OK {
  103. return nil
  104. }
  105. return codeResp.ToError()
  106. }
  107. return fmt.Errorf("unknow response content type: %s", contType)
  108. }

公共库