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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package ops
  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/utils/io2"
  10. "gitlink.org.cn/cloudream/storage/common/pkgs/ioswitch"
  11. )
  12. type FileWrite struct {
  13. InputID ioswitch.StreamID `json:"inputID"`
  14. FilePath string `json:"filePath"`
  15. }
  16. func (o *FileWrite) Execute(sw *ioswitch.Switch, planID ioswitch.PlanID) error {
  17. str, err := sw.WaitStreams(planID, o.InputID)
  18. if err != nil {
  19. return err
  20. }
  21. defer str[0].Stream.Close()
  22. dir := path.Dir(o.FilePath)
  23. err = os.MkdirAll(dir, 0777)
  24. if err != nil {
  25. return fmt.Errorf("mkdir: %w", err)
  26. }
  27. file, err := os.Create(o.FilePath)
  28. if err != nil {
  29. return fmt.Errorf("opening file: %w", err)
  30. }
  31. defer file.Close()
  32. _, err = io.Copy(file, str[0].Stream)
  33. if err != nil {
  34. return fmt.Errorf("copying data to file: %w", err)
  35. }
  36. return nil
  37. }
  38. type FileRead struct {
  39. OutputID ioswitch.StreamID `json:"outputID"`
  40. FilePath string `json:"filePath"`
  41. }
  42. func (o *FileRead) Execute(sw *ioswitch.Switch, planID ioswitch.PlanID) error {
  43. file, err := os.Open(o.FilePath)
  44. if err != nil {
  45. return fmt.Errorf("opening file: %w", err)
  46. }
  47. fut := future.NewSetVoid()
  48. sw.StreamReady(planID, ioswitch.NewStream(o.OutputID, io2.AfterReadClosed(file, func(closer io.ReadCloser) {
  49. fut.SetVoid()
  50. })))
  51. fut.Wait(context.TODO())
  52. return nil
  53. }
  54. func init() {
  55. OpUnion.AddT((*FileRead)(nil))
  56. OpUnion.AddT((*FileWrite)(nil))
  57. }

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