media-rip/backend/app/routers/downloads.py
xpltd 0d9e6b18ac M002/S04: URL preview, playlist support, admin improvements, UX polish
URL preview & playlist support:
- POST /url-info endpoint extracts metadata (title, type, entry count)
- Preview box shows playlist contents before downloading (up to 10 items)
- Auto-detect audio-only sources (SoundCloud, etc) and switch to Audio mode
- Video toggle grayed out for audio-only sources
- Enable playlist downloading (noplaylist=False)

Admin panel improvements:
- Expandable session rows show per-session job list with filename, size,
  status, timestamp, and source URL link
- GET /admin/sessions/{id}/jobs endpoint for session job details
- Logout now redirects to home page instead of staying on login form
- Logo in header is clickable → navigates to home

UX polish:
- Tooltips on output format chips (explains Auto vs specific formats)
- Format tooltips change based on video/audio mode
2026-03-19 02:32:14 -05:00

83 lines
2.5 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"}
@router.post("/url-info")
async def url_info(
request: Request,
body: dict,
) -> dict:
"""Extract metadata about a URL (title, playlist detection, audio-only detection)."""
url = body.get("url", "").strip()
if not url:
return JSONResponse(status_code=400, content={"detail": "URL required"})
download_service = request.app.state.download_service
return await download_service.get_url_info(url)