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.

clone.go 1.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package ops
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "gitlink.org.cn/cloudream/common/utils/io2"
  7. "gitlink.org.cn/cloudream/storage/common/pkgs/ioswitch"
  8. "golang.org/x/sync/semaphore"
  9. )
  10. type CloneStream struct {
  11. Input *ioswitch.StreamVar `json:"input"`
  12. Outputs []*ioswitch.StreamVar `json:"outputs"`
  13. }
  14. func (o *CloneStream) Execute(ctx context.Context, sw *ioswitch.Switch) error {
  15. err := sw.BindVars(ctx, o.Input)
  16. if err != nil {
  17. return err
  18. }
  19. defer o.Input.Stream.Close()
  20. cloned := io2.Clone(o.Input.Stream, len(o.Outputs))
  21. sem := semaphore.NewWeighted(int64(len(o.Outputs)))
  22. for i, s := range cloned {
  23. sem.Acquire(ctx, 1)
  24. o.Outputs[i].Stream = io2.AfterReadClosedOnce(s, func(closer io.ReadCloser) {
  25. sem.Release(1)
  26. })
  27. }
  28. ioswitch.PutArrayVars(sw, o.Outputs)
  29. return sem.Acquire(ctx, int64(len(o.Outputs)))
  30. }
  31. type CloneVar struct {
  32. Raw ioswitch.Var `json:"raw"`
  33. Cloneds []ioswitch.Var `json:"cloneds"`
  34. }
  35. func (o *CloneVar) Execute(ctx context.Context, sw *ioswitch.Switch) error {
  36. err := sw.BindVars(ctx, o.Raw)
  37. if err != nil {
  38. return err
  39. }
  40. for _, v := range o.Cloneds {
  41. if err := ioswitch.AssignVar(o.Raw, v); err != nil {
  42. return fmt.Errorf("clone var: %w", err)
  43. }
  44. }
  45. sw.PutVars(o.Cloneds...)
  46. return nil
  47. }
  48. func init() {
  49. OpUnion.AddT((*CloneStream)(nil))
  50. OpUnion.AddT((*CloneVar)(nil))
  51. }

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