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

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.VarID `json:"input"`
  18. FilePath string `json:"filePath"`
  19. }
  20. func (o *FileWrite) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  21. input, err := exec.BindVar[*exec.StreamValue](e, ctx.Context, o.Input)
  22. if err != nil {
  23. return err
  24. }
  25. defer 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, 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, o.FilePath)
  44. }
  45. type FileRead struct {
  46. Output exec.VarID `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. e.PutVar(o.Output, &exec.StreamValue{
  56. Stream: io2.AfterReadClosed(file, func(closer io.ReadCloser) {
  57. fut.SetVoid()
  58. }),
  59. })
  60. fut.Wait(ctx.Context)
  61. return nil
  62. }
  63. func (o *FileRead) String() string {
  64. return fmt.Sprintf("FileRead %s -> %v", o.FilePath, o.Output)
  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().Init(node, 1)
  76. return node
  77. }
  78. func (t *FileReadNode) Output() dag.StreamOutputSlot {
  79. return dag.StreamOutputSlot{
  80. Node: t,
  81. Index: 0,
  82. }
  83. }
  84. func (t *FileReadNode) GenerateOp() (exec.Op, error) {
  85. return &FileRead{
  86. Output: t.OutputStreams().Get(0).VarID,
  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. node.InputStreams().Init(1)
  103. return node
  104. }
  105. func (t *FileWriteNode) Input() dag.StreamOutputSlot {
  106. return dag.StreamOutputSlot{
  107. Node: t,
  108. Index: 0,
  109. }
  110. }
  111. func (t *FileWriteNode) SetInput(str *dag.StreamVar) {
  112. str.To(t, 0)
  113. }
  114. func (t *FileWriteNode) GenerateOp() (exec.Op, error) {
  115. return &FileWrite{
  116. Input: t.InputStreams().Get(0).VarID,
  117. FilePath: t.FilePath,
  118. }, nil
  119. }

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