diff --git a/app/api/endpoints/history.py b/app/api/endpoints/history.py index 3338c524..5e86289f 100644 --- a/app/api/endpoints/history.py +++ b/app/api/endpoints/history.py @@ -1,4 +1,3 @@ -from pathlib import Path from typing import List, Any from fastapi import APIRouter, Depends @@ -6,7 +5,6 @@ from sqlalchemy.orm import Session from app import schemas from app.chain.storage import StorageChain -from app.core.config import settings from app.core.event import eventmanager from app.core.security import verify_token from app.db import get_db @@ -88,34 +86,16 @@ def delete_transfer_history(history_in: schemas.TransferHistory, # 册除媒体库文件 if deletedest and history.dest_fileitem: dest_fileitem = schemas.FileItem(**history.dest_fileitem) - state = StorageChain().delete_file(dest_fileitem) + state = StorageChain().delete_media_file(fileitem=dest_fileitem, mtype=MediaType(history.type)) if not state: - return schemas.Response(success=False, msg=f"{dest_fileitem.path}删除失败") - # 上级目录 - if history.type == MediaType.TV.value: - dir_path = Path(dest_fileitem.path).parent.parent - else: - dir_path = Path(dest_fileitem.path).parent - dir_item = StorageChain().get_file_item(storage=dest_fileitem.storage, path=dir_path) - if dir_item: - files = StorageChain().list_files(dir_item, recursion=True) - if files: - # 检查是否还有其他媒体文件 - media_file_exist = False - for file in files: - if file.extension and f".{file.extension.lower()}" in settings.RMT_MEDIAEXT: - media_file_exist = True - break - # 删除空目录 - if not media_file_exist: - StorageChain().delete_file(dir_item) + return schemas.Response(success=False, msg=f"{dest_fileitem.path} 删除失败") # 删除源文件 if deletesrc and history.src_fileitem: src_fileitem = schemas.FileItem(**history.src_fileitem) - state = StorageChain().delete_file(src_fileitem) + state = StorageChain().delete_media_file(src_fileitem) if not state: - return schemas.Response(success=False, msg=f"{src_fileitem.path}删除失败") + return schemas.Response(success=False, msg=f"{src_fileitem.path} 删除失败") # 发送事件 eventmanager.send_event( EventType.DownloadFileDeleted, diff --git a/app/api/endpoints/transfer.py b/app/api/endpoints/transfer.py index 1530ebe9..0300d82a 100644 --- a/app/api/endpoints/transfer.py +++ b/app/api/endpoints/transfer.py @@ -96,7 +96,9 @@ def manual_transfer(transer_item: ManualTransferItem, if history.dest_fileitem: # 删除旧的已整理文件 dest_fileitem = FileItem(**history.dest_fileitem) - StorageChain().delete_file(dest_fileitem) + state = StorageChain().delete_media_file(dest_fileitem, mtype=MediaType(history.type)) + if not state: + return schemas.Response(success=False, msg=f"{dest_fileitem.path} 删除失败") # 从历史数据获取信息 if transer_item.from_history: diff --git a/app/chain/storage.py b/app/chain/storage.py index f7515ec9..bf60cd55 100644 --- a/app/chain/storage.py +++ b/app/chain/storage.py @@ -3,6 +3,9 @@ from typing import Optional, Tuple, List, Dict from app import schemas from app.chain import ChainBase +from app.core.config import settings +from app.log import logger +from app.schemas import MediaType class StorageChain(ChainBase): @@ -74,6 +77,12 @@ class StorageChain(ChainBase): """ return self.run_module("get_file_item", storage=storage, path=path) + def get_parent_item(self, fileitem: schemas.FileItem) -> Optional[schemas.FileItem]: + """ + 获取上级目录项 + """ + return self.run_module("get_parent_item", fileitem=fileitem) + def snapshot_storage(self, storage: str, path: Path) -> Optional[Dict[str, float]]: """ 快照存储 @@ -91,3 +100,31 @@ class StorageChain(ChainBase): 获取支持的整理方式 """ return self.run_module("support_transtype", storage=storage) + + def delete_media_file(self, fileitem: schemas.FileItem, mtype: MediaType = None) -> bool: + """ + 删除媒体文件,以及不含媒体文件的目录 + """ + state = self.delete_file(fileitem) + if not state: + logger.warn(f"【{fileitem.storage}】{fileitem.path} 删除失败") + return False + # 上级目录 + if mtype and mtype == MediaType.TV: + dir_path = Path(fileitem.path).parent.parent + dir_item = self.get_file_item(storage=fileitem.storage, path=dir_path) + else: + dir_item = self.get_parent_item(fileitem) + if dir_item: + files = self.list_files(dir_item, recursion=True) + if files: + # 检查是否还有其他媒体文件 + media_file_exist = False + for file in files: + if file.extension and f".{file.extension.lower()}" in settings.RMT_MEDIAEXT: + media_file_exist = True + break + # 删除空目录 + if not media_file_exist: + self.delete_file(dir_item) + return False diff --git a/app/modules/filemanager/__init__.py b/app/modules/filemanager/__init__.py index 4f099087..4b80f269 100644 --- a/app/modules/filemanager/__init__.py +++ b/app/modules/filemanager/__init__.py @@ -260,6 +260,16 @@ class FileManagerModule(_ModuleBase): return None return storage_oper.get_item(path) + def get_parent_item(self, fileitem: FileItem) -> Optional[FileItem]: + """ + 获取上级目录项 + """ + storage_oper = self.__get_storage_oper(fileitem.storage) + if not storage_oper: + logger.error(f"不支持 {fileitem.storage} 的文件获取") + return None + return storage_oper.get_parent(fileitem) + def snapshot_storage(self, storage: str, path: Path) -> Optional[Dict[str, float]]: """ 快照存储