diff --git a/app/chain/media.py b/app/chain/media.py index e6d95a77..2ddd6ae7 100644 --- a/app/chain/media.py +++ b/app/chain/media.py @@ -347,7 +347,8 @@ class MediaChain(ChainBase, metaclass=Singleton): self.scrape_metadata(fileitem=fileitem, meta=meta, mediainfo=mediainfo) def scrape_metadata(self, fileitem: schemas.FileItem, - meta: MetaBase = None, mediainfo: MediaInfo = None, init_folder: bool = True): + meta: MetaBase = None, mediainfo: MediaInfo = None, + init_folder: bool = True, parent: schemas.FileItem = None): """ 手动刮削媒体信息 """ @@ -376,7 +377,7 @@ class MediaChain(ChainBase, metaclass=Singleton): upload_item.name = _path.name upload_item.basename = _path.stem upload_item.extension = _path.suffix - logger.info(f"保存文件:{_path}") + logger.info(f"保存文件:【{_fileitem.storage}】{_path}") StorageChain().upload_file(fileitem=upload_item, path=tmp_file) if tmp_file.exists(): tmp_file.unlink() @@ -417,15 +418,15 @@ class MediaChain(ChainBase, metaclass=Singleton): if not movie_nfo: logger.warn(f"{filepath.name} nfo文件生成失败!") return - # 保存或上传nfo文件 - __save_file(_fileitem=fileitem, _path=filepath.with_suffix(".nfo"), _content=movie_nfo) + # 保存或上传nfo文件到上级目录 + __save_file(_fileitem=parent, _path=filepath.with_suffix(".nfo"), _content=movie_nfo) else: # 电影目录 files = __list_files(_fileitem=fileitem) for file in files: self.scrape_metadata(fileitem=file, meta=meta, mediainfo=mediainfo, - init_folder=False) + init_folder=False, parent=fileitem) # 生成目录内图片文件 if init_folder: # 图片 @@ -439,7 +440,7 @@ class MediaChain(ChainBase, metaclass=Singleton): image_path = filepath / image_name # 下载图片 content = __download_image(_url=attr_value) - # 写入图片到根目录 + # 写入图片到当前目录 __save_file(_fileitem=fileitem, _path=image_path, _content=content) else: # 电视剧 @@ -459,8 +460,8 @@ class MediaChain(ChainBase, metaclass=Singleton): if not episode_nfo: logger.warn(f"{filepath.name} nfo生成失败!") return - # 保存或上传nfo文件 - __save_file(_fileitem=fileitem, _path=filepath.with_suffix(".nfo"), _content=episode_nfo) + # 保存或上传nfo文件到上级目录 + __save_file(_fileitem=parent, _path=filepath.with_suffix(".nfo"), _content=episode_nfo) # 获取集的图片 image_dict = self.metadata_img(mediainfo=file_mediainfo, season=file_meta.begin_season, episode=file_meta.begin_episode) @@ -478,6 +479,7 @@ class MediaChain(ChainBase, metaclass=Singleton): for file in files: self.scrape_metadata(fileitem=file, meta=meta, mediainfo=mediainfo, + parent=fileitem if file.type == "file" else None, init_folder=True if file.type == "dir" else False) # 生成目录的nfo和图片 if init_folder: diff --git a/app/chain/storage.py b/app/chain/storage.py index 6244a0e6..f238ebcb 100644 --- a/app/chain/storage.py +++ b/app/chain/storage.py @@ -43,12 +43,16 @@ class StorageChain(ChainBase): def download_file(self, fileitem: schemas.FileItem, path: Path = None) -> Optional[Path]: """ 下载文件 + :param fileitem: 文件项 + :param path: 本地保存路径 """ return self.run_module("download_file", fileitem=fileitem, path=path) def upload_file(self, fileitem: schemas.FileItem, path: Path) -> Optional[bool]: """ 上传文件 + :param fileitem: 保存目录项 + :param path: 本地文件路径 """ return self.run_module("upload_file", fileitem=fileitem, path=path) diff --git a/app/modules/filemanager/storages/__init__.py b/app/modules/filemanager/storages/__init__.py index 2b045273..f66d1a9f 100644 --- a/app/modules/filemanager/storages/__init__.py +++ b/app/modules/filemanager/storages/__init__.py @@ -99,6 +99,9 @@ class StorageBase(metaclass=ABCMeta): def download(self, fileitem: schemas.FileItem, path: Path = None) -> Path: """ 下载文件,保存到本地,返回本地临时文件地址 + :param fileitem: 文件项 + :param path: 文件保存路径 + """ pass @@ -106,6 +109,8 @@ class StorageBase(metaclass=ABCMeta): def upload(self, fileitem: schemas.FileItem, path: Path) -> Optional[schemas.FileItem]: """ 上传文件 + :param fileitem: 上传目录项 + :param path: 本地文件路径 """ pass diff --git a/app/modules/filemanager/storages/alipan.py b/app/modules/filemanager/storages/alipan.py index 7567ff8f..40bcefcf 100644 --- a/app/modules/filemanager/storages/alipan.py +++ b/app/modules/filemanager/storages/alipan.py @@ -256,7 +256,7 @@ class AliPan(StorageBase): else: items = self.aligo.get_file_list(parent_file_id=fileitem.fileid, drive_id=fileitem.drive_id) if items: - return [self.__get_fileitem(item) for item in items] + return [self.__get_fileitem(item, parent=fileitem.path) for item in items] return [] def create_folder(self, fileitem: schemas.FileItem, name: str) -> Optional[schemas.FileItem]: @@ -341,10 +341,14 @@ class AliPan(StorageBase): """ if not self.aligo: return None - item = self.aligo.upload_file(file_path=str(path.parent), parent_file_id=fileitem.fileid, - drive_id=fileitem.drive_id, name=path.name) - if item: - return self.__get_fileitem(item) + # 上传文件 + result = self.aligo.upload_file(file_path=str(path), parent_file_id=fileitem.fileid, + drive_id=fileitem.drive_id, name=path.name, + check_name_mode="refuse") + if result: + item = self.aligo.get_file(file_id=result.file_id, drive_id=result.drive_id) + if item: + return self.__get_fileitem(item) return None def move(self, fileitem: schemas.FileItem, target: schemas.FileItem) -> bool: diff --git a/app/modules/filemanager/storages/local.py b/app/modules/filemanager/storages/local.py index e188e294..19d2976d 100644 --- a/app/modules/filemanager/storages/local.py +++ b/app/modules/filemanager/storages/local.py @@ -187,12 +187,13 @@ class LocalStorage(StorageBase): """ 上传文件 """ - file_path = Path(fileitem.path) - code, message = SystemUtils.move(path, file_path) + dir_path = Path(fileitem.path) + target_path = dir_path / path.name + code, message = SystemUtils.move(path, target_path) if code != 0: logger.error(f"移动文件失败:{message}") return None - return self.__get_diritem(file_path) + return self.__get_diritem(target_path) def copy(self, fileitem: schemas.FileItem, target_file: Path) -> bool: """