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

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

公共库