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.

public_store.go 2.5 kB

10 months ago
10 months ago
10 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package local
  2. import (
  3. "io"
  4. "io/fs"
  5. "os"
  6. "path/filepath"
  7. "gitlink.org.cn/cloudream/common/pkgs/logger"
  8. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  9. "gitlink.org.cn/cloudream/storage/common/pkgs/storage/types"
  10. )
  11. type PublicStoreDesc struct {
  12. types.EmptyPublicStoreDesc
  13. builder *builder
  14. }
  15. func (d *PublicStoreDesc) Enabled() bool {
  16. return d.builder.detail.Storage.PublicStore != nil
  17. }
  18. type PublicStore struct {
  19. agt *agent
  20. cfg cdssdk.LocalPublicStorage
  21. }
  22. func NewPublicStore(agt *agent, cfg cdssdk.LocalPublicStorage) (*PublicStore, error) {
  23. return &PublicStore{
  24. agt: agt,
  25. cfg: cfg,
  26. }, nil
  27. }
  28. func (s *PublicStore) Start(ch *types.StorageEventChan) {
  29. s.getLogger().Infof("component start, LoadBase: %v", s.cfg.LoadBase)
  30. }
  31. func (s *PublicStore) Stop() {
  32. s.getLogger().Infof("component stop")
  33. }
  34. func (s *PublicStore) Write(objPath string, stream io.Reader) error {
  35. fullPath := filepath.Join(s.cfg.LoadBase, objPath)
  36. err := os.MkdirAll(filepath.Dir(fullPath), 0755)
  37. if err != nil {
  38. return err
  39. }
  40. f, err := os.Create(fullPath)
  41. if err != nil {
  42. return err
  43. }
  44. defer f.Close()
  45. _, err = io.Copy(f, stream)
  46. if err != nil {
  47. return err
  48. }
  49. return nil
  50. }
  51. func (s *PublicStore) Read(objPath string) (io.ReadCloser, error) {
  52. fullPath := filepath.Join(s.cfg.LoadBase, objPath)
  53. f, err := os.Open(fullPath)
  54. if err != nil {
  55. return nil, err
  56. }
  57. return f, nil
  58. }
  59. func (s *PublicStore) List(path string, recursive bool) ([]string, error) {
  60. fullPath := filepath.Join(s.cfg.LoadBase, path)
  61. var pathes []string
  62. if recursive {
  63. err := filepath.WalkDir(fullPath, func(path string, d fs.DirEntry, err error) error {
  64. if err != nil {
  65. return err
  66. }
  67. if d.IsDir() {
  68. return nil
  69. }
  70. relPath, err := filepath.Rel(s.cfg.LoadBase, path)
  71. if err != nil {
  72. return err
  73. }
  74. pathes = append(pathes, filepath.ToSlash(relPath))
  75. return nil
  76. })
  77. if err != nil {
  78. return nil, err
  79. }
  80. } else {
  81. files, err := os.ReadDir(fullPath)
  82. if err != nil {
  83. return nil, err
  84. }
  85. for _, f := range files {
  86. if f.IsDir() {
  87. continue
  88. }
  89. relPath, err := filepath.Rel(s.cfg.LoadBase, filepath.Join(fullPath, f.Name()))
  90. if err != nil {
  91. return nil, err
  92. }
  93. pathes = append(pathes, filepath.ToSlash(relPath))
  94. }
  95. }
  96. return pathes, nil
  97. }
  98. func (s *PublicStore) getLogger() logger.Logger {
  99. return logger.WithField("PublicStore", "Local").WithField("Storage", s.agt.Detail.Storage.String())
  100. }

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