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 1.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package stgsdk
  2. import (
  3. "fmt"
  4. "io"
  5. "net/url"
  6. "strings"
  7. myhttp "gitlink.org.cn/cloudream/common/utils/http"
  8. "gitlink.org.cn/cloudream/common/utils/serder"
  9. )
  10. type ObjectDownloadReq struct {
  11. UserID int64 `json:"userID"`
  12. ObjectID int64 `json:"objectID"`
  13. }
  14. func (c *Client) ObjectDownload(req ObjectDownloadReq) (io.ReadCloser, error) {
  15. url, err := url.JoinPath(c.baseURL, "/object/download")
  16. if err != nil {
  17. return nil, err
  18. }
  19. resp, err := myhttp.GetJSON(url, myhttp.RequestParam{
  20. Query: req,
  21. })
  22. if err != nil {
  23. return nil, err
  24. }
  25. contType := resp.Header.Get("Content-Type")
  26. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  27. var codeResp response[any]
  28. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  29. return nil, fmt.Errorf("parsing response: %w", err)
  30. }
  31. return nil, codeResp.ToError()
  32. }
  33. if strings.Contains(contType, myhttp.ContentTypeOctetStream) {
  34. return resp.Body, nil
  35. }
  36. return nil, fmt.Errorf("unknow response content type: %s", contType)
  37. }