diff --git a/app/modules/filemanager/__init__.py b/app/modules/filemanager/__init__.py index d42c0ec5..51f36dc8 100644 --- a/app/modules/filemanager/__init__.py +++ b/app/modules/filemanager/__init__.py @@ -256,13 +256,13 @@ class FileManagerModule(_ModuleBase): :param transfer_type: 整理方式 """ - def __get_fileitem(_path: Path) -> FileItem: + def __get_targetitem(_path: Path) -> FileItem: """ 获取文件信息 """ return FileItem( storage=target_storage, - path=str(_path), + path=str(_path).replace("\\", "/"), name=_path.name, basename=_path.stem, type="file", @@ -296,7 +296,7 @@ class FileManagerModule(_ModuleBase): elif transfer_type == "softlink": state = source_oper.softlink(fileitem, target_file) if state: - return __get_fileitem(target_file), "" + return __get_targetitem(target_file), "" elif fileitem.storage == "local" and target_storage != "local": # 本地到网盘 filepath = Path(fileitem.path) @@ -327,18 +327,18 @@ class FileManagerModule(_ModuleBase): # 检查本地是否存在 if target_file.exists(): logger.warn(f"文件已存在:{target_file}") - return __get_fileitem(target_file), "" + return __get_targetitem(target_file), "" # 网盘到本地 if transfer_type == "copy": # 下载 if target_oper.download(fileitem, target_file): - return __get_fileitem(target_file), "" + return __get_targetitem(target_file), "" elif transfer_type == "move": # 下载 if target_oper.download(fileitem, target_file): # 删除源文件 source_oper.delete(fileitem) - return __get_fileitem(target_file), "" + return __get_targetitem(target_file), "" return None, "不支持的整理操作" @@ -658,8 +658,23 @@ class FileManagerModule(_ModuleBase): :param need_scrape: 是否需要刮削 :return: TransferInfo、错误信息 """ - # 检查目录路径 + def __get_targetitem(_path: Path) -> FileItem: + """ + 获取文件信息 + """ + return FileItem( + storage=target_storage, + path=str(_path).replace("\\", "/"), + name=_path.name, + basename=_path.stem, + type="file", + size=_path.stat().st_size, + extension=_path.suffix.lstrip('.'), + modify_time=_path.stat().st_mtime + ) + + # 检查目录路径 if fileitem.storage == "local" and not Path(fileitem.path).exists(): return TransferInfo(success=False, fileitem=fileitem, @@ -763,32 +778,14 @@ class FileManagerModule(_ModuleBase): return TransferInfo(success=False, message=f"媒体库中已存在,且质量更好", fileitem=fileitem, - target_fileitem=FileItem( - path=str(target_file), - storage=target_storage, - type="file", - size=target_file.stat().st_size, - name=target_file.name, - basename=target_file.stem, - extension=target_file.suffix[1:], - modify_time=target_file.stat().st_mtime - ), + target_fileitem=__get_targetitem(target_file), fail_list=[fileitem.path]) case 'never': # 存在不覆盖 return TransferInfo(success=False, message=f"媒体库中已存在,当前设置为不覆盖", fileitem=fileitem, - target_fileitem=FileItem( - path=str(target_file), - storage=target_storage, - type="file", - size=target_file.stat().st_size, - name=target_file.name, - basename=target_file.stem, - extension=target_file.suffix[1:], - modify_time=target_file.stat().st_mtime - ), + target_fileitem=__get_targetitem(target_file), fail_list=[fileitem.path]) case 'latest': # 仅保留最新版本 diff --git a/app/modules/filemanager/storage/local.py b/app/modules/filemanager/storage/local.py index aebfddb8..f93e36fc 100644 --- a/app/modules/filemanager/storage/local.py +++ b/app/modules/filemanager/storage/local.py @@ -30,6 +30,34 @@ class LocalStorage(StorageBase): """ return True + def __get_fileitem(self, path: Path): + """ + 获取文件项 + """ + return schemas.FileItem( + storage=self.schema.value, + type="file", + path=str(path).replace("\\", "/"), + name=path.name, + basename=path.stem, + extension=path.suffix[1:], + size=path.stat().st_size, + modify_time=path.stat().st_mtime, + ) + + def __get_diritem(self, path: Path): + """ + 获取目录项 + """ + return schemas.FileItem( + storage=self.schema.value, + type="dir", + path=str(path).replace("\\", "/") + "/", + name=path.name, + basename=path.stem, + modify_time=path.stat().st_mtime, + ) + def list(self, fileitem: schemas.FileItem) -> Optional[List[schemas.FileItem]]: """ 浏览文件 @@ -65,41 +93,16 @@ class LocalStorage(StorageBase): # 如果是文件 if path_obj.is_file(): - ret_items.append(schemas.FileItem( - storage=self.schema.value, - type="file", - path=str(path_obj).replace("\\", "/"), - name=path_obj.name, - basename=path_obj.stem, - extension=path_obj.suffix[1:], - size=path_obj.stat().st_size, - modify_time=path_obj.stat().st_mtime, - )) + ret_items.append(self.__get_fileitem(path_obj)) return ret_items # 扁历所有目录 for item in SystemUtils.list_sub_directory(path_obj): - ret_items.append(schemas.FileItem( - storage=self.schema.value, - type="dir", - path=str(item).replace("\\", "/") + "/", - name=item.name, - basename=item.stem, - modify_time=item.stat().st_mtime, - )) + ret_items.append(self.__get_diritem(item)) # 遍历所有文件,不含子目录 for item in SystemUtils.list_sub_all(path_obj): - ret_items.append(schemas.FileItem( - storage=self.schema.value, - type="file", - path=str(item).replace("\\", "/"), - name=item.name, - basename=item.stem, - extension=item.suffix[1:], - size=item.stat().st_size, - modify_time=item.stat().st_mtime, - )) + ret_items.append(self.__get_fileitem(item)) return ret_items def create_folder(self, fileitem: schemas.FileItem, name: str) -> Optional[schemas.FileItem]: @@ -110,15 +113,8 @@ class LocalStorage(StorageBase): return None path_obj = Path(fileitem.path) / name if not path_obj.exists(): - path_obj.mkdir(parents=True, exist_ok=True) - return schemas.FileItem( - storage=self.schema.value, - type="dir", - path=str(path_obj).replace("\\", "/") + "/", - name=name, - basename=name, - modify_time=path_obj.stat().st_mtime, - ) + path_obj.mkdir(parents=True) + return self.__get_diritem(path_obj) def get_folder(self, path: Path) -> Optional[schemas.FileItem]: """ @@ -126,30 +122,16 @@ class LocalStorage(StorageBase): """ if not path.exists(): path.mkdir(parents=True, exist_ok=True) - return schemas.FileItem( - storage=self.schema.value, - type="dir", - path=str(path).replace("\\", "/") + "/", - name=path.name, - basename=path.stem, - modify_time=path.stat().st_mtime, - ) + return self.__get_diritem(path) def detail(self, fileitm: schemas.FileItem) -> Optional[schemas.FileItem]: """ 获取文件详情 """ path_obj = Path(fileitm.path) - return schemas.FileItem( - storage=self.schema.value, - type="file", - path=str(path_obj).replace("\\", "/"), - name=path_obj.name, - basename=path_obj.stem, - extension=path_obj.suffix[1:], - size=path_obj.stat().st_size, - modify_time=path_obj.stat().st_mtime, - ) + if not path_obj.exists(): + return None + return self.__get_fileitem(path_obj) def delete(self, fileitem: schemas.FileItem) -> bool: """ @@ -160,10 +142,14 @@ class LocalStorage(StorageBase): path_obj = Path(fileitem.path) if not path_obj.exists(): return False - if path_obj.is_file(): - path_obj.unlink() - else: - shutil.rmtree(path_obj, ignore_errors=True) + try: + if path_obj.is_file(): + path_obj.unlink() + else: + shutil.rmtree(path_obj, ignore_errors=True) + except Exception as e: + logger.error(f"删除文件失败:{e}") + return False return True def rename(self, fileitem: schemas.FileItem, name: str) -> bool: @@ -173,13 +159,18 @@ class LocalStorage(StorageBase): path_obj = Path(fileitem.path) if not path_obj.exists(): return False - path_obj.rename(path_obj.parent / name) + try: + path_obj.rename(path_obj.parent / name) + except Exception as e: + logger.error(f"重命名文件失败:{e}") + return False + return True def download(self, fileitem: schemas.FileItem, path: Path) -> bool: """ 下载文件 """ - return False + pass def upload(self, fileitem: schemas.FileItem, path: Path) -> Optional[schemas.FileItem]: """ @@ -190,18 +181,14 @@ class LocalStorage(StorageBase): logger.warn(f"文件不存在:{filepath}") return None if not path.exists(): - filepath.rename(path) + try: + filepath.rename(path) + except Exception as e: + logger.error(f"移动文件失败:{e}") + return None if path.exists(): - return schemas.FileItem( - storage=self.schema.value, - type="file", - path=str(path).replace("\\", "/"), - name=path.name, - basename=path.stem, - extension=path.suffix[1:], - size=path.stat().st_size, - modify_time=path.stat().st_mtime, - ) + return self.__get_fileitem(path) + return None def copy(self, fileitem: schemas.FileItem, target_file: Path) -> bool: """ diff --git a/app/utils/system.py b/app/utils/system.py index b2e486f3..caba5555 100644 --- a/app/utils/system.py +++ b/app/utils/system.py @@ -94,7 +94,6 @@ class SystemUtils: shutil.copy2(src, dest) return 0, "" except Exception as err: - print(str(err)) return -1, str(err) @staticmethod @@ -109,7 +108,6 @@ class SystemUtils: shutil.move(temp, dest) return 0, "" except Exception as err: - print(str(err)) return -1, str(err) @staticmethod @@ -128,7 +126,6 @@ class SystemUtils: shutil.move(tmp_path, dest) return 0, "" except Exception as err: - print(str(err)) return -1, str(err) @staticmethod @@ -140,7 +137,6 @@ class SystemUtils: dest.symlink_to(src) return 0, "" except Exception as err: - print(str(err)) return -1, str(err) @staticmethod