wrapper.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package persistence
  2. import (
  3. "path"
  4. "github.com/langgenius/dify-plugin-daemon/internal/oss"
  5. )
  6. type wrapper struct {
  7. oss oss.OSS
  8. persistenceStoragePath string
  9. }
  10. func NewWrapper(oss oss.OSS, persistenceStoragePath string) *wrapper {
  11. return &wrapper{
  12. oss: oss,
  13. persistenceStoragePath: persistenceStoragePath,
  14. }
  15. }
  16. func (s *wrapper) getFilePath(tenant_id string, plugin_checksum string, key string) string {
  17. return path.Join(s.persistenceStoragePath, tenant_id, plugin_checksum, key)
  18. }
  19. func (s *wrapper) Save(tenant_id string, plugin_checksum string, key string, data []byte) error {
  20. // save to s3
  21. filePath := s.getFilePath(tenant_id, plugin_checksum, key)
  22. return s.oss.Save(filePath, data)
  23. }
  24. func (s *wrapper) Load(tenant_id string, plugin_checksum string, key string) ([]byte, error) {
  25. // load from s3
  26. filePath := s.getFilePath(tenant_id, plugin_checksum, key)
  27. return s.oss.Load(filePath)
  28. }
  29. func (s *wrapper) Delete(tenant_id string, plugin_checksum string, key string) error {
  30. filePath := s.getFilePath(tenant_id, plugin_checksum, key)
  31. return s.oss.Delete(filePath)
  32. }
  33. func (s *wrapper) StateSize(tenant_id string, plugin_checksum string, key string) (int64, error) {
  34. filePath := s.getFilePath(tenant_id, plugin_checksum, key)
  35. state, err := s.oss.State(filePath)
  36. if err != nil {
  37. return 0, err
  38. }
  39. return state.Size, nil
  40. }