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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. "gitlink.org.cn/cloudream/storage/common/pkgs/ioswitch2"
  13. )
  14. func init() {
  15. exec.UseOp[*FileRead]()
  16. exec.UseOp[*FileWrite]()
  17. }
  18. type FileWrite struct {
  19. Input *exec.StreamVar `json:"input"`
  20. FilePath string `json:"filePath"`
  21. }
  22. func (o *FileWrite) Execute(ctx context.Context, e *exec.Executor) error {
  23. err := e.BindVars(ctx, o.Input)
  24. if err != nil {
  25. return err
  26. }
  27. defer o.Input.Stream.Close()
  28. dir := path.Dir(o.FilePath)
  29. err = os.MkdirAll(dir, 0777)
  30. if err != nil {
  31. return fmt.Errorf("mkdir: %w", err)
  32. }
  33. file, err := os.Create(o.FilePath)
  34. if err != nil {
  35. return fmt.Errorf("opening file: %w", err)
  36. }
  37. defer file.Close()
  38. _, err = io.Copy(file, o.Input.Stream)
  39. if err != nil {
  40. return fmt.Errorf("copying data to file: %w", err)
  41. }
  42. return nil
  43. }
  44. func (o *FileWrite) String() string {
  45. return fmt.Sprintf("FileWrite %v -> %s", o.Input.ID, o.FilePath)
  46. }
  47. type FileRead struct {
  48. Output *exec.StreamVar `json:"output"`
  49. FilePath string `json:"filePath"`
  50. }
  51. func (o *FileRead) Execute(ctx context.Context, e *exec.Executor) error {
  52. file, err := os.Open(o.FilePath)
  53. if err != nil {
  54. return fmt.Errorf("opening file: %w", err)
  55. }
  56. fut := future.NewSetVoid()
  57. o.Output.Stream = io2.AfterReadClosed(file, func(closer io.ReadCloser) {
  58. fut.SetVoid()
  59. })
  60. e.PutVars(o.Output)
  61. fut.Wait(ctx)
  62. return nil
  63. }
  64. func (o *FileRead) String() string {
  65. return fmt.Sprintf("FileRead %s -> %v", o.FilePath, o.Output.ID)
  66. }
  67. type FileReadType struct {
  68. FilePath string
  69. }
  70. func (t *FileReadType) InitNode(node *dag.Node) {
  71. dag.NodeNewOutputStream(node, &ioswitch2.VarProps{})
  72. }
  73. func (t *FileReadType) GenerateOp(op *dag.Node) (exec.Op, error) {
  74. return &FileRead{
  75. Output: op.OutputStreams[0].Var,
  76. FilePath: t.FilePath,
  77. }, nil
  78. }
  79. func (t *FileReadType) String(node *dag.Node) string {
  80. return fmt.Sprintf("FileRead[%s]%v%v", t.FilePath, formatStreamIO(node), formatValueIO(node))
  81. }
  82. type FileWriteType struct {
  83. FilePath string
  84. }
  85. func (t *FileWriteType) InitNode(node *dag.Node) {
  86. dag.NodeDeclareInputStream(node, 1)
  87. }
  88. func (t *FileWriteType) GenerateOp(op *dag.Node) (exec.Op, error) {
  89. return &FileWrite{
  90. Input: op.InputStreams[0].Var,
  91. FilePath: t.FilePath,
  92. }, nil
  93. }

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