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.

file.go 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package ops2
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path"
  8. "gitlink.org.cn/cloudream/common/pkgs/future"
  9. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/dag"
  10. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
  11. "gitlink.org.cn/cloudream/common/utils/io2"
  12. )
  13. func init() {
  14. exec.UseOp[*FileRead]()
  15. exec.UseOp[*FileWrite]()
  16. }
  17. type FileWrite struct {
  18. Input *exec.StreamVar `json:"input"`
  19. FilePath string `json:"filePath"`
  20. }
  21. func (o *FileWrite) Execute(ctx context.Context, e *exec.Executor) error {
  22. err := e.BindVars(ctx, o.Input)
  23. if err != nil {
  24. return err
  25. }
  26. defer o.Input.Stream.Close()
  27. dir := path.Dir(o.FilePath)
  28. err = os.MkdirAll(dir, 0777)
  29. if err != nil {
  30. return fmt.Errorf("mkdir: %w", err)
  31. }
  32. file, err := os.Create(o.FilePath)
  33. if err != nil {
  34. return fmt.Errorf("opening file: %w", err)
  35. }
  36. defer file.Close()
  37. _, err = io.Copy(file, o.Input.Stream)
  38. if err != nil {
  39. return fmt.Errorf("copying data to file: %w", err)
  40. }
  41. return nil
  42. }
  43. func (o *FileWrite) String() string {
  44. return fmt.Sprintf("FileWrite %v -> %s", o.Input.ID, o.FilePath)
  45. }
  46. type FileRead struct {
  47. Output *exec.StreamVar `json:"output"`
  48. FilePath string `json:"filePath"`
  49. }
  50. func (o *FileRead) Execute(ctx context.Context, e *exec.Executor) error {
  51. file, err := os.Open(o.FilePath)
  52. if err != nil {
  53. return fmt.Errorf("opening file: %w", err)
  54. }
  55. fut := future.NewSetVoid()
  56. o.Output.Stream = io2.AfterReadClosed(file, func(closer io.ReadCloser) {
  57. fut.SetVoid()
  58. })
  59. e.PutVars(o.Output)
  60. fut.Wait(ctx)
  61. return nil
  62. }
  63. func (o *FileRead) String() string {
  64. return fmt.Sprintf("FileRead %s -> %v", o.FilePath, o.Output.ID)
  65. }
  66. type FileReadNode struct {
  67. dag.NodeBase
  68. FilePath string
  69. }
  70. func (b *GraphNodeBuilder) NewFileRead(filePath string) *FileReadNode {
  71. node := &FileReadNode{
  72. FilePath: filePath,
  73. }
  74. b.AddNode(node)
  75. node.OutputStreams().SetupNew(node, b.NewStreamVar())
  76. return node
  77. }
  78. func (t *FileReadNode) Output() dag.StreamSlot {
  79. return dag.StreamSlot{
  80. Var: t.OutputStreams().Get(0),
  81. Index: 0,
  82. }
  83. }
  84. func (t *FileReadNode) GenerateOp() (exec.Op, error) {
  85. return &FileRead{
  86. Output: t.OutputStreams().Get(0).Var,
  87. FilePath: t.FilePath,
  88. }, nil
  89. }
  90. // func (t *FileReadType) String() string {
  91. // return fmt.Sprintf("FileRead[%s]%v%v", t.FilePath, formatStreamIO(node), formatValueIO(node))
  92. // }
  93. type FileWriteNode struct {
  94. dag.NodeBase
  95. FilePath string
  96. }
  97. func (b *GraphNodeBuilder) NewFileWrite(filePath string) *FileWriteNode {
  98. node := &FileWriteNode{
  99. FilePath: filePath,
  100. }
  101. b.AddNode(node)
  102. return node
  103. }
  104. func (t *FileWriteNode) Input() dag.StreamSlot {
  105. return dag.StreamSlot{
  106. Var: t.InputStreams().Get(0),
  107. Index: 0,
  108. }
  109. }
  110. func (t *FileWriteNode) SetInput(str *dag.StreamVar) {
  111. t.InputStreams().EnsureSize(1)
  112. str.Connect(t, 0)
  113. }
  114. func (t *FileWriteNode) GenerateOp() (exec.Op, error) {
  115. return &FileWrite{
  116. Input: t.InputStreams().Get(0).Var,
  117. FilePath: t.FilePath,
  118. }, nil
  119. }

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