feat: add batch-delete API endpoint, replace N+1 frontend calls

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-12 21:45:28 +08:00
parent 3a49780d8d
commit 17c45cab3f
4 changed files with 56 additions and 24 deletions
+32
View File
@@ -169,6 +169,34 @@ async def delete_file(
return {"message": f"已删除 {filename}"}
@router.post("/batch-delete")
async def batch_delete_files(
req: BatchDeleteRequest,
current_user: dict = Depends(get_current_user),
):
"""Batch delete files from disk and clean up relation records."""
dir_map = {"input": _input_dir, "output": _output_dir, "result": _result_dir}
deleted = 0
errors = []
for item in req.files:
d = item.get("directory", "")
fname = item.get("filename", "")
if d not in dir_map or not fname:
errors.append(f"无效参数: {d}/{fname}")
continue
file_path = dir_map[d] / fname
try:
if file_path.exists():
size = file_path.stat().st_size
file_path.unlink()
deleted += 1
_record_file_action(fname, d, size, "delete", current_user.get("username"))
_cleanup_relation_for_deleted_file(d, fname)
except Exception as e:
errors.append(f"{fname}: {str(e)}")
return {"deleted": deleted, "errors": errors}
@router.post("/clear/{directory}")
async def clear_directory(
directory: str,
@@ -224,6 +252,10 @@ class RelationDeleteRequest(BaseModel):
ids: List[int]
class BatchDeleteRequest(BaseModel):
files: list[dict]
def _cleanup_relation_for_deleted_file(directory: str, filename: str):
"""Clean up relation table when a file is deleted."""
import sqlite3