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.

hub_io.go 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package api
  2. import (
  3. "fmt"
  4. "io"
  5. "net/url"
  6. "gitlink.org.cn/cloudream/common/consts/errorcode"
  7. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
  8. "gitlink.org.cn/cloudream/common/utils/http2"
  9. "gitlink.org.cn/cloudream/common/utils/io2"
  10. "gitlink.org.cn/cloudream/common/utils/serder"
  11. )
  12. // TODO2 重新梳理代码
  13. const GetStreamPath = "/hubIO/getStream"
  14. type GetStreamReq struct {
  15. PlanID exec.PlanID `json:"planID"`
  16. VarID exec.VarID `json:"varID"`
  17. SignalID exec.VarID `json:"signalID"`
  18. Signal exec.VarValue `json:"signal"`
  19. }
  20. func (c *Client) GetStream(req GetStreamReq) (io.ReadCloser, error) {
  21. targetUrl, err := url.JoinPath(c.cfg.URL, GetStreamPath)
  22. if err != nil {
  23. return nil, err
  24. }
  25. body, err := serder.ObjectToJSONEx(req)
  26. if err != nil {
  27. return nil, fmt.Errorf("request to json: %w", err)
  28. }
  29. resp, err := http2.GetJSON(targetUrl, http2.RequestParam{
  30. Body: body,
  31. })
  32. if err != nil {
  33. return nil, err
  34. }
  35. cr := http2.NewChunkedReader(resp.Body)
  36. _, str, err := cr.NextPart()
  37. if err != nil {
  38. return nil, fmt.Errorf("reading response: %w", err)
  39. }
  40. return io2.DelegateReadCloser(str, func() error {
  41. cr.Close()
  42. return nil
  43. }), nil
  44. }
  45. const SendStreamPath = "/hubIO/sendStream"
  46. type SendStreamReq struct {
  47. SendStreamInfo
  48. Stream io.ReadCloser
  49. }
  50. type SendStreamInfo struct {
  51. PlanID exec.PlanID `json:"planID"`
  52. VarID exec.VarID `json:"varID"`
  53. }
  54. func (c *Client) SendStream(req SendStreamReq) error {
  55. targetUrl, err := url.JoinPath(c.cfg.URL, SendStreamPath)
  56. if err != nil {
  57. return err
  58. }
  59. pr, pw := io.Pipe()
  60. errCh := make(chan error, 1)
  61. go func() {
  62. cw := http2.NewChunkedWriter(pw)
  63. infoJSON, err := serder.ObjectToJSONEx(req)
  64. if err != nil {
  65. cw.Abort(fmt.Sprintf("info to json: %v", err))
  66. errCh <- fmt.Errorf("info to json: %w", err)
  67. return
  68. }
  69. if err := cw.WriteDataPart("info", infoJSON); err != nil {
  70. cw.Close()
  71. errCh <- fmt.Errorf("write info: %w", err)
  72. return
  73. }
  74. _, err = cw.WriteStreamPart("stream", req.Stream)
  75. if err != nil {
  76. cw.Close()
  77. errCh <- fmt.Errorf("write stream: %w", err)
  78. return
  79. }
  80. err = cw.Finish()
  81. if err != nil {
  82. errCh <- fmt.Errorf("finish chunked writer: %w", err)
  83. return
  84. }
  85. }()
  86. resp, err := http2.PostChunked2(targetUrl, http2.Chunked2RequestParam{
  87. Body: pr,
  88. })
  89. if err != nil {
  90. return err
  91. }
  92. err = <-errCh
  93. if err != nil {
  94. return err
  95. }
  96. codeResp, err := ParseJSONResponse[response[any]](resp)
  97. if err != nil {
  98. return err
  99. }
  100. if codeResp.Code == errorcode.OK {
  101. return nil
  102. }
  103. return codeResp.ToError()
  104. }
  105. const ExecuteIOPlanPath = "/hubIO/executeIOPlan"
  106. type ExecuteIOPlanReq struct {
  107. Plan exec.Plan `json:"plan"`
  108. }
  109. func (c *Client) ExecuteIOPlan(req ExecuteIOPlanReq) error {
  110. targetUrl, err := url.JoinPath(c.cfg.URL, ExecuteIOPlanPath)
  111. if err != nil {
  112. return err
  113. }
  114. body, err := serder.ObjectToJSONEx(req)
  115. if err != nil {
  116. return fmt.Errorf("request to json: %w", err)
  117. }
  118. resp, err := http2.PostJSON(targetUrl, http2.RequestParam{
  119. Body: body,
  120. })
  121. if err != nil {
  122. return err
  123. }
  124. codeResp, err := ParseJSONResponse[response[any]](resp)
  125. if err != nil {
  126. return err
  127. }
  128. if codeResp.Code == errorcode.OK {
  129. return nil
  130. }
  131. return codeResp.ToError()
  132. }
  133. const SendVarPath = "/hubIO/sendVar"
  134. type SendVarReq struct {
  135. PlanID exec.PlanID `json:"planID"`
  136. VarID exec.VarID `json:"varID"`
  137. VarValue exec.VarValue `json:"varValue"`
  138. }
  139. func (c *Client) SendVar(req SendVarReq) error {
  140. targetUrl, err := url.JoinPath(c.cfg.URL, SendVarPath)
  141. if err != nil {
  142. return err
  143. }
  144. body, err := serder.ObjectToJSONEx(req)
  145. if err != nil {
  146. return fmt.Errorf("request to json: %w", err)
  147. }
  148. resp, err := http2.PostJSON(targetUrl, http2.RequestParam{
  149. Body: body,
  150. })
  151. if err != nil {
  152. return err
  153. }
  154. jsonResp, err := ParseJSONResponse[response[any]](resp)
  155. if err != nil {
  156. return err
  157. }
  158. if jsonResp.Code == errorcode.OK {
  159. return nil
  160. }
  161. return jsonResp.ToError()
  162. }
  163. const GetVarPath = "/hubIO/getVar"
  164. type GetVarReq struct {
  165. PlanID exec.PlanID `json:"planID"`
  166. VarID exec.VarID `json:"varID"`
  167. SignalID exec.VarID `json:"signalID"`
  168. Signal exec.VarValue `json:"signal"`
  169. }
  170. type GetVarResp struct {
  171. Value exec.VarValue `json:"value"`
  172. }
  173. func (c *Client) GetVar(req GetVarReq) (*GetVarResp, error) {
  174. targetUrl, err := url.JoinPath(c.cfg.URL, GetVarPath)
  175. if err != nil {
  176. return nil, err
  177. }
  178. body, err := serder.ObjectToJSONEx(req)
  179. if err != nil {
  180. return nil, fmt.Errorf("request to json: %w", err)
  181. }
  182. resp, err := http2.GetJSON(targetUrl, http2.RequestParam{
  183. Body: body,
  184. })
  185. if err != nil {
  186. return nil, err
  187. }
  188. jsonResp, err := ParseJSONResponse[response[GetVarResp]](resp)
  189. if err != nil {
  190. return nil, err
  191. }
  192. if jsonResp.Code == errorcode.OK {
  193. return &jsonResp.Data, nil
  194. }
  195. return nil, jsonResp.ToError()
  196. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。