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

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

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