From 47481d24824003a3c7951f00649d8a527f593c03 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Thu, 15 Aug 2024 15:27:47 +0800 Subject: [PATCH] fix storage api --- app/api/endpoints/storage.py | 11 ++++++ app/chain/storage.py | 6 +++ app/modules/filemanager/__init__.py | 12 +++++- app/modules/filemanager/storage/__init__.py | 7 ++++ app/modules/filemanager/storage/alipan.py | 6 +++ app/modules/filemanager/storage/local.py | 12 ++++++ app/modules/filemanager/storage/rclone.py | 6 +++ app/modules/filemanager/storage/u115.py | 6 +++ app/schemas/__init__.py | 1 + app/schemas/file.py | 41 +++++++++++++++++++++ app/schemas/transfer.py | 31 +--------------- 11 files changed, 108 insertions(+), 31 deletions(-) create mode 100644 app/schemas/file.py diff --git a/app/api/endpoints/storage.py b/app/api/endpoints/storage.py index 1497bc54..80a09b82 100644 --- a/app/api/endpoints/storage.py +++ b/app/api/endpoints/storage.py @@ -164,3 +164,14 @@ def rename(fileitem: schemas.FileItem, progress.end(ProgressKey.BatchRename) return schemas.Response(success=True) return schemas.Response(success=False) + + +@router.get("/usage/{name}", summary="存储空间信息", response_model=schemas.StorageUsage) +def usage(name: str, _: schemas.TokenPayload = Depends(get_current_active_superuser)) -> Any: + """ + 查询存储空间 + """ + ret = StorageChain().storage_usage(name) + if ret: + return ret + return schemas.StorageUsage() diff --git a/app/chain/storage.py b/app/chain/storage.py index ba200360..05c35506 100644 --- a/app/chain/storage.py +++ b/app/chain/storage.py @@ -69,3 +69,9 @@ class StorageChain(ChainBase): 快照存储 """ return self.run_module("snapshot_storage", storage=storage, path=path) + + def storage_usage(self, storage: str) -> Optional[schemas.StorageUsage]: + """ + 存储使用情况 + """ + return self.run_module("storage_usage", storage=storage) diff --git a/app/modules/filemanager/__init__.py b/app/modules/filemanager/__init__.py index f785274a..f1bbcc86 100644 --- a/app/modules/filemanager/__init__.py +++ b/app/modules/filemanager/__init__.py @@ -16,7 +16,7 @@ from app.helper.module import ModuleHelper from app.log import logger from app.modules import _ModuleBase from app.modules.filemanager.storage import StorageBase -from app.schemas import TransferInfo, ExistMediaInfo, TmdbEpisode, TransferDirectoryConf, FileItem +from app.schemas import TransferInfo, ExistMediaInfo, TmdbEpisode, TransferDirectoryConf, FileItem, StorageUsage from app.schemas.types import MediaType from app.utils.system import SystemUtils @@ -193,6 +193,16 @@ class FileManagerModule(_ModuleBase): return None return storage_oper.snapshot(path) + def storage_usage(self, storage: str) -> Optional[StorageUsage]: + """ + 存储使用情况 + """ + storage_oper = self.__get_storage_oper(storage) + if not storage_oper: + logger.error(f"不支持 {storage} 的存储使用情况") + return None + return storage_oper.usage() + def transfer(self, fileitem: FileItem, meta: MetaBase, mediainfo: MediaInfo, transfer_type: str, target_storage: str = None, target_path: Path = None, episodes_info: List[TmdbEpisode] = None, diff --git a/app/modules/filemanager/storage/__init__.py b/app/modules/filemanager/storage/__init__.py index bc705fe6..60879efd 100644 --- a/app/modules/filemanager/storage/__init__.py +++ b/app/modules/filemanager/storage/__init__.py @@ -138,6 +138,13 @@ class StorageBase(metaclass=ABCMeta): """ pass + @abstractmethod + def usage(self) -> Optional[schemas.StorageUsage]: + """ + 存储使用情况 + """ + pass + def snapshot(self, path: Path) -> Dict[str, float]: """ 快照文件系统,输出所有层级文件信息(不含目录) diff --git a/app/modules/filemanager/storage/alipan.py b/app/modules/filemanager/storage/alipan.py index 240ab7b2..68aa8e18 100644 --- a/app/modules/filemanager/storage/alipan.py +++ b/app/modules/filemanager/storage/alipan.py @@ -744,3 +744,9 @@ class AliPan(StorageBase): 软链接文件 """ pass + + def usage(self) -> Optional[schemas.StorageUsage]: + """ + 存储使用情况 + """ + pass diff --git a/app/modules/filemanager/storage/local.py b/app/modules/filemanager/storage/local.py index 26437811..2520a732 100644 --- a/app/modules/filemanager/storage/local.py +++ b/app/modules/filemanager/storage/local.py @@ -3,6 +3,7 @@ from pathlib import Path from typing import Optional, List from app import schemas +from app.helper.directory import DirectoryHelper from app.log import logger from app.modules.filemanager.storage import StorageBase from app.schemas.types import StorageSchema @@ -231,3 +232,14 @@ class LocalStorage(StorageBase): logger.error(f"移动文件失败:{message}") return False return True + + def usage(self) -> Optional[schemas.StorageUsage]: + """ + 存储使用情况 + """ + library_dirs = DirectoryHelper().get_local_library_dirs() + total_storage, free_storage = SystemUtils.space_usage([Path(d.library_path) for d in library_dirs]) + return schemas.StorageUsage( + total=total_storage, + available=free_storage + ) diff --git a/app/modules/filemanager/storage/rclone.py b/app/modules/filemanager/storage/rclone.py index 213e0ff8..db1af0dc 100644 --- a/app/modules/filemanager/storage/rclone.py +++ b/app/modules/filemanager/storage/rclone.py @@ -296,3 +296,9 @@ class Rclone(StorageBase): def softlink(self, fileitm: schemas.FileItem, target_file: Path) -> bool: pass + + def usage(self) -> Optional[schemas.StorageUsage]: + """ + 存储使用情况 + """ + pass diff --git a/app/modules/filemanager/storage/u115.py b/app/modules/filemanager/storage/u115.py index 38700955..c19cc98a 100644 --- a/app/modules/filemanager/storage/u115.py +++ b/app/modules/filemanager/storage/u115.py @@ -375,3 +375,9 @@ class U115Pan(StorageBase, metaclass=Singleton): def softlink(self, fileitm: schemas.FileItem, target_file: Path) -> bool: pass + + def usage(self) -> Optional[schemas.StorageUsage]: + """ + 存储使用情况 + """ + pass diff --git a/app/schemas/__init__.py b/app/schemas/__init__.py index f3fc1883..cab5d2c1 100644 --- a/app/schemas/__init__.py +++ b/app/schemas/__init__.py @@ -15,3 +15,4 @@ from .tmdb import * from .transfer import * from .rule import * from .system import * +from .file import * diff --git a/app/schemas/file.py b/app/schemas/file.py new file mode 100644 index 00000000..9f260290 --- /dev/null +++ b/app/schemas/file.py @@ -0,0 +1,41 @@ +from typing import Optional + +from pydantic import BaseModel + + +class FileItem(BaseModel): + # 存储类型 + storage: Optional[str] = "local" + # 类型 dir/file + type: Optional[str] = None + # 文件路径 + path: Optional[str] = "/" + # 文件名 + name: Optional[str] = None + # 文件名 + basename: Optional[str] = None + # 文件后缀 + extension: Optional[str] = None + # 文件大小 + size: Optional[int] = None + # 修改时间 + modify_time: Optional[float] = None + # 子节点 + children: Optional[list] = [] + # ID + fileid: Optional[str] = None + # 父ID + parent_fileid: Optional[str] = None + # 缩略图 + thumbnail: Optional[str] = None + # 115 pickcode + pickcode: Optional[str] = None + # drive_id + drive_id: Optional[str] = None + + +class StorageUsage(BaseModel): + # 总空间 + total: float = 0.0 + # 剩余空间 + available: float = 0.0 diff --git a/app/schemas/transfer.py b/app/schemas/transfer.py index 2813994d..5e55a6f1 100644 --- a/app/schemas/transfer.py +++ b/app/schemas/transfer.py @@ -3,36 +3,7 @@ from typing import Optional from pydantic import BaseModel - -class FileItem(BaseModel): - # 存储类型 - storage: Optional[str] = "local" - # 类型 dir/file - type: Optional[str] = None - # 文件路径 - path: Optional[str] = "/" - # 文件名 - name: Optional[str] = None - # 文件名 - basename: Optional[str] = None - # 文件后缀 - extension: Optional[str] = None - # 文件大小 - size: Optional[int] = None - # 修改时间 - modify_time: Optional[float] = None - # 子节点 - children: Optional[list] = [] - # ID - fileid: Optional[str] = None - # 父ID - parent_fileid: Optional[str] = None - # 缩略图 - thumbnail: Optional[str] = None - # 115 pickcode - pickcode: Optional[str] = None - # drive_id - drive_id: Optional[str] = None +from app.schemas.file import FileItem class TransferTorrent(BaseModel):