feat: set default storage by inline keyboard

This commit is contained in:
krau
2025-02-19 12:23:12 +08:00
parent 692e970772
commit c4eb824457
11 changed files with 157 additions and 32 deletions

View File

@@ -24,15 +24,6 @@ type Alist struct {
config config.AlistStorageConfig
}
var ConfigurableItems = []string{
"url",
"username",
"password",
"base_path",
"token_exp",
"token",
}
func (a *Alist) Init(cfg config.StorageConfig) error {
alistConfig, ok := cfg.(*config.AlistStorageConfig)
if !ok {

View File

@@ -15,10 +15,6 @@ type Local struct {
config config.LocalStorageConfig
}
var ConfigurableItems = []string{
"base_path",
}
func (l *Local) Init(cfg config.StorageConfig) error {
localConfig, ok := cfg.(*config.LocalStorageConfig)
if !ok {

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/logger"
"github.com/krau/SaveAny-Bot/storage/alist"
"github.com/krau/SaveAny-Bot/storage/local"
"github.com/krau/SaveAny-Bot/storage/webdav"
@@ -44,6 +45,19 @@ func GetStorageByName(name string) (Storage, error) {
return storage, nil
}
// 检查 user 是否可用指定的 storage, 若不可用则返回未找到错误
func GetStorageByUserIDAndName(chatID int64, name string) (Storage, error) {
if name == "" {
return nil, fmt.Errorf("storage name is required")
}
if !config.Cfg.HasStorage(chatID, name) {
return nil, fmt.Errorf("storage %s not found for user %d", name, chatID)
}
return GetStorageByName(name)
}
func GetUserStorages(chatID int64) []Storage {
var storages []Storage
for _, name := range config.Cfg.GetStorageNamesByUserID(chatID) {
@@ -78,14 +92,13 @@ func NewStorage(cfg config.StorageConfig) (Storage, error) {
return storage, nil
}
func GetStorageConfigurableItems(storageType types.StorageType) []string {
switch storageType {
case types.StorageTypeAlist:
return alist.ConfigurableItems
case types.StorageTypeLocal:
return local.ConfigurableItems
case types.StorageTypeWebdav:
return webdav.ConfigurableItems
func LoadStorages() {
logger.L.Info("Loading storages")
for _, storage := range config.Cfg.Storages {
_, err := GetStorageByName(storage.GetName())
if err != nil {
logger.L.Errorf("Failed to load storage %s: %v", storage.GetName(), err)
}
}
return nil
logger.L.Infof("Successfully loaded %d storages", len(Storages))
}

View File

@@ -18,8 +18,6 @@ type Webdav struct {
client *gowebdav.Client
}
var ConfigurableItems = []string{"url", "username", "password", "base_path"}
func (w *Webdav) Init(cfg config.StorageConfig) error {
webdavConfig, ok := cfg.(*config.WebdavStorageConfig)
if !ok {