diff --git a/app/api/endpoints/aliyun.py b/app/api/endpoints/aliyun.py index e704fba4..8acc8cee 100644 --- a/app/api/endpoints/aliyun.py +++ b/app/api/endpoints/aliyun.py @@ -10,6 +10,8 @@ from app.core.config import settings from app.core.metainfo import MetaInfoPath from app.core.security import verify_token, verify_uri_token from app.helper.aliyun import AliyunHelper +from app.helper.progress import ProgressHelper +from app.schemas.types import ProgressKey from app.utils.string import StringUtils router = APIRouter() @@ -166,27 +168,41 @@ def rename_aliyun(fileid: str, new_name: str, path: str, media_exts = settings.RMT_MEDIAEXT + settings.RMT_SUBEXT + settings.RMT_AUDIO_TRACK_EXT # 递归修改目录内文件(智能识别命名) sub_files: List[schemas.FileItem] = list_aliyun(path=path, fileid=fileid) - for sub_file in sub_files: - if sub_file.type == "dir": - continue - if not sub_file.extension: - continue - if f".{sub_file.extension.lower()}" not in media_exts: - continue - sub_path = Path(f"{path}{sub_file.name}") - meta = MetaInfoPath(sub_path) - mediainfo = transferchain.recognize_media(meta) - if not mediainfo: - return schemas.Response(success=False, message=f"{sub_path.name} 未识别到媒体信息") - new_path = transferchain.recommend_name(meta=meta, mediainfo=mediainfo) - if not new_path: - return schemas.Response(success=False, message=f"{sub_path.name} 未识别到新名称") - ret: schemas.Response = rename_aliyun(fileid=sub_file.fileid, - path=path, - new_name=Path(new_path).name, - recursive=False) - if not ret.success: - return schemas.Response(success=False, message=f"{sub_path.name} 重命名失败!") + if sub_files: + # 开始进度 + progress = ProgressHelper() + progress.start(ProgressKey.BatchRename) + total = len(sub_files) + handled = 0 + for sub_file in sub_files: + handled += 1 + progress.update(value=handled / total * 100, + text=f"正在处理 {sub_file.name} ...", + key=ProgressKey.BatchRename) + if sub_file.type == "dir": + continue + if not sub_file.extension: + continue + if f".{sub_file.extension.lower()}" not in media_exts: + continue + sub_path = Path(f"{path}{sub_file.name}") + meta = MetaInfoPath(sub_path) + mediainfo = transferchain.recognize_media(meta) + if not mediainfo: + progress.end(ProgressKey.BatchRename) + return schemas.Response(success=False, message=f"{sub_path.name} 未识别到媒体信息") + new_path = transferchain.recommend_name(meta=meta, mediainfo=mediainfo) + if not new_path: + progress.end(ProgressKey.BatchRename) + return schemas.Response(success=False, message=f"{sub_path.name} 未识别到新名称") + ret: schemas.Response = rename_aliyun(fileid=sub_file.fileid, + path=path, + new_name=Path(new_path).name, + recursive=False) + if not ret.success: + progress.end(ProgressKey.BatchRename) + return schemas.Response(success=False, message=f"{sub_path.name} 重命名失败!") + progress.end(ProgressKey.BatchRename) return schemas.Response(success=True) return schemas.Response(success=False) diff --git a/app/api/endpoints/local.py b/app/api/endpoints/local.py index 34e4d4c8..e8d08972 100644 --- a/app/api/endpoints/local.py +++ b/app/api/endpoints/local.py @@ -10,7 +10,9 @@ from app.chain.transfer import TransferChain from app.core.config import settings from app.core.metainfo import MetaInfoPath from app.core.security import verify_token, verify_uri_token +from app.helper.progress import ProgressHelper from app.log import logger +from app.schemas.types import ProgressKey from app.utils.system import SystemUtils router = APIRouter() @@ -212,24 +214,38 @@ def rename_local(path: str, new_name: str, media_exts = settings.RMT_MEDIAEXT + settings.RMT_SUBEXT + settings.RMT_AUDIO_TRACK_EXT # 递归修改目录内文件(智能识别命名) sub_files: List[schemas.FileItem] = list_local(path) - for sub_file in sub_files: - if sub_file.type == "dir": - continue - if not sub_file.extension: - continue - if f".{sub_file.extension.lower()}" not in media_exts: - continue - sub_path = Path(sub_file.path) - meta = MetaInfoPath(sub_path) - mediainfo = transferchain.recognize_media(meta) - if not mediainfo: - return schemas.Response(success=False, message=f"{sub_path.name} 未识别到媒体信息") - new_path = transferchain.recommend_name(meta=meta, mediainfo=mediainfo) - if not new_path: - return schemas.Response(success=False, message=f"{sub_path.name} 未识别到新名称") - ret: schemas.Response = rename_local(new_path, new_name=Path(new_path).name, recursive=False) - if not ret.success: - return schemas.Response(success=False, message=f"{sub_path.name} 重命名失败!") + if sub_files: + # 开始进度 + progress = ProgressHelper() + progress.start(ProgressKey.BatchRename) + total = len(sub_files) + handled = 0 + for sub_file in sub_files: + handled += 1 + progress.update(value=handled / total * 100, + text=f"正在处理 {sub_file.name} ...", + key=ProgressKey.BatchRename) + if sub_file.type == "dir": + continue + if not sub_file.extension: + continue + if f".{sub_file.extension.lower()}" not in media_exts: + continue + sub_path = Path(sub_file.path) + meta = MetaInfoPath(sub_path) + mediainfo = transferchain.recognize_media(meta) + if not mediainfo: + progress.end(ProgressKey.BatchRename) + return schemas.Response(success=False, message=f"{sub_path.name} 未识别到媒体信息") + new_path = transferchain.recommend_name(meta=meta, mediainfo=mediainfo) + if not new_path: + progress.end(ProgressKey.BatchRename) + return schemas.Response(success=False, message=f"{sub_path.name} 未识别到新名称") + ret: schemas.Response = rename_local(new_path, new_name=Path(new_path).name, recursive=False) + if not ret.success: + progress.end(ProgressKey.BatchRename) + return schemas.Response(success=False, message=f"{sub_path.name} 重命名失败!") + progress.end(ProgressKey.BatchRename) return schemas.Response(success=True) diff --git a/app/api/endpoints/u115.py b/app/api/endpoints/u115.py index bc534b74..df54a038 100644 --- a/app/api/endpoints/u115.py +++ b/app/api/endpoints/u115.py @@ -10,7 +10,9 @@ from app.chain.transfer import TransferChain from app.core.config import settings from app.core.metainfo import MetaInfoPath from app.core.security import verify_token, verify_uri_token +from app.helper.progress import ProgressHelper from app.helper.u115 import U115Helper +from app.schemas.types import ProgressKey from app.utils.http import RequestUtils router = APIRouter() @@ -171,27 +173,41 @@ def rename_115(fileid: str, new_name: str, path: str, media_exts = settings.RMT_MEDIAEXT + settings.RMT_SUBEXT + settings.RMT_AUDIO_TRACK_EXT # 递归修改目录内文件(智能识别命名) sub_files: List[schemas.FileItem] = list_115(path=path, fileid=fileid) - for sub_file in sub_files: - if sub_file.type == "dir": - continue - if not sub_file.extension: - continue - if f".{sub_file.extension.lower()}" not in media_exts: - continue - sub_path = Path(f"{path}{sub_file.name}") - meta = MetaInfoPath(sub_path) - mediainfo = transferchain.recognize_media(meta) - if not mediainfo: - return schemas.Response(success=False, message=f"{sub_path.name} 未识别到媒体信息") - new_path = transferchain.recommend_name(meta=meta, mediainfo=mediainfo) - if not new_path: - return schemas.Response(success=False, message=f"{sub_path.name} 未识别到新名称") - ret: schemas.Response = rename_115(fileid=sub_file.fileid, - path=path, - new_name=Path(new_path).name, - recursive=False) - if not ret.success: - return schemas.Response(success=False, message=f"{sub_path.name} 重命名失败!") + if sub_files: + # 开始进度 + progress = ProgressHelper() + progress.start(ProgressKey.BatchRename) + total = len(sub_files) + handled = 0 + for sub_file in sub_files: + handled += 1 + progress.update(value=handled / total * 100, + text=f"正在处理 {sub_file.name} ...", + key=ProgressKey.BatchRename) + if sub_file.type == "dir": + continue + if not sub_file.extension: + continue + if f".{sub_file.extension.lower()}" not in media_exts: + continue + sub_path = Path(f"{path}{sub_file.name}") + meta = MetaInfoPath(sub_path) + mediainfo = transferchain.recognize_media(meta) + if not mediainfo: + progress.end(ProgressKey.BatchRename) + return schemas.Response(success=False, message=f"{sub_path.name} 未识别到媒体信息") + new_path = transferchain.recommend_name(meta=meta, mediainfo=mediainfo) + if not new_path: + progress.end(ProgressKey.BatchRename) + return schemas.Response(success=False, message=f"{sub_path.name} 未识别到新名称") + ret: schemas.Response = rename_115(fileid=sub_file.fileid, + path=path, + new_name=Path(new_path).name, + recursive=False) + if not ret.success: + progress.end(ProgressKey.BatchRename) + return schemas.Response(success=False, message=f"{sub_path.name} 重命名失败!") + progress.end(ProgressKey.BatchRename) return schemas.Response(success=True) return schemas.Response(success=False) diff --git a/app/helper/progress.py b/app/helper/progress.py index 4d1004ce..67294f6c 100644 --- a/app/helper/progress.py +++ b/app/helper/progress.py @@ -34,7 +34,11 @@ class ProgressHelper(metaclass=Singleton): key = key.value if not self._process_detail.get(key): return - self._process_detail[key]['enable'] = False + self._process_detail[key] = { + "enable": False, + "value": 100, + "text": "正在处理..." + } def update(self, key: Union[ProgressKey, str], value: float = None, text: str = None): if isinstance(key, Enum): diff --git a/app/schemas/types.py b/app/schemas/types.py index 313b04e1..625e27d4 100644 --- a/app/schemas/types.py +++ b/app/schemas/types.py @@ -106,6 +106,8 @@ class ProgressKey(Enum): Search = "search" # 转移 FileTransfer = "filetransfer" + # 批量重命名 + BatchRename = "batchrename" # 媒体图片类型