diff --git a/internal/core/persistence/init.go b/internal/core/persistence/init.go index 9e784e6cf..51eca5e42 100644 --- a/internal/core/persistence/init.go +++ b/internal/core/persistence/init.go @@ -1,6 +1,9 @@ package persistence import ( + "path" + "strings" + "github.com/langgenius/dify-cloud-kit/oss" "github.com/langgenius/dify-plugin-daemon/internal/types/app" @@ -12,8 +15,18 @@ var ( ) func InitPersistence(oss oss.OSS, config *app.Config) { + storagePath := config.PersistenceStoragePath + if prefix := strings.Trim(config.StoragePathPrefix, "/"); prefix != "" { + for _, seg := range strings.Split(prefix, "/") { + if seg == ".." { + log.Panic("STORAGE_PATH_PREFIX must not contain '..'") + } + } + storagePath = path.Join(prefix, storagePath) + } + persistence = &Persistence{ - storage: NewWrapper(oss, config.PersistenceStoragePath), + storage: NewWrapper(oss, storagePath), maxStorageSize: config.PersistenceStorageMaxSize, } diff --git a/internal/core/plugin_manager/manager.go b/internal/core/plugin_manager/manager.go index 82542fce9..9c9e635a4 100644 --- a/internal/core/plugin_manager/manager.go +++ b/internal/core/plugin_manager/manager.go @@ -2,6 +2,7 @@ package plugin_manager import ( "fmt" + "path" "strings" lru "github.com/hashicorp/golang-lru/v2" @@ -58,20 +59,36 @@ var ( ) func InitGlobalManager(oss oss.OSS, config *app.Config) *PluginManager { + prefix := strings.Trim(config.StoragePathPrefix, "/") + if prefix != "" { + for _, seg := range strings.Split(prefix, "/") { + if seg == ".." { + log.Panic("STORAGE_PATH_PREFIX must not contain '..'") + } + } + } + + joinPrefix := func(p string) string { + if prefix == "" { + return p + } + return path.Join(prefix, p) + } + mediaBucket := media_transport.NewAssetsBucket( oss, - config.PluginMediaCachePath, + joinPrefix(config.PluginMediaCachePath), config.PluginMediaCacheSize, ) installedBucket := media_transport.NewInstalledBucket( oss, - config.PluginInstalledPath, + joinPrefix(config.PluginInstalledPath), ) packageBucket := media_transport.NewPackageBucket( oss, - config.PluginPackageCachePath, + joinPrefix(config.PluginPackageCachePath), ) pluginAssetCache, err := lru.New[string, []byte](int(config.PluginAssetCacheSize)) diff --git a/internal/types/app/config.go b/internal/types/app/config.go index 9a69b31da..707fdc165 100644 --- a/internal/types/app/config.go +++ b/internal/types/app/config.go @@ -159,6 +159,9 @@ type Config struct { DBReadTimeout time.Duration `envconfig:"DB_READ_TIMEOUT" default:"30s"` DBWriteTimeout time.Duration `envconfig:"DB_WRITE_TIMEOUT" default:"30s"` + // global storage path prefix + StoragePathPrefix string `envconfig:"STORAGE_PATH_PREFIX"` + // persistence storage PersistenceStoragePath string `envconfig:"PERSISTENCE_STORAGE_PATH"` PersistenceStorageMaxSize int64 `envconfig:"PERSISTENCE_STORAGE_MAX_SIZE"`