diff --git a/app/chain/storage.py b/app/chain/storage.py index 4f77b884..49b39366 100644 --- a/app/chain/storage.py +++ b/app/chain/storage.py @@ -57,13 +57,14 @@ class StorageChain(ChainBase): """ return self.run_module("download_file", fileitem=fileitem, path=path) - def upload_file(self, fileitem: schemas.FileItem, path: Path) -> Optional[bool]: + def upload_file(self, fileitem: schemas.FileItem, path: Path, new_name: str = None) -> Optional[bool]: """ 上传文件 :param fileitem: 保存目录项 :param path: 本地文件路径 + :param new_name: 新文件名 """ - return self.run_module("upload_file", fileitem=fileitem, path=path) + return self.run_module("upload_file", fileitem=fileitem, path=path, new_name=new_name) def delete_file(self, fileitem: schemas.FileItem) -> Optional[bool]: """ diff --git a/app/modules/filemanager/__init__.py b/app/modules/filemanager/__init__.py index e0d98a7b..a0349d2f 100644 --- a/app/modules/filemanager/__init__.py +++ b/app/modules/filemanager/__init__.py @@ -268,7 +268,7 @@ class FileManagerModule(_ModuleBase): return None return storage_oper.download(fileitem, path=path) - def upload_file(self, fileitem: FileItem, path: Path) -> Optional[FileItem]: + def upload_file(self, fileitem: FileItem, path: Path, new_name: str = None) -> Optional[FileItem]: """ 上传文件 """ @@ -276,7 +276,7 @@ class FileManagerModule(_ModuleBase): if not storage_oper: logger.error(f"不支持 {fileitem.storage} 的上传处理") return None - return storage_oper.upload(fileitem, path) + return storage_oper.upload(fileitem, path, new_name) def get_file_item(self, storage: str, path: Path) -> Optional[FileItem]: """ @@ -487,13 +487,8 @@ class FileManagerModule(_ModuleBase): target_fileitem = target_oper.get_folder(target_file.parent) if target_fileitem: # 上传文件 - new_item = target_oper.upload(target_fileitem, filepath) + new_item = target_oper.upload(target_fileitem, filepath, target_file.name) if new_item: - # 重命名为目标文件名 - if new_item.name != target_file.name: - if target_oper.rename(new_item, target_file.name): - new_item.name = target_file.name - new_item.path = str(Path(new_item.path).parent / target_file.name) return new_item, "" else: return None, f"{fileitem.path} 上传 {target_storage} 失败" @@ -505,13 +500,8 @@ class FileManagerModule(_ModuleBase): target_fileitem = target_oper.get_folder(target_file.parent) if target_fileitem: # 上传文件 - new_item = target_oper.upload(target_fileitem, filepath) + new_item = target_oper.upload(target_fileitem, filepath, target_file.name) if new_item: - # 重命名为目标文件名 - if new_item.name != target_file.name: - if target_oper.rename(new_item, target_file.name): - new_item.name = target_file.name - new_item.path = str(Path(new_item.path).parent / target_file.name) # 删除源文件 source_oper.delete(fileitem) return new_item, "" diff --git a/app/modules/filemanager/storages/__init__.py b/app/modules/filemanager/storages/__init__.py index d55b19ff..7c52b61a 100644 --- a/app/modules/filemanager/storages/__init__.py +++ b/app/modules/filemanager/storages/__init__.py @@ -119,11 +119,12 @@ class StorageBase(metaclass=ABCMeta): pass @abstractmethod - def upload(self, fileitem: schemas.FileItem, path: Path) -> Optional[schemas.FileItem]: + def upload(self, fileitem: schemas.FileItem, path: Path, new_name: str = None) -> Optional[schemas.FileItem]: """ 上传文件 :param fileitem: 上传目录项 :param path: 本地文件路径 + :param new_name: 上传后文件名 """ pass diff --git a/app/modules/filemanager/storages/alipan.py b/app/modules/filemanager/storages/alipan.py index 236610f2..5fd67db1 100644 --- a/app/modules/filemanager/storages/alipan.py +++ b/app/modules/filemanager/storages/alipan.py @@ -353,7 +353,7 @@ class AliPan(StorageBase): return Path(local_path) return None - def upload(self, fileitem: schemas.FileItem, path: Path) -> Optional[schemas.FileItem]: + def upload(self, fileitem: schemas.FileItem, path: Path, new_name: str = None) -> Optional[schemas.FileItem]: """ 上传文件,并标记完成 """ @@ -361,7 +361,7 @@ class AliPan(StorageBase): return None # 上传文件 result = self.aligo.upload_file(file_path=str(path), parent_file_id=fileitem.fileid, - drive_id=fileitem.drive_id, name=path.name, + drive_id=fileitem.drive_id, name=new_name or path.name, check_name_mode="refuse") if result: item = self.aligo.get_file(file_id=result.file_id, drive_id=result.drive_id) diff --git a/app/modules/filemanager/storages/alist.py b/app/modules/filemanager/storages/alist.py index 9ea40835..6072ce23 100644 --- a/app/modules/filemanager/storages/alist.py +++ b/app/modules/filemanager/storages/alist.py @@ -534,12 +534,13 @@ class Alist(StorageBase): return None def upload( - self, fileitem: schemas.FileItem, path: Path, task: bool = False + self, fileitem: schemas.FileItem, path: Path, new_name: str = None, task: bool = False ) -> Optional[schemas.FileItem]: """ 上传文件 :param fileitem: 上传目录项 :param path: 本地文件路径 + :param new_name: 上传后文件名 :param task: 是否为任务,默认为False避免未完成上传时对文件进行操作 """ encoded_path = UrlUtils.quote(fileitem.path) @@ -557,7 +558,11 @@ class Alist(StorageBase): logging.warning(f"请求上传文件 {path} 失败,状态码:{resp.status_code}") return - return fileitem + if new_name and new_name != path.name: + if self.rename(fileitem, new_name): + return self.get_item(Path(fileitem.path).parent / new_name) + + return self.get_item(Path(fileitem.path) / path.name) def detail(self, fileitem: schemas.FileItem) -> Optional[schemas.FileItem]: """ diff --git a/app/modules/filemanager/storages/local.py b/app/modules/filemanager/storages/local.py index 5ea5c564..a7e26077 100644 --- a/app/modules/filemanager/storages/local.py +++ b/app/modules/filemanager/storages/local.py @@ -183,12 +183,12 @@ class LocalStorage(StorageBase): """ return Path(fileitem.path) - def upload(self, fileitem: schemas.FileItem, path: Path) -> Optional[schemas.FileItem]: + def upload(self, fileitem: schemas.FileItem, path: Path, new_name: str = None) -> Optional[schemas.FileItem]: """ 上传文件 """ dir_path = Path(fileitem.path) - target_path = dir_path / path.name + target_path = dir_path / (new_name or path.name) code, message = SystemUtils.move(path, target_path) if code != 0: logger.error(f"移动文件失败:{message}") diff --git a/app/modules/filemanager/storages/rclone.py b/app/modules/filemanager/storages/rclone.py index 1f62c19e..e582efb6 100644 --- a/app/modules/filemanager/storages/rclone.py +++ b/app/modules/filemanager/storages/rclone.py @@ -260,7 +260,7 @@ class Rclone(StorageBase): logger.error(f"rclone复制文件失败:{err}") return None - def upload(self, fileitem: schemas.FileItem, path: Path) -> Optional[schemas.FileItem]: + def upload(self, fileitem: schemas.FileItem, path: Path, new_name: str = None) -> Optional[schemas.FileItem]: """ 上传文件 """ @@ -269,7 +269,7 @@ class Rclone(StorageBase): [ 'rclone', 'copyto', str(path), - f'MP:{Path(fileitem.path) / path.name}' + f'MP:{Path(fileitem.path) / (new_name or path.name)}' ], startupinfo=self.__get_hidden_shell() ).returncode diff --git a/app/modules/filemanager/storages/u115.py b/app/modules/filemanager/storages/u115.py index d5f023db..43c628d0 100644 --- a/app/modules/filemanager/storages/u115.py +++ b/app/modules/filemanager/storages/u115.py @@ -328,7 +328,7 @@ class U115Pan(StorageBase, metaclass=Singleton): logger.error(f"115下载失败:{str(e)}") return None - def upload(self, fileitem: schemas.FileItem, path: Path) -> Optional[schemas.FileItem]: + def upload(self, fileitem: schemas.FileItem, path: Path, new_name: str = None) -> Optional[schemas.FileItem]: """ 上传文件 """ @@ -359,7 +359,7 @@ class U115Pan(StorageBase, metaclass=Singleton): if result: result_data = result.get('data') logger.info(f"115上传文件成功:{result_data.get('file_name')}") - return schemas.FileItem( + item = schemas.FileItem( storage=self.schema.value, fileid=result_data.get('file_id'), parent_fileid=fileitem.fileid, @@ -371,6 +371,13 @@ class U115Pan(StorageBase, metaclass=Singleton): extension=Path(result_data.get('file_name')).suffix[1:], pickcode=result_data.get('pickcode') ) + if new_name and new_name != item.name: + if self.rename(item, new_name): + item.name = new_name + item.basename = Path(new_name).stem + item.path = f"{fileitem.path}{new_name}" + item.extension = Path(new_name).suffix[1:] + return item else: logger.warn(f"115上传文件失败:{por.resp.response.text}") return None