From 91d7942a6f56505479a9be00ec8e3f8227693a04 Mon Sep 17 00:00:00 2001 From: Deroino <117969239+Deroino@users.noreply.github.com> Date: Thu, 13 Nov 2025 05:58:01 +0800 Subject: [PATCH] fix(backend): delete actual file when removing from database - Add file deletion logic to delete_file endpoint - Remove actual file from videoFile directory when deleting record - Add error handling for file deletion operations - Add logging for file deletion status - Ensure data consistency between database and file system --- sau_backend.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/sau_backend.py b/sau_backend.py index 7a54d92..051804d 100644 --- a/sau_backend.py +++ b/sau_backend.py @@ -235,6 +235,18 @@ def delete_file(): record = dict(record) + # 获取文件路径并删除实际文件 + file_path = Path(BASE_DIR / "videoFile" / record['file_path']) + if file_path.exists(): + try: + file_path.unlink() # 删除文件 + print(f"✅ 实际文件已删除: {file_path}") + except Exception as e: + print(f"⚠️ 删除实际文件失败: {e}") + # 即使删除文件失败,也要继续删除数据库记录,避免数据不一致 + else: + print(f"⚠️ 实际文件不存在: {file_path}") + # 删除数据库记录 cursor.execute("DELETE FROM file_records WHERE id = ?", (file_id,)) conn.commit()