fix: 防止任务删除误删源视频目录并跟踪审查问题

This commit is contained in:
2026-09-11 15:40:09 +08:00
parent eba9163246
commit 13f72178e9
4 changed files with 174 additions and 12 deletions
+7 -7
View File
@@ -82,9 +82,9 @@ class OrphanCleaner:
run = self.db.get_run(run_id)
if run is None:
continue
# 批量处理运行(source=batch)跳过清理:其产物在用户视频旁的同名
# 文件夹里,不在主存储目录下;_has_files 检查不到会误判为孤儿删除,
# 且 _remove_run 还会删除 input_uri 的父目录(用户的视频文件夹)
# 批量处理运行(source=batch)跳过清理:其工作空间位于独立的
# storage/batch 层级,_has_files 检查主 runs 目录会误判为孤儿
# 批量记录与私有工作空间由批量引擎负责收尾,用户媒体目录始终保留
if run.get("source") == "batch":
continue
if run["status"] != "COMPLETED":
@@ -125,10 +125,10 @@ class OrphanCleaner:
return any(path.is_file() for path in run_dir.rglob("*"))
def _remove_run(self, run_id: str, run: dict) -> int:
"""删除孤儿任务:数据库记录(含产物)、上传目录与步骤目录"""
"""删除孤儿上传任务的记录与私有目录,保留任何外部输入路径"""
self.db.delete_run(run_id)
input_uri = run.get("input_uri")
if input_uri:
shutil.rmtree(Path(input_uri).parent, ignore_errors=True)
# 与手动删除一致,只按私有存储布局定位;历史记录的 input_uri
# 可能引用用户媒体库,不能把其父目录当作可递归删除的上传目录。
shutil.rmtree(self.storage_dir / "uploads" / run_id, ignore_errors=True)
shutil.rmtree(self.storage_dir / "runs" / run_id, ignore_errors=True)
return 1
+8 -5
View File
@@ -184,18 +184,21 @@ def resume_run(run_id: str, db: Database = Depends(_get_db)) -> dict:
@router.delete("/api/runs/{run_id}")
def delete_run(run_id: str, db: Database = Depends(_get_db)) -> dict:
"""删除任务:清理产物记录、上传文件与步骤产物目录"""
"""删除上传任务及其私有文件;批量 run 必须通过批量任务入口删除"""
run = db.get_run(run_id)
if run is None:
raise HTTPException(status_code=404, detail="run not found")
# 批量 run 的输入是用户原始视频,且由 batch_videos 关联管理。
# 在任何数据库/文件删除之前拒绝,避免误删媒体库或留下悬空的批量明细。
if run.get("source") == "batch":
raise HTTPException(status_code=422, detail="请通过批量任务入口删除该任务")
from wov_app.config import STORAGE_DIR
# 先删数据库记录(含产物表),再清理磁盘上的上传与中间产物。
db.delete_run(run_id)
input_uri = run.get("input_uri")
if input_uri:
# 上传文件位于 <storage>/uploads/<run_id>/,整目录一并删除。
shutil.rmtree(Path(input_uri).parent, ignore_errors=True)
# 删除范围仅来自该任务的私有存储布局,绝不由 input_uri 推导:
# 即使历史上传记录引用外部路径,也必须保留源视频和同目录的用户文件。
shutil.rmtree(STORAGE_DIR / "uploads" / run_id, ignore_errors=True)
# 步骤产物位于 <storage>/runs/<run_id>/,整目录一并删除。
shutil.rmtree(STORAGE_DIR / "runs" / run_id, ignore_errors=True)
return {"deleted": run_id}