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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/ioswitch"
  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. type FileRead struct {
  45. Output *exec.StreamVar `json:"output"`
  46. FilePath string `json:"filePath"`
  47. }
  48. func (o *FileRead) Execute(ctx context.Context, e *exec.Executor) error {
  49. file, err := os.Open(o.FilePath)
  50. if err != nil {
  51. return fmt.Errorf("opening file: %w", err)
  52. }
  53. fut := future.NewSetVoid()
  54. o.Output.Stream = io2.AfterReadClosed(file, func(closer io.ReadCloser) {
  55. fut.SetVoid()
  56. })
  57. e.PutVars(o.Output)
  58. fut.Wait(ctx)
  59. return nil
  60. }
  61. type FileReadType struct {
  62. FilePath string
  63. }
  64. func (t *FileReadType) InitNode(node *dag.Node) {
  65. dag.NodeNewOutputStream(node, &ioswitch.VarProps{})
  66. }
  67. func (t *FileReadType) GenerateOp(op *dag.Node) (exec.Op, error) {
  68. return &FileRead{
  69. Output: op.OutputStreams[0].Var,
  70. FilePath: t.FilePath,
  71. }, nil
  72. }
  73. func (t *FileReadType) String(node *dag.Node) string {
  74. return fmt.Sprintf("FileRead[%s]%v%v", t.FilePath, formatStreamIO(node), formatValueIO(node))
  75. }
  76. type FileWriteType struct {
  77. FilePath string
  78. }
  79. func (t *FileWriteType) InitNode(node *dag.Node) {
  80. dag.NodeDeclareInputStream(node, 1)
  81. }
  82. func (t *FileWriteType) GenerateOp(op *dag.Node) (exec.Op, error) {
  83. return &FileWrite{
  84. Input: op.InputStreams[0].Var,
  85. FilePath: t.FilePath,
  86. }, nil
  87. }

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