mirror of
https://github.com/JefferyHcool/BiliNote.git
synced 2026-05-06 20:42:52 +08:00
refactor(events): 重构清理临时文件的逻辑
- 优化了文件路径的处理方式 -增加了对路径不存在的情况的处理 - 改进了日志信息的记录
This commit is contained in:
@@ -4,18 +4,22 @@ logger = get_logger(__name__)
|
|||||||
|
|
||||||
def cleanup_temp_files(data):
|
def cleanup_temp_files(data):
|
||||||
logger.info(f"starting cleanup temp files :{data['file_path']}")
|
logger.info(f"starting cleanup temp files :{data['file_path']}")
|
||||||
# 获取名称
|
file_path = data['file_path']
|
||||||
path=data['file_path']
|
if not os.path.exists(file_path):
|
||||||
name=path[:-4]
|
logger.warning(f"路径不存在:{file_path}")
|
||||||
# 删除以这个文件开头的
|
return
|
||||||
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']}")
|
|
||||||
|
|
||||||
|
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}")
|
||||||
|
|||||||
Reference in New Issue
Block a user