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_iterator.go 2.1 kB

4 months ago
4 months ago
4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package putp
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "time"
  7. "github.com/inhies/go-bytesize"
  8. cliapi "gitlink.org.cn/cloudream/jcs-pub/client/sdk/api/v1"
  9. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/iterator"
  10. jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
  11. )
  12. type FileIterator struct {
  13. absRootPath string
  14. jpathRoot jcstypes.JPath
  15. init bool
  16. curEntries []dirEntry
  17. lastStartTime time.Time
  18. totalSize int64
  19. fileCount int
  20. }
  21. func (i *FileIterator) MoveNext() (*cliapi.UploadingObject, error) {
  22. if !i.init {
  23. es, err := os.ReadDir(i.absRootPath)
  24. if err != nil {
  25. return nil, err
  26. }
  27. for _, e := range es {
  28. i.curEntries = append(i.curEntries, dirEntry{
  29. dir: jcstypes.JPath{},
  30. entry: e,
  31. })
  32. }
  33. i.init = true
  34. }
  35. for {
  36. if len(i.curEntries) == 0 {
  37. return nil, iterator.ErrNoMoreItem
  38. }
  39. entry := i.curEntries[0]
  40. i.curEntries = i.curEntries[1:]
  41. if entry.entry.IsDir() {
  42. es, err := os.ReadDir(filepath.Join(i.absRootPath, entry.dir.OSString(), entry.entry.Name()))
  43. if err != nil {
  44. return nil, err
  45. }
  46. // 多个entry对象共享同一个JPath对象,但因为不会修改JPath,所以没问题
  47. dir := entry.dir.Clone()
  48. dir.Push(entry.entry.Name())
  49. for _, e := range es {
  50. i.curEntries = append(i.curEntries, dirEntry{
  51. dir: dir,
  52. entry: e,
  53. })
  54. }
  55. continue
  56. }
  57. file, err := os.Open(filepath.Join(i.absRootPath, entry.dir.OSString(), entry.entry.Name()))
  58. if err != nil {
  59. return nil, err
  60. }
  61. info, err := file.Stat()
  62. if err != nil {
  63. return nil, err
  64. }
  65. i.totalSize += info.Size()
  66. i.fileCount++
  67. jpath := i.jpathRoot.ConcatNew(entry.dir)
  68. path := jpath.PushNew(entry.entry.Name()).String()
  69. now := time.Now()
  70. if !i.lastStartTime.IsZero() {
  71. dt := now.Sub(i.lastStartTime)
  72. fmt.Printf("\t%v\n", dt)
  73. }
  74. i.lastStartTime = now
  75. fmt.Printf("%v\t%v", path, bytesize.ByteSize(info.Size()))
  76. return &cliapi.UploadingObject{
  77. Path: path,
  78. File: file,
  79. }, nil
  80. }
  81. }
  82. func (i *FileIterator) Close() {
  83. }
  84. type dirEntry struct {
  85. dir jcstypes.JPath
  86. entry os.DirEntry
  87. }

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