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.

range.go 1.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package ops
  2. import (
  3. "context"
  4. "io"
  5. "gitlink.org.cn/cloudream/common/pkgs/future"
  6. "gitlink.org.cn/cloudream/common/utils/io2"
  7. "gitlink.org.cn/cloudream/common/utils/math2"
  8. "gitlink.org.cn/cloudream/storage/common/pkgs/ioswitch"
  9. )
  10. type Range struct {
  11. Input *ioswitch.StreamVar `json:"input"`
  12. Output *ioswitch.StreamVar `json:"output"`
  13. Offset int64 `json:"offset"`
  14. Length *int64 `json:"length"`
  15. }
  16. func (o *Range) Execute(ctx context.Context, sw *ioswitch.Switch) error {
  17. err := sw.BindVars(ctx, o.Input)
  18. if err != nil {
  19. return err
  20. }
  21. defer o.Input.Stream.Close()
  22. buf := make([]byte, 1024*16)
  23. // 跳过前Offset个字节
  24. for o.Offset > 0 {
  25. rdCnt := math2.Min(o.Offset, int64(len(buf)))
  26. rd, err := o.Input.Stream.Read(buf[:rdCnt])
  27. if err == io.EOF {
  28. // 输入流不够长度也不报错,只是产生一个空的流
  29. break
  30. }
  31. if err != nil {
  32. return err
  33. }
  34. o.Offset -= int64(rd)
  35. }
  36. fut := future.NewSetVoid()
  37. if o.Length == nil {
  38. o.Output.Stream = io2.AfterEOF(o.Input.Stream, func(closer io.ReadCloser, err error) {
  39. fut.SetVoid()
  40. })
  41. sw.PutVars(o.Output)
  42. return fut.Wait(ctx)
  43. }
  44. o.Output.Stream = io2.AfterEOF(io2.Length(o.Input.Stream, *o.Length), func(closer io.ReadCloser, err error) {
  45. fut.SetVoid()
  46. })
  47. sw.PutVars(o.Output)
  48. err = fut.Wait(ctx)
  49. if err != nil {
  50. return err
  51. }
  52. io2.DropWithBuf(o.Input.Stream, buf)
  53. return nil
  54. }
  55. func init() {
  56. OpUnion.AddT((*Range)(nil))
  57. }

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