56 lines
1.5 KiB
Python
Executable File
56 lines
1.5 KiB
Python
Executable File
"""Shared job status and download endpoints used by all tools."""
|
|
from pathlib import Path
|
|
|
|
from fastapi import APIRouter, HTTPException
|
|
from fastapi.responses import FileResponse
|
|
|
|
from .. import job_manager as jm
|
|
|
|
router = APIRouter(prefix="/api/jobs", tags=["jobs"])
|
|
|
|
|
|
@router.get("")
|
|
def list_jobs():
|
|
return jm.list_jobs()
|
|
|
|
|
|
@router.get("/{job_id}")
|
|
def get_job(job_id: str):
|
|
job = jm.get_job(job_id)
|
|
if job is None:
|
|
raise HTTPException(status_code=404, detail="Job not found")
|
|
return job
|
|
|
|
|
|
@router.delete("/{job_id}")
|
|
def cancel_job(job_id: str):
|
|
job = jm.get_job(job_id)
|
|
if job is None:
|
|
raise HTTPException(status_code=404, detail="Job not found")
|
|
jm.cancel_job(job_id)
|
|
return {"detail": "Cancellation requested"}
|
|
|
|
|
|
@router.get("/{job_id}/download/{filename}")
|
|
def download_file(job_id: str, filename: str):
|
|
job = jm.get_job(job_id)
|
|
if job is None:
|
|
raise HTTPException(status_code=404, detail="Job not found")
|
|
if job["status"] != "done":
|
|
raise HTTPException(status_code=400, detail="Job not complete")
|
|
|
|
# Find the file in the job's file list
|
|
match = next((f for f in job["files"] if f["name"] == filename), None)
|
|
if match is None:
|
|
raise HTTPException(status_code=404, detail="File not found")
|
|
|
|
file_path = Path(match["path"])
|
|
if not file_path.exists():
|
|
raise HTTPException(status_code=404, detail="File missing from disk")
|
|
|
|
return FileResponse(
|
|
path=str(file_path),
|
|
filename=filename,
|
|
media_type="application/octet-stream",
|
|
)
|