fix storage api

This commit is contained in:
jxxghp
2024-08-15 15:27:47 +08:00
parent 65c8f35f6d
commit 47481d2482
11 changed files with 108 additions and 31 deletions
+11
View File
@@ -164,3 +164,14 @@ def rename(fileitem: schemas.FileItem,
progress.end(ProgressKey.BatchRename) progress.end(ProgressKey.BatchRename)
return schemas.Response(success=True) return schemas.Response(success=True)
return schemas.Response(success=False) 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()
+6
View File
@@ -69,3 +69,9 @@ class StorageChain(ChainBase):
快照存储 快照存储
""" """
return self.run_module("snapshot_storage", storage=storage, path=path) 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)
+11 -1
View File
@@ -16,7 +16,7 @@ from app.helper.module import ModuleHelper
from app.log import logger from app.log import logger
from app.modules import _ModuleBase from app.modules import _ModuleBase
from app.modules.filemanager.storage import StorageBase 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.schemas.types import MediaType
from app.utils.system import SystemUtils from app.utils.system import SystemUtils
@@ -193,6 +193,16 @@ class FileManagerModule(_ModuleBase):
return None return None
return storage_oper.snapshot(path) 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, def transfer(self, fileitem: FileItem, meta: MetaBase, mediainfo: MediaInfo,
transfer_type: str, target_storage: str = None, target_path: Path = None, transfer_type: str, target_storage: str = None, target_path: Path = None,
episodes_info: List[TmdbEpisode] = None, episodes_info: List[TmdbEpisode] = None,
@@ -138,6 +138,13 @@ class StorageBase(metaclass=ABCMeta):
""" """
pass pass
@abstractmethod
def usage(self) -> Optional[schemas.StorageUsage]:
"""
存储使用情况
"""
pass
def snapshot(self, path: Path) -> Dict[str, float]: def snapshot(self, path: Path) -> Dict[str, float]:
""" """
快照文件系统,输出所有层级文件信息(不含目录) 快照文件系统,输出所有层级文件信息(不含目录)
@@ -744,3 +744,9 @@ class AliPan(StorageBase):
软链接文件 软链接文件
""" """
pass pass
def usage(self) -> Optional[schemas.StorageUsage]:
"""
存储使用情况
"""
pass
+12
View File
@@ -3,6 +3,7 @@ from pathlib import Path
from typing import Optional, List from typing import Optional, List
from app import schemas from app import schemas
from app.helper.directory import DirectoryHelper
from app.log import logger from app.log import logger
from app.modules.filemanager.storage import StorageBase from app.modules.filemanager.storage import StorageBase
from app.schemas.types import StorageSchema from app.schemas.types import StorageSchema
@@ -231,3 +232,14 @@ class LocalStorage(StorageBase):
logger.error(f"移动文件失败:{message}") logger.error(f"移动文件失败:{message}")
return False return False
return True 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
)
@@ -296,3 +296,9 @@ class Rclone(StorageBase):
def softlink(self, fileitm: schemas.FileItem, target_file: Path) -> bool: def softlink(self, fileitm: schemas.FileItem, target_file: Path) -> bool:
pass pass
def usage(self) -> Optional[schemas.StorageUsage]:
"""
存储使用情况
"""
pass
+6
View File
@@ -375,3 +375,9 @@ class U115Pan(StorageBase, metaclass=Singleton):
def softlink(self, fileitm: schemas.FileItem, target_file: Path) -> bool: def softlink(self, fileitm: schemas.FileItem, target_file: Path) -> bool:
pass pass
def usage(self) -> Optional[schemas.StorageUsage]:
"""
存储使用情况
"""
pass
+1
View File
@@ -15,3 +15,4 @@ from .tmdb import *
from .transfer import * from .transfer import *
from .rule import * from .rule import *
from .system import * from .system import *
from .file import *
+41
View File
@@ -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
+1 -30
View File
@@ -3,36 +3,7 @@ from typing import Optional
from pydantic import BaseModel from pydantic import BaseModel
from app.schemas.file import FileItem
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 TransferTorrent(BaseModel): class TransferTorrent(BaseModel):