|
- package local
-
- import (
- "io"
- "io/fs"
- "os"
- "path/filepath"
-
- "gitlink.org.cn/cloudream/common/pkgs/logger"
- cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
- "gitlink.org.cn/cloudream/storage/common/pkgs/storage/types"
- )
-
- type PublicStoreDesc struct {
- types.EmptyPublicStoreDesc
- builder *builder
- }
-
- func (d *PublicStoreDesc) Enabled() bool {
- return d.builder.detail.Storage.PublicStore != nil
- }
-
- type PublicStore struct {
- agt *agent
- cfg cdssdk.LocalPublicStorage
- }
-
- func NewPublicStore(agt *agent, cfg cdssdk.LocalPublicStorage) (*PublicStore, error) {
- return &PublicStore{
- agt: agt,
- cfg: cfg,
- }, nil
- }
-
- func (s *PublicStore) Start(ch *types.StorageEventChan) {
- s.getLogger().Infof("component start, LoadBase: %v", s.cfg.LoadBase)
- }
-
- func (s *PublicStore) Stop() {
- s.getLogger().Infof("component stop")
- }
-
- func (s *PublicStore) Write(objPath string, stream io.Reader) error {
- fullPath := filepath.Join(s.cfg.LoadBase, objPath)
- err := os.MkdirAll(filepath.Dir(fullPath), 0755)
- if err != nil {
- return err
- }
-
- f, err := os.Create(fullPath)
- if err != nil {
- return err
- }
- defer f.Close()
-
- _, err = io.Copy(f, stream)
- if err != nil {
- return err
- }
-
- return nil
- }
-
- func (s *PublicStore) Read(objPath string) (io.ReadCloser, error) {
- fullPath := filepath.Join(s.cfg.LoadBase, objPath)
- f, err := os.Open(fullPath)
- if err != nil {
- return nil, err
- }
-
- return f, nil
- }
-
- func (s *PublicStore) List(path string, recursive bool) ([]string, error) {
- fullPath := filepath.Join(s.cfg.LoadBase, path)
-
- var pathes []string
- if recursive {
- err := filepath.WalkDir(fullPath, func(path string, d fs.DirEntry, err error) error {
- if err != nil {
- return err
- }
- if d.IsDir() {
- return nil
- }
-
- relPath, err := filepath.Rel(s.cfg.LoadBase, path)
- if err != nil {
- return err
- }
-
- pathes = append(pathes, filepath.ToSlash(relPath))
- return nil
- })
- if err != nil {
- return nil, err
- }
-
- } else {
- files, err := os.ReadDir(fullPath)
- if err != nil {
- return nil, err
- }
-
- for _, f := range files {
- if f.IsDir() {
- continue
- }
-
- relPath, err := filepath.Rel(s.cfg.LoadBase, filepath.Join(fullPath, f.Name()))
- if err != nil {
- return nil, err
- }
-
- pathes = append(pathes, filepath.ToSlash(relPath))
- }
- }
-
- return pathes, nil
- }
-
- func (s *PublicStore) getLogger() logger.Logger {
- return logger.WithField("PublicStore", "Local").WithField("Storage", s.agt.Detail.Storage.String())
- }
|