批量重命名进度更新

This commit is contained in:
jxxghp 2024-06-19 15:22:05 +08:00
parent 681f1eaeb5
commit 79c57d8e4f
5 changed files with 115 additions and 61 deletions

View File

@ -10,6 +10,8 @@ from app.core.config import settings
from app.core.metainfo import MetaInfoPath from app.core.metainfo import MetaInfoPath
from app.core.security import verify_token, verify_uri_token from app.core.security import verify_token, verify_uri_token
from app.helper.aliyun import AliyunHelper from app.helper.aliyun import AliyunHelper
from app.helper.progress import ProgressHelper
from app.schemas.types import ProgressKey
from app.utils.string import StringUtils from app.utils.string import StringUtils
router = APIRouter() 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 media_exts = settings.RMT_MEDIAEXT + settings.RMT_SUBEXT + settings.RMT_AUDIO_TRACK_EXT
# 递归修改目录内文件(智能识别命名) # 递归修改目录内文件(智能识别命名)
sub_files: List[schemas.FileItem] = list_aliyun(path=path, fileid=fileid) sub_files: List[schemas.FileItem] = list_aliyun(path=path, fileid=fileid)
for sub_file in sub_files: if sub_files:
if sub_file.type == "dir": # 开始进度
continue progress = ProgressHelper()
if not sub_file.extension: progress.start(ProgressKey.BatchRename)
continue total = len(sub_files)
if f".{sub_file.extension.lower()}" not in media_exts: handled = 0
continue for sub_file in sub_files:
sub_path = Path(f"{path}{sub_file.name}") handled += 1
meta = MetaInfoPath(sub_path) progress.update(value=handled / total * 100,
mediainfo = transferchain.recognize_media(meta) text=f"正在处理 {sub_file.name} ...",
if not mediainfo: key=ProgressKey.BatchRename)
return schemas.Response(success=False, message=f"{sub_path.name} 未识别到媒体信息") if sub_file.type == "dir":
new_path = transferchain.recommend_name(meta=meta, mediainfo=mediainfo) continue
if not new_path: if not sub_file.extension:
return schemas.Response(success=False, message=f"{sub_path.name} 未识别到新名称") continue
ret: schemas.Response = rename_aliyun(fileid=sub_file.fileid, if f".{sub_file.extension.lower()}" not in media_exts:
path=path, continue
new_name=Path(new_path).name, sub_path = Path(f"{path}{sub_file.name}")
recursive=False) meta = MetaInfoPath(sub_path)
if not ret.success: mediainfo = transferchain.recognize_media(meta)
return schemas.Response(success=False, message=f"{sub_path.name} 重命名失败!") 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=True)
return schemas.Response(success=False) return schemas.Response(success=False)

View File

@ -10,7 +10,9 @@ from app.chain.transfer import TransferChain
from app.core.config import settings from app.core.config import settings
from app.core.metainfo import MetaInfoPath from app.core.metainfo import MetaInfoPath
from app.core.security import verify_token, verify_uri_token from app.core.security import verify_token, verify_uri_token
from app.helper.progress import ProgressHelper
from app.log import logger from app.log import logger
from app.schemas.types import ProgressKey
from app.utils.system import SystemUtils from app.utils.system import SystemUtils
router = APIRouter() 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 media_exts = settings.RMT_MEDIAEXT + settings.RMT_SUBEXT + settings.RMT_AUDIO_TRACK_EXT
# 递归修改目录内文件(智能识别命名) # 递归修改目录内文件(智能识别命名)
sub_files: List[schemas.FileItem] = list_local(path) sub_files: List[schemas.FileItem] = list_local(path)
for sub_file in sub_files: if sub_files:
if sub_file.type == "dir": # 开始进度
continue progress = ProgressHelper()
if not sub_file.extension: progress.start(ProgressKey.BatchRename)
continue total = len(sub_files)
if f".{sub_file.extension.lower()}" not in media_exts: handled = 0
continue for sub_file in sub_files:
sub_path = Path(sub_file.path) handled += 1
meta = MetaInfoPath(sub_path) progress.update(value=handled / total * 100,
mediainfo = transferchain.recognize_media(meta) text=f"正在处理 {sub_file.name} ...",
if not mediainfo: key=ProgressKey.BatchRename)
return schemas.Response(success=False, message=f"{sub_path.name} 未识别到媒体信息") if sub_file.type == "dir":
new_path = transferchain.recommend_name(meta=meta, mediainfo=mediainfo) continue
if not new_path: if not sub_file.extension:
return schemas.Response(success=False, message=f"{sub_path.name} 未识别到新名称") continue
ret: schemas.Response = rename_local(new_path, new_name=Path(new_path).name, recursive=False) if f".{sub_file.extension.lower()}" not in media_exts:
if not ret.success: continue
return schemas.Response(success=False, message=f"{sub_path.name} 重命名失败!") 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) return schemas.Response(success=True)

