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.

uploader.go 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package uploadersdk
  2. import (
  3. "fmt"
  4. "gitlink.org.cn/cloudream/common/pkgs/types"
  5. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  6. "gitlink.org.cn/cloudream/common/sdks/storage/cdsapi"
  7. "gitlink.org.cn/cloudream/common/utils/http2"
  8. "gitlink.org.cn/cloudream/common/utils/serder"
  9. "net/url"
  10. "strings"
  11. )
  12. //type DataScheduleReq struct {
  13. // PackageID cdssdk.PackageID `json:"packageID"`
  14. // DataType string `json:"dataType"`
  15. // ScheduleTarget ScheduleTarget `json:"scheduleTarget"`
  16. //}
  17. //type DataScheduleResp struct {
  18. // Results []sch.DataScheduleResult `json:"data"`
  19. //}
  20. //type TmpDataScheduleResult struct {
  21. // Cluster sch.DataDetail `json:"cluster"`
  22. // PackageID cdssdk.PackageID `json:"packageID"`
  23. // Status bool `json:"status"`
  24. // Msg string `json:"msg"`
  25. //}
  26. //func (c *Client) DataSchedule(req DataScheduleReq) ([]sch.DataScheduleResult, error) {
  27. // targetUrl, err := url.JoinPath(c.baseURL, "/dataSchedule")
  28. // if err != nil {
  29. // return nil, err
  30. // }
  31. //
  32. // resp, err := http2.PostJSON(targetUrl, http2.RequestParam{
  33. // Body: req,
  34. // })
  35. // if err != nil {
  36. // return nil, err
  37. // }
  38. // println(resp.Body)
  39. //
  40. // contType := resp.Header.Get("Content-Type")
  41. // if strings.Contains(contType, http2.ContentTypeJSON) {
  42. // var codeResp response[[]TmpDataScheduleResult]
  43. // if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  44. // return nil, fmt.Errorf("parsing response: %w", err)
  45. // }
  46. //
  47. // if codeResp.Code == ResponseCodeOK {
  48. // var results []sch.DataScheduleResult
  49. // for _, tmpResult := range codeResp.Data {
  50. // result := sch.DataScheduleResult{
  51. // PackageID: tmpResult.PackageID,
  52. // Status: tmpResult.Status,
  53. // Msg: tmpResult.Msg,
  54. // Clusters: []sch.DataDetail{
  55. // tmpResult.Cluster,
  56. // },
  57. // }
  58. // results = append(results, result)
  59. // }
  60. // return results, nil
  61. // }
  62. //
  63. // return nil, codeResp.ToError()
  64. // }
  65. //
  66. // return nil, fmt.Errorf("unknow response content type: %s", contType)
  67. //}
  68. type UploadReq struct {
  69. DataType string `json:"dataType"`
  70. Source UploadSource `json:"source"`
  71. Target UploadTarget `json:"target"`
  72. //StorageIDs []cdssdk.StorageID `json:"storageIDs"`
  73. }
  74. type UploadSource interface {
  75. Noop()
  76. }
  77. var UploadSourceTypeUnion = types.NewTypeUnion[UploadSource](
  78. (*PackageSource)(nil),
  79. (*UrlSource)(nil),
  80. )
  81. var _ = serder.UseTypeUnionInternallyTagged(&UploadSourceTypeUnion, "type")
  82. type PackageSource struct {
  83. serder.Metadata `union:"jcs"`
  84. UploadSourceBase
  85. Type string `json:"type"`
  86. PackageID cdssdk.PackageID `json:"packageID"`
  87. }
  88. type UrlSource struct {
  89. serder.Metadata `union:"url"`
  90. UploadSourceBase
  91. Type string `json:"type"`
  92. Url string `json:"url"`
  93. }
  94. type UploadSourceBase struct{}
  95. func (d *UploadSourceBase) Noop() {}
  96. type UploadTarget interface {
  97. Noop()
  98. }
  99. var UploadTargetTypeUnion = types.NewTypeUnion[UploadTarget](
  100. (*UrlTarget)(nil),
  101. (*ApiTarget)(nil),
  102. )
  103. var _ = serder.UseTypeUnionInternallyTagged(&UploadTargetTypeUnion, "type")
  104. type UrlTarget struct {
  105. serder.Metadata `union:"url"`
  106. UploadTargetBase
  107. Type string `json:"type"`
  108. ClusterID ClusterID `json:"clusterId"`
  109. JCSUploadInfo cdsapi.ObjectUploadInfo `form:"JCSUploadInfo"`
  110. }
  111. type ApiTarget struct {
  112. serder.Metadata `union:"api"`
  113. UploadTargetBase
  114. Type string `json:"type"`
  115. Clusters []ClusterID `json:"clusters"`
  116. }
  117. type UploadTargetBase struct{}
  118. func (d *UploadTargetBase) Noop() {}
  119. type UploadResp struct {
  120. PackageID cdssdk.PackageID `json:"packageID"`
  121. ObjectIDs []cdssdk.ObjectID `json:"objectIDs"`
  122. ClusterID ClusterID `json:"clusterID"`
  123. JsonData string `json:"jsonData"`
  124. Status bool `json:"status"`
  125. Message string `json:"message"`
  126. }
  127. func (c *Client) Upload(req UploadReq) (*UploadResp, error) {
  128. targetUrl, err := url.JoinPath(c.baseURL, "/dataUpload")
  129. if err != nil {
  130. return nil, err
  131. }
  132. resp, err := http2.PostJSON(targetUrl, http2.RequestParam{
  133. Body: req,
  134. })
  135. if err != nil {
  136. return nil, err
  137. }
  138. contType := resp.Header.Get("Content-Type")
  139. if strings.Contains(contType, http2.ContentTypeJSON) {
  140. var codeResp response[UploadResp]
  141. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  142. return nil, fmt.Errorf("parsing response: %w", err)
  143. }
  144. if codeResp.Code == ResponseCodeOK {
  145. return &codeResp.Data, nil
  146. }
  147. return nil, codeResp.ToError()
  148. }
  149. return nil, fmt.Errorf("unknow response content type: %s", contType)
  150. }