refactor(events): 重构清理临时文件的逻辑

- 优化了文件路径的处理方式
-增加了对路径不存在的情况的处理
- 改进了日志信息的记录
This commit is contained in:
Jefferyhcool
2025-04-15 00:40:38 +08:00
parent 7bf31ad16e
commit a434b9a7a4

View File

@@ -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}")