View File

@ -10,7 +10,9 @@ from app.chain.transfer import TransferChain
from app.core.config import settings from app.core.config import settings
from app.core.metainfo import MetaInfoPath from app.core.metainfo import MetaInfoPath
from app.core.security import verify_token, verify_uri_token from app.core.security import verify_token, verify_uri_token
from app.helper.progress import ProgressHelper
from app.helper.u115 import U115Helper from app.helper.u115 import U115Helper
from app.schemas.types import ProgressKey
from app.utils.http import RequestUtils from app.utils.http import RequestUtils
router = APIRouter() 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 media_exts = settings.RMT_MEDIAEXT + settings.RMT_SUBEXT + settings.RMT_AUDIO_TRACK_EXT
# 递归修改目录内文件(智能识别命名) # 递归修改目录内文件(智能识别命名)
sub_files: List[schemas.FileItem] = list_115(path=path, fileid=fileid) sub_files: List[schemas.FileItem] = list_115(path=path, fileid=fileid)
for sub_file in sub_files: if sub_files:
if sub_file.type == "dir": # 开始进度
continue progress = ProgressHelper()
if not sub_file.extension: progress.start(ProgressKey.BatchRename)
continue total = len(sub_files)
if f".{sub_file.extension.lower()}" not in media_exts: handled = 0
continue for sub_file in sub_files:
sub_path = Path(f"{path}{sub_file.name}") handled += 1
meta = MetaInfoPath(sub_path) progress.update(value=handled / total * 100,
mediainfo = transferchain.recognize_media(meta) text=f"正在处理 {sub_file.name} ...",
if not mediainfo: key=ProgressKey.BatchRename)
return schemas.Response(success=False, message=f"{sub_path.name} 未识别到媒体信息") if sub_file.type == "dir":
new_path = transferchain.recommend_name(meta=meta, mediainfo=mediainfo) continue
if not new_path: if not sub_file.extension:
return schemas.Response(success=False, message=f"{sub_path.name} 未识别到新名称") continue
ret: schemas.Response = rename_115(fileid=sub_file.fileid, if f".{sub_file.extension.lower()}" not in media_exts:
path=path, continue
new_name=Path(new_path).name, sub_path = Path(f"{path}{sub_file.name}")
recursive=False) meta = MetaInfoPath(sub_path)
if not ret.success: mediainfo = transferchain.recognize_media(meta)
return schemas.Response(success=False, message=f"{sub_path.name} 重命名失败!") 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=True)
return schemas.Response(success=False) return schemas.Response(success=False)

View File

@ -34,7 +34,11 @@ class ProgressHelper(metaclass=Singleton):
key = key.value key = key.value
if not self._process_detail.get(key): if not self._process_detail.get(key):
return 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): def update(self, key: Union[ProgressKey, str], value: float = None, text: str = None):
if isinstance(key, Enum): if isinstance(key, Enum):

View File

@ -106,6 +106,8 @@ class ProgressKey(Enum):
Search = "search" Search = "search"
# 转移 # 转移
FileTransfer = "filetransfer" FileTransfer = "filetransfer"
# 批量重命名
BatchRename = "batchrename"
# 媒体图片类型 # 媒体图片类型