feat: refactor storage retrieval functions and improve documentation

This commit is contained in:
krau
2025-12-19 13:49:20 +08:00
parent df64ec3069
commit 5f7b936270
2 changed files with 7 additions and 4 deletions
+5 -4
View File
@@ -11,7 +11,8 @@ import (
var UserStorages = make(map[int64][]Storage) var UserStorages = make(map[int64][]Storage)
// GetStorageByName returns storage by name from cache or creates new one // GetStorageByName returns storage by name from cache or creates new one
func getStorageByName(ctx context.Context, name string) (Storage, error) { // It should NOT be used to get storage for user, use GetStorageByUserIDAndName instead
func GetStorageByName(ctx context.Context, name string) (Storage, error) {
if name == "" { if name == "" {
return nil, ErrStorageNameEmpty return nil, ErrStorageNameEmpty
} }
@@ -43,7 +44,7 @@ func GetStorageByUserIDAndName(ctx context.Context, chatID int64, name string) (
return nil, fmt.Errorf("no storage %s for user %d", name, chatID) return nil, fmt.Errorf("no storage %s for user %d", name, chatID)
} }
return getStorageByName(ctx, name) return GetStorageByName(ctx, name)
} }
func GetUserStorages(ctx context.Context, chatID int64) []Storage { func GetUserStorages(ctx context.Context, chatID int64) []Storage {
@@ -55,7 +56,7 @@ func GetUserStorages(ctx context.Context, chatID int64) []Storage {
} }
var storages []Storage var storages []Storage
for _, name := range config.C().GetStorageNamesByUserID(chatID) { for _, name := range config.C().GetStorageNamesByUserID(chatID) {
storage, err := getStorageByName(ctx, name) storage, err := GetStorageByName(ctx, name)
if err != nil { if err != nil {
continue continue
} }
@@ -68,7 +69,7 @@ func LoadStorages(ctx context.Context) {
logger := log.FromContext(ctx) logger := log.FromContext(ctx)
logger.Info("加载存储...") logger.Info("加载存储...")
for _, storage := range config.C().Storages { for _, storage := range config.C().Storages {
_, err := getStorageByName(ctx, storage.GetName()) _, err := GetStorageByName(ctx, storage.GetName())
if err != nil { if err != nil {
logger.Errorf("加载存储 %s 失败: %v", storage.GetName(), err) logger.Errorf("加载存储 %s 失败: %v", storage.GetName(), err)
} }
+2
View File
@@ -16,6 +16,7 @@ import (
) )
type Storage interface { type Storage interface {
// Init 只应该在创建存储时调用一次
Init(ctx context.Context, cfg storcfg.StorageConfig) error Init(ctx context.Context, cfg storcfg.StorageConfig) error
Type() storenum.StorageType Type() storenum.StorageType
Name() string Name() string
@@ -42,6 +43,7 @@ var storageConstructors = map[storenum.StorageType]StorageConstructor{
storenum.Telegram: func() Storage { return new(telegram.Telegram) }, storenum.Telegram: func() Storage { return new(telegram.Telegram) },
} }
// NewStorage creates a new storage instance based on the provided config and initializes it
func NewStorage(ctx context.Context, cfg storcfg.StorageConfig) (Storage, error) { func NewStorage(ctx context.Context, cfg storcfg.StorageConfig) (Storage, error) {
constructor, ok := storageConstructors[cfg.GetType()] constructor, ok := storageConstructors[cfg.GetType()]
if !ok { if !ok {