From a6f07920142bfbb5d0c0b9f5ffd60cdecc53b19e Mon Sep 17 00:00:00 2001 From: jxxghp Date: Wed, 23 Oct 2024 17:47:01 +0800 Subject: [PATCH] refactor: get_parent --- app/modules/filemanager/__init__.py | 17 ++++++----------- app/modules/filemanager/storages/__init__.py | 6 ++++++ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/app/modules/filemanager/__init__.py b/app/modules/filemanager/__init__.py index 3c833d9cd..efd6ee9a6 100644 --- a/app/modules/filemanager/__init__.py +++ b/app/modules/filemanager/__init__.py @@ -565,17 +565,14 @@ def __transfer_subtitles(self, fileitem: FileItem, target_storage: str, target_f if not storage_oper: logger.error(f"不支持 {fileitem.storage} 的文件整理") return False, f"不支持的文件存储:{fileitem.storage}" - - # 上级目录 - org_dir: Path = org_path.parent # 查找上级文件项 - parent_item: FileItem = storage_oper.get_folder(org_dir) + parent_item: FileItem = storage_oper.get_parent(fileitem) if not parent_item: - return False, f"{org_dir} 目录获取失败" + return False, f"{org_path} 上级目录获取失败" # 字幕文件列表 file_list: List[FileItem] = storage_oper.list(parent_item) if len(file_list) == 0: - logger.debug(f"{org_dir} 目录下没有找到字幕文件...") + logger.debug(f"{parent_item.path} 目录下没有找到字幕文件...") else: logger.debug("字幕文件清单:" + str(file_list)) # 识别文件名 @@ -662,18 +659,16 @@ def __transfer_audio_track_files(self, fileitem: FileItem, target_storage: str, if not storage_oper: logger.error(f"不支持 {fileitem.storage} 的文件整理") return False, f"不支持的文件存储:{fileitem.storage}" - # 上级目录 - org_dir = org_path.parent # 查找上级文件项 - parent_item: FileItem = storage_oper.get_folder(org_dir) + parent_item: FileItem = storage_oper.get_parent(fileitem) if not parent_item: - return False, f"{org_dir} 目录获取失败" + return False, f"{org_path} 上级目录获取失败" file_list: List[FileItem] = storage_oper.list(parent_item) # 匹配音轨文件 pending_file_list: List[FileItem] = [file for file in file_list if Path(file.name).stem == org_path.name and f".{file.extension.lower()}" in settings.RMT_AUDIOEXT] if len(pending_file_list) == 0: - return True, f"{org_dir} 目录下没有找到匹配的音轨文件" + return True, f"{parent_item.path} 目录下没有找到匹配的音轨文件" logger.debug("音轨文件清单:" + str(pending_file_list)) for track_file in pending_file_list: track_ext = f".{track_file.extension}" diff --git a/app/modules/filemanager/storages/__init__.py b/app/modules/filemanager/storages/__init__.py index f66d1a9f2..60dc44afb 100644 --- a/app/modules/filemanager/storages/__init__.py +++ b/app/modules/filemanager/storages/__init__.py @@ -81,6 +81,12 @@ def get_item(self, path: Path) -> Optional[schemas.FileItem]: """ pass + def get_parent(self, fileitem: schemas.FileItem) -> Optional[schemas.FileItem]: + """ + 获取父目录 + """ + return self.get_folder(Path(fileitem.path).parent) + @abstractmethod def delete(self, fileitem: schemas.FileItem) -> bool: """