feat!: (WIP) switched back to using config files config storages because the conversation handling is shit

This commit is contained in:
krau
2025-02-19 11:05:30 +08:00
parent 80696c9661
commit 692e970772
24 changed files with 584 additions and 645 deletions

View File

@@ -2,9 +2,9 @@ package storage
import (
"context"
"errors"
"fmt"
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/storage/alist"
"github.com/krau/SaveAny-Bot/storage/local"
"github.com/krau/SaveAny-Bot/storage/webdav"
@@ -12,34 +12,50 @@ import (
)
type Storage interface {
Init(model types.StorageModel) error
Init(cfg config.StorageConfig) error
Type() types.StorageType
Name() string
JoinStoragePath(task types.Task) string
Save(cttx context.Context, localFilePath, storagePath string) error
}
var (
ErrInvalidStorageID = errors.New("invalid storage ID")
)
var Storages = make(map[string]Storage)
var Storages = make(map[uint]Storage)
// Get storage from model, if it exists, otherwise create and init a new storage
func GetStorageFromModel(model types.StorageModel) (Storage, error) {
if model.ID == 0 {
return nil, ErrInvalidStorageID
// GetStorageByName returns storage by name from cache or creates new one
func GetStorageByName(name string) (Storage, error) {
if name == "" {
return nil, fmt.Errorf("storage name is required")
}
if storage, ok := Storages[model.ID]; ok {
storage, ok := Storages[name]
if ok {
return storage, nil
}
storage, err := NewStorage(model)
cfg := config.Cfg.GetStorageByName(name)
if cfg == nil {
return nil, fmt.Errorf("storage %s not found", name)
}
storage, err := NewStorage(cfg)
if err != nil {
return nil, err
}
Storages[model.ID] = storage
Storages[name] = storage
return storage, nil
}
func GetUserStorages(chatID int64) []Storage {
var storages []Storage
for _, name := range config.Cfg.GetStorageNamesByUserID(chatID) {
storage, err := GetStorageByName(name)
if err != nil {
continue
}
storages = append(storages, storage)
}
return storages
}
type StorageConstructor func() Storage
var storageConstructors = map[string]StorageConstructor{
@@ -48,15 +64,15 @@ var storageConstructors = map[string]StorageConstructor{
string(types.StorageTypeWebdav): func() Storage { return new(webdav.Webdav) },
}
func NewStorage(model types.StorageModel) (Storage, error) {
constructor, ok := storageConstructors[model.Type]
func NewStorage(cfg config.StorageConfig) (Storage, error) {
constructor, ok := storageConstructors[string(cfg.GetType())]
if !ok {
return nil, fmt.Errorf("unsupported storage type: %s", model.Type)
return nil, fmt.Errorf("unsupported storage type: %s", cfg.GetType())
}
storage := constructor()
if err := storage.Init(model); err != nil {
return nil, fmt.Errorf("failed to init %s storage: %w", model.Type, err)
if err := storage.Init(cfg); err != nil {
return nil, fmt.Errorf("failed to init %s storage: %w", cfg.GetName(), err)
}
return storage, nil