diff --git a/app/chain/transfer.py b/app/chain/transfer.py index f5f6c4df..2baad3e7 100644 --- a/app/chain/transfer.py +++ b/app/chain/transfer.py @@ -120,7 +120,7 @@ class TransferChain(ChainBase): # 非MoviePilot下载的任务,按文件识别 mediainfo = None - # 执行整理 + # 执行整理,匹配源目录 state, errmsg = self.__do_transfer( fileitem=FileItem( storage="local", @@ -131,7 +131,8 @@ class TransferChain(ChainBase): extension=file_path.suffix.lstrip('.'), ), mediainfo=mediainfo, - download_hash=torrent.hash + download_hash=torrent.hash, + src_match=True ) # 设置下载任务状态 @@ -148,7 +149,8 @@ class TransferChain(ChainBase): target_storage: str = None, target_path: Path = None, transfer_type: str = None, scrape: bool = None, season: int = None, epformat: EpisodeFormat = None, - min_filesize: int = 0, download_hash: str = None, force: bool = False) -> Tuple[bool, str]: + min_filesize: int = 0, download_hash: str = None, + force: bool = False, src_match: bool = False) -> Tuple[bool, str]: """ 执行一个复杂目录的整理操作 :param fileitem: 文件项 @@ -164,6 +166,7 @@ class TransferChain(ChainBase): :param min_filesize: 最小文件大小(MB) :param download_hash: 下载记录hash :param force: 是否强制整理 + :param src_match: 是否源目录匹配 返回:成功标识,错误信息 """ @@ -379,6 +382,15 @@ class TransferChain(ChainBase): if download_file: download_hash = download_file.download_hash + # 查询整理目标目录 + if not target_directory: + if target_path: + target_directory = self.directoryhelper.get_dir(mediainfo, dest_path=target_path) + elif src_match: + target_directory = self.directoryhelper.get_dir(mediainfo, src_path=file_path) + else: + target_directory = self.directoryhelper.get_dir(mediainfo) + # 执行整理 transferinfo: TransferInfo = self.transfer(fileitem=file_item, meta=file_meta, diff --git a/app/helper/directory.py b/app/helper/directory.py index 00570b82..2d59f848 100644 --- a/app/helper/directory.py +++ b/app/helper/directory.py @@ -4,7 +4,7 @@ from typing import List, Optional from app import schemas from app.core.context import MediaInfo from app.db.systemconfig_oper import SystemConfigOper -from app.schemas.types import SystemConfigKey, MediaType +from app.schemas.types import SystemConfigKey class DirectoryHelper: @@ -59,10 +59,10 @@ class DirectoryHelper: :param local: 是否本地目录 """ # 处理类型 - if media: - media_type = media.type.value - else: - media_type = MediaType.UNKNOWN.value + if not media: + return None + # 电影/电视剧 + media_type = media.type.value dirs = self.get_dirs() # 按照配置顺序查找 for d in dirs: @@ -70,16 +70,15 @@ class DirectoryHelper: download_path = Path(d.download_path) # 媒体库目录 library_path = Path(d.library_path) - # 下载目录不匹配, 不符合条件, 通常处理`下载`匹配 - if src_path and download_path != src_path: + # 有源目录时,源目录不匹配下载目录 + if src_path and not src_path.is_relative_to(download_path): continue - # 媒体库目录不匹配, 或监控方式为None(即不自动整理), 不符合条件, 通常处理`整理`匹配 - if dest_path: - if library_path != dest_path or not d.monitor_type: - continue - # 没有目录配置时起作用, 通常处理`手动整理`未选择`目标目录`的情况 + # 有文件项时,文件项不匹配下载目录 if fileitem and not Path(fileitem.path).is_relative_to(download_path): continue + # 有目标目录时,目标目录不匹配媒体库目录 + if dest_path and not dest_path.is_relative_to(library_path): + continue # 本地目录 if local and d.storage != "local": continue diff --git a/app/modules/filemanager/__init__.py b/app/modules/filemanager/__init__.py index 469b70b2..ea551880 100644 --- a/app/modules/filemanager/__init__.py +++ b/app/modules/filemanager/__init__.py @@ -65,9 +65,8 @@ class FileManagerModule(_ModuleBase): """ 测试模块连接性 """ - directoryhelper = DirectoryHelper() # 检查目录 - dirs = directoryhelper.get_dirs() + dirs = self.directoryhelper.get_dirs() if not dirs: return False, "未设置任何目录" for d in dirs: @@ -349,14 +348,6 @@ class FileManagerModule(_ModuleBase): fileitem=fileitem, message=f"{target_path} 不是有效目录") # 获取目标路径 - directoryhelper = DirectoryHelper() - if not target_directory: - # 根据目的路径查找目录配置 - if target_path: - target_directory = directoryhelper.get_dir(mediainfo, dest_path=target_path) - else: - target_directory = directoryhelper.get_dir(mediainfo, fileitem=fileitem) - if target_directory: # 拼装媒体库一、二级子目录 target_path = self.__get_dest_dir(mediainfo=mediainfo, target_dir=target_directory) @@ -378,7 +369,7 @@ class FileManagerModule(_ModuleBase): # 覆盖模式 overwrite_mode = target_directory.overwrite_mode elif target_path: - # 自定义目标路径,仅适用于手动整理的场景 + # 手动整理的场景,有自定义目标路径 need_scrape = scrape or False need_rename = True need_notify = False diff --git a/app/monitor.py b/app/monitor.py index b4fe221c..e61673c4 100644 --- a/app/monitor.py +++ b/app/monitor.py @@ -263,19 +263,17 @@ class Monitor(metaclass=Singleton): try: item = self._queue.get(timeout=self._transfer_interval) if item: - self.__handle_file(storage=item.get("storage"), - event_path=item.get("filepath"), - mon_path=item.get("mon_path")) + self.__handle_file(storage=item.get("storage"), event_path=item.get("filepath")) except queue.Empty: continue except Exception as e: logger.error(f"整理队列处理出现错误:{e}") - def __handle_file(self, storage: str, event_path: Path, mon_path: Path): + def __handle_file(self, storage: str, event_path: Path): """ 整理一个文件 + :param storage: 存储 :param event_path: 事件文件路径 - :param mon_path: 监控目录 """ def __get_bluray_dir(_path: Path): @@ -386,7 +384,7 @@ class Monitor(metaclass=Singleton): return # 查询转移目的目录 - dir_info = self.directoryhelper.get_dir(mediainfo, src_path=mon_path) + dir_info = self.directoryhelper.get_dir(mediainfo, src_path=event_path) if not dir_info: logger.warn(f"{event_path.name} 未找到对应的目标目录") return