feat: 优化打包产物与历史筛选
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"""任务 API"""
|
||||
import os
|
||||
import json
|
||||
import shutil
|
||||
import tempfile
|
||||
import uuid
|
||||
@@ -164,6 +165,25 @@ async def delete_task(task_id: str, db: Session = Depends(get_db)):
|
||||
if task.status in ("running", "pending"):
|
||||
raise HTTPException(status_code=400, detail="任务正在运行中,无法删除")
|
||||
|
||||
# 同一个 App / 版本 / 分支重复打包会覆盖并复用同一个远端文件;
|
||||
# 只要仍有其他历史记录引用该下载地址,就不能删除远端产物。
|
||||
has_shared_artifact = bool(
|
||||
task.oss_url and db.query(Task).filter(
|
||||
Task.id != task.id,
|
||||
Task.oss_url == task.oss_url,
|
||||
).first()
|
||||
)
|
||||
if task.oss_url and not has_shared_artifact:
|
||||
if not task.config_json:
|
||||
raise HTTPException(status_code=400, detail="缺少打包配置快照,无法安全删除远端文件")
|
||||
try:
|
||||
build_config = json.loads(task.config_json)
|
||||
from .config import load_config
|
||||
from ..services.distribution import DistributionError, delete_published_artifacts
|
||||
delete_published_artifacts(build_config, load_config().get("upload", {}))
|
||||
except (json.JSONDecodeError, DistributionError) as exc:
|
||||
raise HTTPException(status_code=502, detail=f"远端产物删除失败,记录未删除:{exc}") from exc
|
||||
|
||||
# 删除日志文件
|
||||
log_path = Path(__file__).parent.parent / "logs" / f"{task_id}.log"
|
||||
if log_path.exists():
|
||||
@@ -178,7 +198,10 @@ async def delete_task(task_id: str, db: Session = Depends(get_db)):
|
||||
db.delete(task)
|
||||
db.commit()
|
||||
|
||||
return {"message": "记录已删除"}
|
||||
message = "记录已删除"
|
||||
if task.oss_url:
|
||||
message += "(远端产物已删除)" if not has_shared_artifact else "(远端产物仍被其他记录引用,未删除)"
|
||||
return {"message": message}
|
||||
|
||||
|
||||
@router.get("/{task_id}/log")
|
||||
|
||||
Reference in New Issue
Block a user