from __future__ import annotations import asyncio import json from fastapi import APIRouter, Depends from fastapi.responses import StreamingResponse from pydantic import BaseModel, Field from sqlalchemy.ext.asyncio import AsyncSession from src.core.db import get_session, get_session_factory from src.services import upload_jobs as svc router = APIRouter(prefix="/projects/{project_id}/upload-jobs", tags=["upload-jobs"]) class UploadFileIn(BaseModel): fileName: str contentType: str = "application/octet-stream" size: int = Field(default=0, ge=0) class CreateUploadJobIn(BaseModel): kind: str = "images" files: list[UploadFileIn] = Field(min_length=1, max_length=2000) class CompleteItemIn(BaseModel): width: int = Field(default=0, ge=0) height: int = Field(default=0, ge=0) @router.post("", status_code=201) async def create(project_id: str, body: CreateUploadJobIn, db: AsyncSession = Depends(get_session)): return await svc.create_job(project_id, body.kind, [f.model_dump() for f in body.files], db) @router.get("/{job_id}") async def get(project_id: str, job_id: str, db: AsyncSession = Depends(get_session)): data = await svc.get_job(job_id, db) if data["projectId"] != project_id: from fastapi import HTTPException raise HTTPException(404, "upload job not found") return data @router.post("/{job_id}/items/{item_id}/complete") async def complete(project_id: str, job_id: str, item_id: str, body: CompleteItemIn, db: AsyncSession = Depends(get_session)): data = await svc.complete_item(job_id, item_id, body.width, body.height, db) if data["projectId"] != project_id: from fastapi import HTTPException raise HTTPException(404, "upload job not found") return data @router.get("/{job_id}/events") async def events(project_id: str, job_id: str): async def stream(): factory = get_session_factory() while True: async with factory() as db: data = await svc.get_job(job_id, db) if data["projectId"] != project_id: break yield f"event: progress\ndata: {json.dumps(data)}\n\n" if data["status"] in {"done", "partial_error", "error", "cancelled"}: break await asyncio.sleep(1) return StreamingResponse(stream(), media_type="text/event-stream", headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"})