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.

public_store.go 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package ops2
  2. import (
  3. "fmt"
  4. "io"
  5. "gitlink.org.cn/cloudream/common/pkgs/future"
  6. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/dag"
  7. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
  8. "gitlink.org.cn/cloudream/common/pkgs/logger"
  9. "gitlink.org.cn/cloudream/common/utils/io2"
  10. clitypes "gitlink.org.cn/cloudream/jcs-pub/client/types"
  11. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ioswitch2"
  12. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/storage/pool"
  13. )
  14. func init() {
  15. exec.UseOp[*PublicWrite]()
  16. exec.UseOp[*PublicRead]()
  17. }
  18. type PublicRead struct {
  19. Output exec.VarID
  20. UserSpace clitypes.UserSpaceDetail
  21. ObjectPath string
  22. }
  23. func (o *PublicRead) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  24. logger.
  25. WithField("Output", o.Output).
  26. WithField("UserSpace", o.UserSpace).
  27. WithField("ObjectPath", o.ObjectPath).
  28. Debug("public read")
  29. defer logger.Debug("public read end")
  30. stgPool, err := exec.GetValueByType[*pool.Pool](ctx)
  31. if err != nil {
  32. return fmt.Errorf("getting storage pool: %w", err)
  33. }
  34. store, err := stgPool.GetPublicStore(&o.UserSpace)
  35. if err != nil {
  36. return fmt.Errorf("getting public store of storage %v: %w", o.UserSpace, err)
  37. }
  38. stream, err := store.Read(o.ObjectPath)
  39. if err != nil {
  40. return fmt.Errorf("reading object %v: %w", o.ObjectPath, err)
  41. }
  42. fut := future.NewSetVoid()
  43. output := &exec.StreamValue{
  44. Stream: io2.AfterReadClosed(stream, func(closer io.ReadCloser) {
  45. fut.SetVoid()
  46. }),
  47. }
  48. e.PutVar(o.Output, output)
  49. return fut.Wait(ctx.Context)
  50. }
  51. func (o *PublicRead) String() string {
  52. return fmt.Sprintf("PublicRead %v:%v -> %v", o.UserSpace, o.ObjectPath, o.Output)
  53. }
  54. type PublicWrite struct {
  55. Input exec.VarID
  56. UserSpace clitypes.UserSpaceDetail
  57. ObjectPath string
  58. }
  59. func (o *PublicWrite) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  60. logger.
  61. WithField("Input", o.Input).
  62. Debugf("write file to public store")
  63. defer logger.Debugf("write file to public store finished")
  64. stgPool, err := exec.GetValueByType[*pool.Pool](ctx)
  65. if err != nil {
  66. return fmt.Errorf("getting storage pool: %w", err)
  67. }
  68. store, err := stgPool.GetPublicStore(&o.UserSpace)
  69. if err != nil {
  70. return fmt.Errorf("getting public store of storage %v: %w", o.UserSpace, err)
  71. }
  72. input, err := exec.BindVar[*exec.StreamValue](e, ctx.Context, o.Input)
  73. if err != nil {
  74. return err
  75. }
  76. defer input.Stream.Close()
  77. return store.Write(o.ObjectPath, input.Stream)
  78. }
  79. func (o *PublicWrite) String() string {
  80. return fmt.Sprintf("PublicWrite %v -> %v:%v", o.Input, o.UserSpace, o.ObjectPath)
  81. }
  82. type PublicReadNode struct {
  83. dag.NodeBase
  84. From ioswitch2.From
  85. UserSpace clitypes.UserSpaceDetail
  86. ObjectPath string
  87. }
  88. func (b *GraphNodeBuilder) NewPublicRead(from ioswitch2.From, userSpace clitypes.UserSpaceDetail, objPath string) *PublicReadNode {
  89. node := &PublicReadNode{
  90. From: from,
  91. UserSpace: userSpace,
  92. ObjectPath: objPath,
  93. }
  94. b.AddNode(node)
  95. node.OutputStreams().Init(node, 1)
  96. return node
  97. }
  98. func (t *PublicReadNode) GetFrom() ioswitch2.From {
  99. return t.From
  100. }
  101. func (t *PublicReadNode) Output() dag.StreamOutputSlot {
  102. return dag.StreamOutputSlot{
  103. Node: t,
  104. Index: 0,
  105. }
  106. }
  107. func (t *PublicReadNode) GenerateOp() (exec.Op, error) {
  108. return &PublicRead{
  109. Output: t.Output().Var().VarID,
  110. UserSpace: t.UserSpace,
  111. ObjectPath: t.ObjectPath,
  112. }, nil
  113. }
  114. type PublicWriteNode struct {
  115. dag.NodeBase
  116. To ioswitch2.To
  117. UserSpace clitypes.UserSpaceDetail
  118. ObjectPath string
  119. }
  120. func (b *GraphNodeBuilder) NewPublicWrite(to ioswitch2.To, userSpace clitypes.UserSpaceDetail, objPath string) *PublicWriteNode {
  121. node := &PublicWriteNode{
  122. To: to,
  123. UserSpace: userSpace,
  124. ObjectPath: objPath,
  125. }
  126. b.AddNode(node)
  127. node.InputStreams().Init(1)
  128. return node
  129. }
  130. func (t *PublicWriteNode) GetTo() ioswitch2.To {
  131. return t.To
  132. }
  133. func (t *PublicWriteNode) SetInput(input *dag.StreamVar) {
  134. input.To(t, 0)
  135. }
  136. func (t *PublicWriteNode) Input() dag.StreamInputSlot {
  137. return dag.StreamInputSlot{
  138. Node: t,
  139. Index: 0,
  140. }
  141. }
  142. func (t *PublicWriteNode) GenerateOp() (exec.Op, error) {
  143. return &PublicWrite{
  144. Input: t.InputStreams().Get(0).VarID,
  145. UserSpace: t.UserSpace,
  146. ObjectPath: t.ObjectPath,
  147. }, nil
  148. }

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