From a434b9a7a41899a2cb1ca9fd8cda2dc137462a43 Mon Sep 17 00:00:00 2001 From: Jefferyhcool <1063474837@qq.com> Date: Tue, 15 Apr 2025 00:40:38 +0800 Subject: [PATCH] =?UTF-8?q?refactor(events):=20=E9=87=8D=E6=9E=84=E6=B8=85?= =?UTF-8?q?=E7=90=86=E4=B8=B4=E6=97=B6=E6=96=87=E4=BB=B6=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 优化了文件路径的处理方式 -增加了对路径不存在的情况的处理 - 改进了日志信息的记录 --- backend/events/handlers.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/backend/events/handlers.py b/backend/events/handlers.py index 32f4858..e4b0015 100644 --- a/backend/events/handlers.py +++ b/backend/events/handlers.py @@ -4,18 +4,22 @@ logger = get_logger(__name__) def cleanup_temp_files(data): logger.info(f"starting cleanup temp files :{data['file_path']}") - # 获取名称 - path=data['file_path'] - name=path[:-4] - # 删除以这个文件开头的 - for file in os.listdir(os.path.dirname(path)): - if file.startswith(name): - os.remove(os.path.join(os.path.dirname(path), file)) - os.remove(data['file_path']) - # 检查是否删除文件 - if os.path.exists(data['file_path']): - logger.info(f"cleanup temp files failed :{data['file_path']}") - else: - logger.info(f"cleanup temp files success :{data['file_path']}") + file_path = data['file_path'] + if not os.path.exists(file_path): + logger.warning(f"路径不存在:{file_path}") + return + dir_path = os.path.dirname(file_path) + base_name = os.path.basename(file_path) + video_id, _ = os.path.splitext(base_name) + logger.info(f"开始清理 video_id={video_id} 所有相关文件") + + for file in os.listdir(dir_path): + if file.startswith(video_id): + full_path = os.path.join(dir_path, file) + try: + os.remove(full_path) + logger.info(f"删除文件:{full_path}") + except Exception as e: + logger.error(f"删除失败:{full_path},原因:{e}")