mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-07-12 16:02:05 +08:00
refactor: streamline storage configuration loading and remove redundant code
This commit is contained in:
@@ -1,5 +1,3 @@
|
|||||||
// storage_config.go
|
|
||||||
|
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -18,25 +16,49 @@ type StorageConfig interface {
|
|||||||
|
|
||||||
// Base storage config
|
// Base storage config
|
||||||
type NewStorageConfig struct {
|
type NewStorageConfig struct {
|
||||||
Name string `toml:"name" mapstructure:"name" json:"name"`
|
Name string `toml:"name" mapstructure:"name" json:"name"`
|
||||||
Type string `toml:"type" mapstructure:"type" json:"type"`
|
Type string `toml:"type" mapstructure:"type" json:"type"`
|
||||||
Enable bool `toml:"enable" mapstructure:"enable" json:"enable"`
|
Enable bool `toml:"enable" mapstructure:"enable" json:"enable"`
|
||||||
RawConfig map[string]interface{} `toml:"-" mapstructure:",remain"`
|
RawConfig map[string]any `toml:"-" mapstructure:",remain"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type StorageConfigFactory func(cfg *NewStorageConfig) (StorageConfig, error)
|
var storageFactories = map[types.StorageType]func(cfg *NewStorageConfig) (StorageConfig, error){
|
||||||
|
types.StorageTypeLocal: newLocalStorageConfig,
|
||||||
var storageFactories = make(map[string]StorageConfigFactory)
|
types.StorageTypeAlist: newAlistStorageConfig,
|
||||||
|
types.StorageTypeWebdav: newWebdavStorageConfig,
|
||||||
func RegisterStorageFactory(storageType string, factory StorageConfigFactory) {
|
types.StorageTypeMinio: newMinioStorageConfig,
|
||||||
storageFactories[storageType] = factory
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func LoadStorageConfigs(v *viper.Viper) ([]StorageConfig, error) {
|
||||||
RegisterStorageFactory(string(types.StorageTypeLocal), newLocalStorageConfig)
|
var baseConfigs []NewStorageConfig
|
||||||
RegisterStorageFactory(string(types.StorageTypeAlist), newAlistStorageConfig)
|
if err := v.UnmarshalKey("storages", &baseConfigs); err != nil {
|
||||||
RegisterStorageFactory(string(types.StorageTypeWebdav), newWebdavStorageConfig)
|
return nil, fmt.Errorf("failed to unmarshal storage configs: %w", err)
|
||||||
RegisterStorageFactory(string(types.StorageTypeMinio), newMinioStorageConfig)
|
}
|
||||||
|
|
||||||
|
var configs []StorageConfig
|
||||||
|
for _, baseCfg := range baseConfigs {
|
||||||
|
if !baseCfg.Enable {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
factory, ok := storageFactories[types.StorageType(baseCfg.Type)]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("unsupported storage type: %s", baseCfg.Type)
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg, err := factory(&baseCfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create storage config for %s: %w", baseCfg.Name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := cfg.Validate(); err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid storage config for %s: %w", baseCfg.Name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
configs = append(configs, cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
return configs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newLocalStorageConfig(cfg *NewStorageConfig) (StorageConfig, error) {
|
func newLocalStorageConfig(cfg *NewStorageConfig) (StorageConfig, error) {
|
||||||
@@ -72,38 +94,6 @@ func newWebdavStorageConfig(cfg *NewStorageConfig) (StorageConfig, error) {
|
|||||||
return &webdavCfg, nil
|
return &webdavCfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadStorageConfigs(v *viper.Viper) ([]StorageConfig, error) {
|
|
||||||
var baseConfigs []NewStorageConfig
|
|
||||||
if err := v.UnmarshalKey("storages", &baseConfigs); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to unmarshal storage configs: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var configs []StorageConfig
|
|
||||||
for _, baseCfg := range baseConfigs {
|
|
||||||
if !baseCfg.Enable {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
factory, ok := storageFactories[baseCfg.Type]
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unsupported storage type: %s", baseCfg.Type)
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg, err := factory(&baseCfg)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to create storage config for %s: %w", baseCfg.Name, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := cfg.Validate(); err != nil {
|
|
||||||
return nil, fmt.Errorf("invalid storage config for %s: %w", baseCfg.Name, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
configs = append(configs, cfg)
|
|
||||||
}
|
|
||||||
|
|
||||||
return configs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func newMinioStorageConfig(cfg *NewStorageConfig) (StorageConfig, error) {
|
func newMinioStorageConfig(cfg *NewStorageConfig) (StorageConfig, error) {
|
||||||
var minioCfg MinioStorageConfig
|
var minioCfg MinioStorageConfig
|
||||||
minioCfg.NewStorageConfig = *cfg
|
minioCfg.NewStorageConfig = *cfg
|
||||||
|
|||||||
Reference in New Issue
Block a user