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

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

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