From 5f13824aa6712e7667002e61f736618e1c036045 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Sun, 26 May 2024 08:39:59 +0800 Subject: [PATCH] =?UTF-8?q?fix=20#2184=20=E6=89=8B=E5=8A=A8=E9=80=89?= =?UTF-8?q?=E6=8B=A9=E4=B8=8B=E8=BD=BD/=E5=AA=92=E4=BD=93=E5=BA=93?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E6=97=B6=E5=B0=9D=E8=AF=95=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E5=AF=B9=E5=BA=94=E7=9A=84=E9=85=8D=E7=BD=AE=E5=B9=B6=E5=81=9A?= =?UTF-8?q?=E5=88=86=E7=B1=BB=EF=BC=88=E5=A6=82=E6=9E=9C=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E4=B8=8D=E5=88=B0=E5=88=99=E4=B8=8D=E8=87=AA=E5=8A=A8=E5=88=86?= =?UTF-8?q?=E7=B1=BB=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/chain/download.py | 23 ++++++++----- app/helper/directory.py | 13 ++++++-- app/modules/filetransfer/__init__.py | 49 ++++++++++++++++------------ 3 files changed, 53 insertions(+), 32 deletions(-) diff --git a/app/chain/download.py b/app/chain/download.py index 126300ca..bd5e79cb 100644 --- a/app/chain/download.py +++ b/app/chain/download.py @@ -230,15 +230,14 @@ class DownloadChain(ChainBase): _folder_name, _file_list = self.torrent.get_torrent_info(torrent_file) # 下载目录 - if not save_path: - # 获取下载目录 + if save_path: + # 有自定义下载目录时,尝试匹配目录配置 + dir_info = self.directoryhelper.get_download_dir(_media, to_path=Path(save_path)) + else: + # 根据媒体信息查询下载目录配置 dir_info = self.directoryhelper.get_download_dir(_media) - if not dir_info: - logger.error(f"未找到下载目录:{_media.type.value} {_media.title_year}") - self.messagehelper.put(f"{_media.type.value} {_media.title_year} 未找到下载目录!", - title="下载失败", role="system") - return None - + # 拼装子目录 + if dir_info: # 一级目录 if not dir_info.media_type and dir_info.auto_category: # 一级自动分类 @@ -251,9 +250,15 @@ class DownloadChain(ChainBase): if not dir_info.category and dir_info.auto_category and _media and _media.category: # 二级自动分类 download_dir = download_dir / _media.category - else: + elif save_path: # 自定义下载目录 download_dir = Path(save_path) + else: + # 未找到下载目录,且没有自定义下载目录 + logger.error(f"未找到下载目录:{_media.type.value} {_media.title_year}") + self.messagehelper.put(f"{_media.type.value} {_media.title_year} 未找到下载目录!", + title="下载失败", role="system") + return None # 添加下载 result: Optional[tuple] = self.download(content=content, diff --git a/app/helper/directory.py b/app/helper/directory.py index a90c1641..2511294e 100644 --- a/app/helper/directory.py +++ b/app/helper/directory.py @@ -35,10 +35,11 @@ class DirectoryHelper: return [] return [schemas.MediaDirectory(**d) for d in dir_conf] - def get_download_dir(self, media: MediaInfo = None) -> Optional[schemas.MediaDirectory]: + def get_download_dir(self, media: MediaInfo = None, to_path: Path = None) -> Optional[schemas.MediaDirectory]: """ 根据媒体信息获取下载目录 :param media: 媒体信息 + :param to_path: 目标目录 """ # 处理类型 if media: @@ -50,6 +51,9 @@ class DirectoryHelper: for media_dir in media_dirs: if not media_dir.path: continue + # 有目标目录,但目标目录与当前目录不相等时不要 + if to_path and Path(media_dir.path) != to_path: + continue # 目录类型为全部的,符合条件 if not media_dir.media_type: return media_dir @@ -62,11 +66,13 @@ class DirectoryHelper: return None - def get_library_dir(self, media: MediaInfo = None, in_path: Path = None) -> Optional[schemas.MediaDirectory]: + def get_library_dir(self, media: MediaInfo = None, in_path: Path = None, + to_path: Path = None) -> Optional[schemas.MediaDirectory]: """ 根据媒体信息获取媒体库目录,需判断是否同盘优先 :param media: 媒体信息 :param in_path: 源目录 + :param to_path: 目标目录 """ # 处理类型 if media: @@ -80,6 +86,9 @@ class DirectoryHelper: for library_dir in library_dirs: if not library_dir.path: continue + # 有目标目录,但目标目录与当前目录不相等时不要 + if to_path and Path(library_dir.path) != to_path: + continue # 目录类型为全部的,符合条件 if not library_dir.media_type: matched_dirs.append(library_dir) diff --git a/app/modules/filetransfer/__init__.py b/app/modules/filetransfer/__init__.py index bcc5768e..d1d9caea 100644 --- a/app/modules/filetransfer/__init__.py +++ b/app/modules/filetransfer/__init__.py @@ -95,30 +95,36 @@ class FileTransferModule(_ModuleBase): :param scrape: 是否刮削元数据 :return: {path, target_path, message} """ + # 目标路径不能是文件 + if target and target.is_file(): + logger.error(f"转移目标路径是一个文件 {target} 是一个文件") + return TransferInfo(success=False, + path=path, + message=f"{target} 不是有效目录") # 获取目标路径 - if not target: - # 未指定目的目录,选择一个媒体库 - dir_info = DirectoryHelper().get_library_dir(mediainfo, in_path=path) - if not dir_info: - logger.error(f"{mediainfo.type.value} {mediainfo.title_year} 未找到有效的媒体库目录,无法转移文件,源路径:{path}") - return TransferInfo(success=False, - path=path, - message="未找到有效的媒体库目录") - # 拼装媒体库一、二级子目录 + directoryhelper = DirectoryHelper() + if target: + dir_info = directoryhelper.get_library_dir(mediainfo, in_path=path, to_path=target) + else: + dir_info = directoryhelper.get_library_dir(mediainfo, in_path=path) + if dir_info: + # 是否需要刮削 if scrape is None: need_scrape = dir_info.scrape else: need_scrape = scrape + # 拼装媒体库一、二级子目录 target = self.__get_dest_dir(mediainfo=mediainfo, target_dir=dir_info) - else: - # 指定了目的目录 - if target.is_file(): - logger.error(f"转移目标路径是一个文件 {target} 是一个文件") - return TransferInfo(success=False, - path=path, - message=f"{target} 不是有效目录") - # FIXME 指定了目的目录时,拿不到是否需要刮削的配置了 + elif target: + # 自定义目标路径 need_scrape = False + else: + # 未找到有效的媒体库目录 + logger.error( + f"{mediainfo.type.value} {mediainfo.title_year} 未找到有效的媒体库目录,无法转移文件,源路径:{path}") + return TransferInfo(success=False, + path=path, + message="未找到有效的媒体库目录") logger.info(f"获取转移目标路径:{target}") # 转移 @@ -701,21 +707,22 @@ class FileTransferModule(_ModuleBase): dest_paths = DirectoryHelper().get_library_dirs() # 检查每一个媒体库目录 for dest_path in dest_paths: - # 媒体库路径 - if not dest_path.path: - continue # 媒体分类路径 target_dir = self.__get_dest_dir(mediainfo=mediainfo, target_dir=dest_path) + if not target_dir.exists(): + continue + # 重命名格式 rename_format = settings.TV_RENAME_FORMAT \ if mediainfo.type == MediaType.TV else settings.MOVIE_RENAME_FORMAT - # 相对路径 + # 获取相对路径(重命名路径) meta = MetaInfo(mediainfo.title) rel_path = self.get_rename_path( template_string=rename_format, rename_dict=self.__get_naming_dict(meta=meta, mediainfo=mediainfo) ) + # 取相对路径的第1层目录 if rel_path.parts: media_path = target_dir / rel_path.parts[0]