media-rip/backend/app/routers/downloads.py
xpltd efc2ead796 M001: media.rip() v1.0 — complete application
Full-featured self-hosted yt-dlp web frontend:
- Python 3.12+ / FastAPI backend with async SQLite, SSE transport, session isolation
- Vue 3 / TypeScript / Pinia frontend with real-time progress, theme picker
- 3 built-in themes (cyberpunk/dark/light) + drop-in custom theme system
- Admin auth (bcrypt), purge system, cookie upload, file serving
- Docker multi-stage build, GitHub Actions CI/CD
- 179 backend tests, 29 frontend tests (208 total)

Slices: S01 (Foundation), S02 (SSE+Sessions), S03 (Frontend),
        S04 (Admin+Auth), S05 (Themes), S06 (Docker+CI)
2026-03-18 20:00:17 -05:00

70 lines
2.1 KiB
Python

"""Download management API routes.
POST /downloads — enqueue a new download job
GET /downloads — list jobs for the current session
DELETE /downloads/{job_id} — cancel a job
"""
from __future__ import annotations
import logging
from fastapi import APIRouter, Depends, Request
from fastapi.responses import JSONResponse
from app.core.database import get_job, get_jobs_by_session
from app.dependencies import get_session_id
from app.models.job import Job, JobCreate
logger = logging.getLogger("mediarip.api.downloads")
router = APIRouter(tags=["downloads"])
@router.post("/downloads", response_model=Job, status_code=201)
async def create_download(
job_create: JobCreate,
request: Request,
session_id: str = Depends(get_session_id),
) -> Job:
"""Submit a URL for download."""
logger.debug("POST /downloads session=%s url=%s", session_id, job_create.url)
download_service = request.app.state.download_service
job = await download_service.enqueue(job_create, session_id)
return job
@router.get("/downloads", response_model=list[Job])
async def list_downloads(
request: Request,
session_id: str = Depends(get_session_id),
) -> list[Job]:
"""List all download jobs for the current session."""
logger.debug("GET /downloads session=%s", session_id)
jobs = await get_jobs_by_session(request.app.state.db, session_id)
return jobs
@router.delete("/downloads/{job_id}")
async def cancel_download(
job_id: str,
request: Request,
) -> dict:
"""Cancel (mark as failed) a download job."""
logger.debug("DELETE /downloads/%s", job_id)
db = request.app.state.db
download_service = request.app.state.download_service
# Fetch the job first to get its session_id for the SSE broadcast
job = await get_job(db, job_id)
await download_service.cancel(job_id)
# Notify any SSE clients watching this session
if job is not None:
request.app.state.broker.publish(
job.session_id,
{"event": "job_removed", "data": {"job_id": job_id}},
)
return {"status": "cancelled"}