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.

dir_reader.go 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package local
  2. import (
  3. "io"
  4. "os"
  5. "path/filepath"
  6. clitypes "gitlink.org.cn/cloudream/jcs-pub/client/types"
  7. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/storage/types"
  8. )
  9. type DirReader struct {
  10. // 完整的根路径(包括ReadDir的path参数),比如包括了盘符
  11. absRootPath string
  12. // ReadDir函数传递进来的path参数
  13. rootJPath clitypes.JPath
  14. init bool
  15. curEntries []dirEntry
  16. }
  17. func (r *DirReader) Next() (types.DirEntry, error) {
  18. if !r.init {
  19. info, err := os.Stat(r.absRootPath)
  20. if err != nil {
  21. return types.DirEntry{}, err
  22. }
  23. if !info.IsDir() {
  24. r.init = true
  25. return types.DirEntry{
  26. Path: r.rootJPath,
  27. Size: info.Size(),
  28. ModTime: info.ModTime(),
  29. IsDir: false,
  30. }, nil
  31. }
  32. es, err := os.ReadDir(r.absRootPath)
  33. if err != nil {
  34. return types.DirEntry{}, err
  35. }
  36. for _, e := range es {
  37. r.curEntries = append(r.curEntries, dirEntry{
  38. dir: clitypes.JPath{},
  39. entry: e,
  40. })
  41. }
  42. r.init = true
  43. }
  44. if len(r.curEntries) == 0 {
  45. return types.DirEntry{}, io.EOF
  46. }
  47. entry := r.curEntries[0]
  48. r.curEntries = r.curEntries[1:]
  49. if entry.entry.IsDir() {
  50. es, err := os.ReadDir(filepath.Join(r.absRootPath, entry.dir.JoinOSPath(), entry.entry.Name()))
  51. if err != nil {
  52. return types.DirEntry{}, nil
  53. }
  54. // 多个entry对象共享同一个JPath对象,但因为不会修改JPath,所以没问题
  55. dir := entry.dir.Clone()
  56. dir.Push(entry.entry.Name())
  57. for _, e := range es {
  58. r.curEntries = append(r.curEntries, dirEntry{
  59. dir: dir,
  60. entry: e,
  61. })
  62. }
  63. }
  64. info, err := entry.entry.Info()
  65. if err != nil {
  66. return types.DirEntry{}, err
  67. }
  68. p := r.rootJPath.ConcatNew(entry.dir)
  69. p.Push(entry.entry.Name())
  70. if entry.entry.IsDir() {
  71. return types.DirEntry{
  72. Path: p,
  73. Size: 0,
  74. ModTime: info.ModTime(),
  75. IsDir: true,
  76. }, nil
  77. }
  78. return types.DirEntry{
  79. Path: p,
  80. Size: info.Size(),
  81. ModTime: info.ModTime(),
  82. IsDir: false,
  83. }, nil
  84. }
  85. func (r *DirReader) Close() {
  86. }
  87. type dirEntry struct {
  88. dir clitypes.JPath
  89. entry os.DirEntry
  90. }
  91. type fileInfoDirEntry struct {
  92. info os.FileInfo
  93. }
  94. func (d fileInfoDirEntry) Name() string { return d.info.Name() }
  95. func (d fileInfoDirEntry) IsDir() bool { return d.info.IsDir() }
  96. func (d fileInfoDirEntry) Type() os.FileMode { return d.info.Mode().Type() }
  97. func (d fileInfoDirEntry) Info() (os.FileInfo, error) { return d.info, nil }

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