From b37e50480a9ebb2fb557f7e6436e8104433fe779 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Tue, 2 Jul 2024 18:31:17 +0800 Subject: [PATCH] fix storage --- app/helper/storage.py | 31 ++++++++++++++++++++- app/modules/filemanager/storage/__init__.py | 18 +++++++++++- app/modules/filemanager/storage/alipan.py | 14 ++++------ app/modules/filemanager/storage/u115.py | 15 ++++------ app/schemas/system.py | 2 -- app/schemas/types.py | 4 --- 6 files changed, 59 insertions(+), 25 deletions(-) diff --git a/app/helper/storage.py b/app/helper/storage.py index 2216b88f..dcddd072 100644 --- a/app/helper/storage.py +++ b/app/helper/storage.py @@ -1,4 +1,4 @@ -from typing import List +from typing import List, Optional from app import schemas from app.db.systemconfig_oper import SystemConfigOper @@ -21,3 +21,32 @@ class StorageHelper: if not storage_confs: return [] return [schemas.StorageConf(**s) for s in storage_confs] + + def get_storage(self, storage: str) -> Optional[schemas.StorageConf]: + """ + 获取指定存储配置 + """ + storagies = self.get_storagies() + for s in storagies: + if s.type == storage: + return s + return None + + def set_storage(self, storage: str, conf: dict): + """ + 设置存储配置 + """ + storagies = self.get_storagies() + if not storagies: + storagies = [ + schemas.StorageConf( + type=storage, + config=conf + ) + ] + else: + for s in storagies: + if s.type == storage: + s.config = conf + break + self.systemconfig.set(SystemConfigKey.Storages, [s.dict() for s in storagies]) diff --git a/app/modules/filemanager/storage/__init__.py b/app/modules/filemanager/storage/__init__.py index 062af698..c7430856 100644 --- a/app/modules/filemanager/storage/__init__.py +++ b/app/modules/filemanager/storage/__init__.py @@ -3,15 +3,31 @@ from pathlib import Path from typing import Optional, List from app import schemas +from app.helper.storage import StorageHelper class StorageBase(metaclass=ABCMeta): """ 存储基类 """ - + schema = None transtype = {} + def __init__(self): + self.storagehelper = StorageHelper() + + def get_config(self) -> Optional[schemas.StorageConf]: + """ + 获取配置 + """ + return self.storagehelper.get_storage(self.schema.value) + + def set_config(self, conf: dict): + """ + 设置配置 + """ + self.storagehelper.set_storage(self.schema.value, conf) + def support_transtype(self) -> dict: """ 支持的整理方式 diff --git a/app/modules/filemanager/storage/alipan.py b/app/modules/filemanager/storage/alipan.py index b78928d3..f0c6359c 100644 --- a/app/modules/filemanager/storage/alipan.py +++ b/app/modules/filemanager/storage/alipan.py @@ -9,10 +9,9 @@ from requests import Response from app import schemas from app.core.config import settings -from app.db.systemconfig_oper import SystemConfigOper from app.log import logger from app.modules.filemanager.storage import StorageBase -from app.schemas.types import SystemConfigKey, StorageSchema +from app.schemas.types import StorageSchema from app.utils.http import RequestUtils from app.utils.string import StringUtils from app.utils.system import SystemUtils @@ -25,6 +24,7 @@ class AliPan(StorageBase): # 存储类型 schema = StorageSchema.Alipan + # 支持的整理方式 transtype = { "move": "移动" @@ -65,9 +65,6 @@ class AliPan(StorageBase): # 上传文件完成 upload_file_complete_url = "https://api.aliyundrive.com/v2/file/complete" - def __init__(self): - self.systemconfig = SystemConfigOper() - def __handle_error(self, res: Response, apiname: str, action: bool = True): """ 统一处理和打印错误信息 @@ -103,7 +100,8 @@ class AliPan(StorageBase): """ 获取阿里云盘认证参数并初始化参数格式 """ - return self.systemconfig.get(SystemConfigKey.UserAliyunParams) or {} + conf = self.get_config() + return conf.config if conf else {} def __update_params(self, params: dict): """ @@ -111,13 +109,13 @@ class AliPan(StorageBase): """ current_params = self.__auth_params current_params.update(params) - self.systemconfig.set(SystemConfigKey.UserAliyunParams, current_params) + self.set_config(current_params) def __clear_params(self): """ 清除阿里云盘认证参数 """ - self.systemconfig.delete(SystemConfigKey.UserAliyunParams) + self.set_config({}) def generate_qrcode(self) -> Optional[Tuple[dict, str]]: """ diff --git a/app/modules/filemanager/storage/u115.py b/app/modules/filemanager/storage/u115.py index 7f47fb91..d356bfa0 100644 --- a/app/modules/filemanager/storage/u115.py +++ b/app/modules/filemanager/storage/u115.py @@ -8,10 +8,9 @@ from py115 import Cloud from py115.types import LoginTarget, QrcodeSession, QrcodeStatus, Credential from app import schemas -from app.db.systemconfig_oper import SystemConfigOper from app.log import logger from app.modules.filemanager.storage import StorageBase -from app.schemas.types import SystemConfigKey, StorageSchema +from app.schemas.types import StorageSchema from app.utils.http import RequestUtils from app.utils.singleton import Singleton @@ -23,6 +22,7 @@ class U115Pan(StorageBase, metaclass=Singleton): # 存储类型 schema = StorageSchema.U115 + # 支持的整理方式 transtype = { "move": "移动" @@ -31,9 +31,6 @@ class U115Pan(StorageBase, metaclass=Singleton): cloud: Optional[Cloud] = None _session: QrcodeSession = None - def __init__(self): - self.systemconfig = SystemConfigOper() - def __init_cloud(self) -> bool: """ 初始化Cloud @@ -56,22 +53,22 @@ class U115Pan(StorageBase, metaclass=Singleton): """ 获取已保存的115认证参数 """ - cookie_dict = self.systemconfig.get(SystemConfigKey.User115Params) + cookie_dict = self.get_config() if not cookie_dict: return None - return Credential.from_dict(cookie_dict) + return Credential.from_dict(cookie_dict.dict()) def __save_credentail(self, credential: Credential): """ 设置115认证参数 """ - self.systemconfig.set(SystemConfigKey.User115Params, credential.to_dict()) + self.set_config(credential.to_dict()) def __clear_credential(self): """ 清除115认证参数 """ - self.systemconfig.delete(SystemConfigKey.User115Params) + self.set_config({}) def generate_qrcode(self) -> Optional[Tuple[dict, str]]: """ diff --git a/app/schemas/system.py b/app/schemas/system.py index 7ceab74c..047d0000 100644 --- a/app/schemas/system.py +++ b/app/schemas/system.py @@ -53,8 +53,6 @@ class StorageConf(BaseModel): """ 存储配置 """ - # 名称 - name: Optional[str] = None # 类型 local/alipan/u115/rclone type: Optional[str] = None # 配置 diff --git a/app/schemas/types.py b/app/schemas/types.py index 314a14a8..ec7422ea 100644 --- a/app/schemas/types.py +++ b/app/schemas/types.py @@ -68,10 +68,6 @@ class SystemConfigKey(Enum): Directories = "Directories" # 存储配置 Storages = "Storages" - # 阿里云盘认证参数 - UserAliyunParams = "UserAliyunParams" - # 115网盘认证参数 - User115Params = "User115Params" # 搜索站点范围 IndexerSites = "IndexerSites" # 订阅站点范围