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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. }
  108. type ExecuteIOPlanResp struct {
  109. Result exec.ExecutorResult `json:"result"`
  110. }
  111. func (c *Client) ExecuteIOPlan(req ExecuteIOPlanReq) (*ExecuteIOPlanResp, error) {
  112. targetUrl, err := url.JoinPath(c.cfg.URL, ExecuteIOPlanPath)
  113. if err != nil {
  114. return nil, err
  115. }
  116. body, err := serder.ObjectToJSONEx(req)
  117. if err != nil {
  118. return nil, fmt.Errorf("request to json: %w", err)
  119. }
  120. resp, err := http2.PostJSON(targetUrl, http2.RequestParam{
  121. Body: body,
  122. })
  123. if err != nil {
  124. return nil, err
  125. }
  126. codeResp, err := ParseJSONResponse[response[ExecuteIOPlanResp]](resp)
  127. if err != nil {
  128. return nil, err
  129. }
  130. if codeResp.Code == errorcode.OK {
  131. return &codeResp.Data, nil
  132. }
  133. return nil, codeResp.ToError()
  134. }
  135. const SendVarPath = "/hubIO/sendVar"
  136. type SendVarReq struct {
  137. PlanID exec.PlanID `json:"planID"`
  138. VarID exec.VarID `json:"varID"`
  139. VarValue exec.VarValue `json:"varValue"`
  140. }
  141. func (c *Client) SendVar(req SendVarReq) error {
  142. targetUrl, err := url.JoinPath(c.cfg.URL, SendVarPath)
  143. if err != nil {
  144. return err
  145. }
  146. body, err := serder.ObjectToJSONEx(req)
  147. if err != nil {
  148. return fmt.Errorf("request to json: %w", err)
  149. }
  150. resp, err := http2.PostJSON(targetUrl, http2.RequestParam{
  151. Body: body,
  152. })
  153. if err != nil {
  154. return err
  155. }
  156. jsonResp, err := ParseJSONResponse[response[any]](resp)
  157. if err != nil {
  158. return err
  159. }
  160. if jsonResp.Code == errorcode.OK {
  161. return nil
  162. }
  163. return jsonResp.ToError()
  164. }
  165. const GetVarPath = "/hubIO/getVar"
  166. type GetVarReq struct {
  167. PlanID exec.PlanID `json:"planID"`
  168. VarID exec.VarID `json:"varID"`
  169. SignalID exec.VarID `json:"signalID"`
  170. Signal exec.VarValue `json:"signal"`
  171. }
  172. type GetVarResp struct {
  173. Value exec.VarValue `json:"value"`
  174. }
  175. func (c *Client) GetVar(req GetVarReq) (*GetVarResp, error) {
  176. targetUrl, err := url.JoinPath(c.cfg.URL, GetVarPath)
  177. if err != nil {
  178. return nil, err
  179. }
  180. body, err := serder.ObjectToJSONEx(req)
  181. if err != nil {
  182. return nil, fmt.Errorf("request to json: %w", err)
  183. }
  184. resp, err := http2.GetJSON(targetUrl, http2.RequestParam{
  185. Body: body,
  186. })
  187. if err != nil {
  188. return nil, err
  189. }
  190. jsonResp, err := ParseJSONResponse[response[GetVarResp]](resp)
  191. if err != nil {
  192. return nil, err
  193. }
  194. if jsonResp.Code == errorcode.OK {
  195. return &jsonResp.Data, nil
  196. }
  197. return nil, jsonResp.ToError()
  198. }

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