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

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

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