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.

ipfs.go 1.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package imsdk
  2. import (
  3. "fmt"
  4. "io"
  5. "net/url"
  6. "strings"
  7. "gitlink.org.cn/cloudream/common/utils/http2"
  8. "gitlink.org.cn/cloudream/common/utils/serder"
  9. )
  10. const IPFSReadPath = "/ipfs/read"
  11. type IPFSRead struct {
  12. FileHash string `json:"fileHash"`
  13. Offset int64 `json:"offset"`
  14. Length int64 `json:"length"`
  15. }
  16. func (c *Client) IPFSRead(req IPFSRead) (io.ReadCloser, error) {
  17. url, err := url.JoinPath(c.baseURL, IPFSReadPath)
  18. if err != nil {
  19. return nil, err
  20. }
  21. resp, err := http2.GetForm(url, http2.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, http2.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, http2.ContentTypeOctetStream) {
  36. return resp.Body, nil
  37. }
  38. return nil, fmt.Errorf("unknow response content type: %s", contType)
  39. }