local_storage.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package local
  2. import (
  3. "os"
  4. "path/filepath"
  5. "github.com/langgenius/dify-plugin-daemon/internal/oss"
  6. )
  7. type LocalStorage struct {
  8. root string
  9. }
  10. func NewLocalStorage(root string) oss.OSS {
  11. return &LocalStorage{root: root}
  12. }
  13. func (l *LocalStorage) Save(key string, data []byte) error {
  14. path := filepath.Join(l.root, key)
  15. return os.WriteFile(path, data, 0o644)
  16. }
  17. func (l *LocalStorage) Load(key string) ([]byte, error) {
  18. path := filepath.Join(l.root, key)
  19. return os.ReadFile(path)
  20. }
  21. func (l *LocalStorage) Exists(key string) (bool, error) {
  22. path := filepath.Join(l.root, key)
  23. _, err := os.Stat(path)
  24. return err == nil, nil
  25. }
  26. func (l *LocalStorage) State(key string) (oss.OSSState, error) {
  27. path := filepath.Join(l.root, key)
  28. info, err := os.Stat(path)
  29. if err != nil {
  30. return oss.OSSState{}, err
  31. }
  32. return oss.OSSState{Size: info.Size(), LastModified: info.ModTime()}, nil
  33. }
  34. func (l *LocalStorage) List(prefix string) ([]string, error) {
  35. prefix = filepath.Join(l.root, prefix)
  36. entries, err := os.ReadDir(prefix)
  37. if err != nil {
  38. return nil, err
  39. }
  40. paths := make([]string, 0, len(entries))
  41. for _, entry := range entries {
  42. paths = append(paths, filepath.Join(prefix, entry.Name()))
  43. }
  44. return paths, nil
  45. }
  46. func (l *LocalStorage) Delete(key string) error {
  47. path := filepath.Join(l.root, key)
  48. return os.RemoveAll(path)
  49. }