Download backend/src/api/routes/media.py from new-arc/vector: direct link, hf CLI and curl.
- Browser
- Download file 19.7 kB
-
https://huggingface.co/spaces/new-arc/vector/resolve/main/backend/src/api/routes/media.py
- Command line
-
hf download hf://spaces/new-arc/vector/backend/src/api/routes/media.py
-
curl -L -o media.py https://huggingface.co/spaces/new-arc/vector/resolve/main/backend/src/api/routes/media.py
19.7 kB
| """ | |
| Media API routes (fonts, transitions, uploads). | |
| """ | |
| from fastapi import APIRouter, HTTPException, Request, UploadFile, File | |
| from fastapi.responses import FileResponse, JSONResponse | |
| from pathlib import Path | |
| from typing import Any, cast | |
| import logging | |
| import asyncio | |
| import re | |
| import uuid | |
| import aiofiles | |
| from starlette.background import BackgroundTask | |
| from ...config import get_config | |
| from ...media_storage import MediaStorage, STORAGE_ERRORS, media_work | |
| from ...media.upload_ingest import ( | |
| UploadRejected, | |
| persist_upload_after_response, | |
| publish_spooled_upload, | |
| ) | |
| from ...repositories.upload_repository import ( | |
| reserve_upload, | |
| bind_upload, | |
| get_upload, | |
| finish_upload, | |
| claim_stored_upload, | |
| UploadIdempotencyConflict, | |
| UploadLimitReached, | |
| ) | |
| from ...utils.async_helpers import run_in_thread | |
| from ...database import get_db | |
| from ...auth_headers import resolve_authenticated_user_id | |
| from ...services.billing_service import BillingService, grants_paid_access | |
| from starlette.formparsers import MultiPartException, MultiPartParser | |
| from ...media.upload_limits import ( | |
| MULTIPART_FRAMING_BYTES, | |
| reject_content_length, | |
| upload_too_large_detail, | |
| ) | |
| from ...font_registry import ( | |
| FONTS_DIR, | |
| SUPPORTED_FONT_EXTENSIONS, | |
| build_user_font_stem, | |
| find_font_path, | |
| find_user_font_path, | |
| get_available_fonts as list_available_fonts, | |
| get_user_fonts_dir, | |
| sanitize_font_stem, | |
| ) | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| from fastapi import Depends | |
| logger = logging.getLogger(__name__) | |
| router = APIRouter(tags=["media"]) | |
| MAX_FONT_UPLOAD_BYTES = 10 * 1024 * 1024 | |
| async def _get_authenticated_user_id(request: Request, db: AsyncSession) -> str: | |
| config = get_config() | |
| return await resolve_authenticated_user_id(request, db, config) | |
| async def _read_limited_upload_form(request: Request, max_bytes: int): | |
| """Parse one video upload without buffering past the cap. | |
| Starlette's default multipart parser spools a file part with no size | |
| limit, so a chunked body or a spoofed short Content-Length (when the | |
| server still delivers the rest) would fill the disk before the handler | |
| ran. Oversized declared lengths are refused first. Everything else is | |
| counted as it arrives, and a file past the cap becomes 413. | |
| """ | |
| raw_headers = list(request.scope.get("headers") or []) | |
| lengths = [ | |
| value.decode("latin-1").strip() | |
| for name, value in raw_headers | |
| if name.lower() == b"content-length" | |
| ] | |
| encodings = [ | |
| value.decode("latin-1").strip() | |
| for name, value in raw_headers | |
| if name.lower() == b"transfer-encoding" | |
| ] | |
| reason = reject_content_length(lengths, encodings, max_bytes) | |
| if reason == "too_large": | |
| raise HTTPException(status_code=413, detail=upload_too_large_detail(max_bytes)) | |
| if reason == "conflict": | |
| raise HTTPException(status_code=400, detail="Conflicting upload length") | |
| if reason == "malformed": | |
| raise HTTPException(status_code=400, detail="A single Content-Length is required") | |
| ceiling = max_bytes + MULTIPART_FRAMING_BYTES | |
| async def chunks(): | |
| received = 0 | |
| async for chunk in request.stream(): | |
| received += len(chunk) | |
| if received > ceiling: | |
| raise HTTPException(status_code=413, detail=upload_too_large_detail(max_bytes)) | |
| yield chunk | |
| parser = MultiPartParser( | |
| request.headers, | |
| chunks(), | |
| max_files=1, | |
| max_fields=8, | |
| max_part_size=1024 * 1024, | |
| ) | |
| try: | |
| return await parser.parse() | |
| except HTTPException: | |
| for spool in parser._files_to_close_on_error: | |
| spool.close() | |
| raise | |
| except MultiPartException as exc: | |
| raise HTTPException(status_code=400, detail=exc.message) from None | |
| except KeyError: | |
| # Starlette indexes Content-Type before it reports a multipart error. | |
| raise HTTPException(status_code=400, detail="A multipart upload is required") from None | |
| async def _write_upload_to_disk( | |
| uploaded_file: UploadFile, | |
| target_path: Path, | |
| max_bytes: int, | |
| ) -> None: | |
| chunk_size = 1024 * 1024 | |
| written = 0 | |
| try: | |
| async with aiofiles.open(target_path, "wb") as destination: | |
| while True: | |
| chunk = await uploaded_file.read(chunk_size) | |
| if not chunk: | |
| break | |
| written += len(chunk) | |
| if written > max_bytes: | |
| raise HTTPException( | |
| status_code=413, | |
| detail=upload_too_large_detail(max_bytes), | |
| ) | |
| await destination.write(chunk) | |
| except Exception: | |
| if target_path.exists(): | |
| target_path.unlink(missing_ok=True) | |
| raise | |
| async def get_available_fonts_route( | |
| request: Request, db: AsyncSession = Depends(get_db) | |
| ): | |
| """Get list of available fonts.""" | |
| try: | |
| user_id = await _get_authenticated_user_id(request, db) | |
| if not FONTS_DIR.exists(): | |
| return {"fonts": [], "message": "Fonts directory not found"} | |
| # Coverage queries launch Fontconfig; keep cold scans off the API loop. | |
| fonts = await asyncio.to_thread(list_available_fonts, user_id=user_id) | |
| logger.info(f"Found {len(fonts)} available fonts") | |
| return {"fonts": fonts} | |
| except HTTPException: | |
| raise | |
| except Exception as e: | |
| logger.error(f"Error retrieving fonts: {str(e)}") | |
| raise HTTPException(status_code=500, detail=f"Error retrieving fonts: {str(e)}") | |
| async def get_font_file( | |
| font_name: str, | |
| request: Request, | |
| db: AsyncSession = Depends(get_db, scope="function"), | |
| ): | |
| """Serve a specific font file.""" | |
| try: | |
| user_id = await _get_authenticated_user_id(request, db) | |
| font_path = find_font_path(font_name, user_id=user_id) | |
| if not font_path: | |
| raise HTTPException(status_code=404, detail="Font not found") | |
| media_type = "font/ttf" if font_path.suffix.lower() == ".ttf" else "font/otf" | |
| return FileResponse( | |
| path=str(font_path), | |
| media_type=media_type, | |
| headers={ | |
| "Cache-Control": "private, no-cache", | |
| "Access-Control-Allow-Origin": "*", | |
| }, | |
| ) | |
| except HTTPException: | |
| raise | |
| except Exception as e: | |
| logger.error(f"Error serving font {font_name}: {str(e)}") | |
| raise HTTPException(status_code=500, detail=f"Error serving font: {str(e)}") | |
| async def upload_font( | |
| request: Request, | |
| uploaded_file: UploadFile = File(..., alias="file"), | |
| db: AsyncSession = Depends(get_db), | |
| ): | |
| """Upload a custom .ttf/.otf font so it appears in the font picker.""" | |
| try: | |
| user_id = await _get_authenticated_user_id(request, db) | |
| billing_service = BillingService(db) | |
| summary = await billing_service.get_usage_summary(user_id) | |
| paid_access = not summary.get("monetization_enabled") or grants_paid_access( | |
| summary.get("plan"), | |
| summary.get("subscription_status"), | |
| summary.get("subscription_provider"), | |
| ) | |
| if not paid_access: | |
| raise HTTPException( | |
| status_code=403, | |
| detail="Custom font uploads are available for paid plans only", | |
| ) | |
| if not uploaded_file.filename: | |
| raise HTTPException(status_code=400, detail="Missing file name") | |
| uploaded_filename = uploaded_file.filename or "font.ttf" | |
| extension = Path(uploaded_filename).suffix.lower() | |
| if extension not in SUPPORTED_FONT_EXTENSIONS: | |
| raise HTTPException( | |
| status_code=400, detail="Only .ttf and .otf fonts are supported" | |
| ) | |
| user_fonts_dir = get_user_fonts_dir(user_id) | |
| user_fonts_dir.mkdir(parents=True, exist_ok=True) | |
| original_stem = sanitize_font_stem(uploaded_filename) | |
| stored_stem = build_user_font_stem(user_id, original_stem) | |
| target_path = user_fonts_dir / f"{stored_stem}{extension}" | |
| suffix = 2 | |
| while target_path.exists(): | |
| target_path = user_fonts_dir / f"{stored_stem}-{suffix}{extension}" | |
| suffix += 1 | |
| await _write_upload_to_disk(uploaded_file, target_path, MAX_FONT_UPLOAD_BYTES) | |
| logger.info(f"Uploaded font: {target_path.name}") | |
| return { | |
| "font": { | |
| "name": target_path.stem, | |
| "display_name": original_stem.replace("-", " ") | |
| .replace("_", " ") | |
| .title(), | |
| "filename": target_path.name, | |
| "format": extension.lstrip("."), | |
| "scope": "user", | |
| }, | |
| "message": "Font uploaded successfully", | |
| } | |
| except HTTPException: | |
| raise | |
| except ValueError as e: | |
| raise HTTPException(status_code=400, detail=str(e)) | |
| except Exception as e: | |
| logger.error(f"Error uploading font: {str(e)}") | |
| raise HTTPException(status_code=500, detail=f"Error uploading font: {str(e)}") | |
| async def delete_font( | |
| font_name: str, request: Request, db: AsyncSession = Depends(get_db) | |
| ): | |
| """Delete a custom font owned by the authenticated user.""" | |
| try: | |
| user_id = await _get_authenticated_user_id(request, db) | |
| font_path = find_user_font_path(font_name, user_id) | |
| if font_path is None: | |
| if find_font_path(font_name) is not None: | |
| raise HTTPException( | |
| status_code=403, detail="Bundled system fonts cannot be deleted" | |
| ) | |
| raise HTTPException(status_code=404, detail="Custom font not found") | |
| deleted_name = font_path.stem | |
| font_path.unlink(missing_ok=True) | |
| logger.info("Deleted custom font %s for user %s", font_path.name, user_id) | |
| return {"font_name": deleted_name, "message": "Font deleted successfully"} | |
| except HTTPException: | |
| raise | |
| except Exception as e: | |
| logger.error("Error deleting font %s: %s", font_name, e) | |
| raise HTTPException(status_code=500, detail=f"Error deleting font: {str(e)}") | |
| async def get_available_transitions(): | |
| """Get list of available transition effects.""" | |
| try: | |
| from ...video_utils import get_available_transitions | |
| transitions = get_available_transitions() | |
| transition_info = [] | |
| for transition_path in transitions: | |
| transition_file = Path(transition_path) | |
| transition_info.append( | |
| { | |
| "name": transition_file.stem, | |
| "display_name": transition_file.stem.replace("_", " ") | |
| .replace("-", " ") | |
| .title(), | |
| "file_path": transition_path, | |
| } | |
| ) | |
| logger.info(f"Found {len(transition_info)} available transitions") | |
| return {"transitions": transition_info} | |
| except Exception as e: | |
| logger.error(f"Error retrieving transitions: {str(e)}") | |
| raise HTTPException( | |
| status_code=500, detail=f"Error retrieving transitions: {str(e)}" | |
| ) | |
| async def get_caption_templates(): | |
| """Get available caption templates. | |
| Returns a stable default list if optional template module is unavailable. | |
| """ | |
| default_templates = [ | |
| { | |
| "id": "default", | |
| "name": "Default", | |
| "description": "Clean subtitle style", | |
| "animation": "none", | |
| "font_family": "TikTokSans-Regular", | |
| "font_size": 24, | |
| "font_color": "#FFFFFF", | |
| } | |
| ] | |
| try: | |
| from ...caption_templates import get_template_info | |
| templates = get_template_info() | |
| return {"templates": templates or default_templates} | |
| except Exception: | |
| return {"templates": default_templates} | |
| async def get_broll_status(): | |
| """Return whether B-roll integrations are configured.""" | |
| config = get_config() | |
| return { | |
| "configured": bool(config.pexels_api_key), | |
| "provider": "pexels" if config.pexels_api_key else None, | |
| } | |
| def _idempotency_key(request: Request) -> str | None: | |
| raw = request.headers.get("idempotency-key") | |
| if raw is None or not raw.strip(): | |
| return None | |
| key = raw.strip() | |
| if not re.fullmatch(r"[A-Za-z0-9._:-]{8,200}", key): | |
| raise HTTPException(status_code=400, detail="Invalid idempotency key") | |
| return key | |
| async def upload_video(request: Request, db: AsyncSession = Depends(get_db)): | |
| """Store an uploaded video and respond once the bytes are durable locally. | |
| The same bytes (or the same Idempotency-Key) reuse the stored file. | |
| Object storage copy and 1080p scaling both happen after this response. | |
| """ | |
| published: Path | None = None | |
| keep_published = False | |
| form_data = None | |
| try: | |
| user_id = await _get_authenticated_user_id(request, db) | |
| config = get_config() | |
| max_bytes = config.max_video_upload_bytes | |
| idempotency_key = _idempotency_key(request) | |
| form_data = await _read_limited_upload_form(request, max_bytes) | |
| video_file = cast(Any, form_data.get("video")) | |
| if not getattr(video_file, "filename", None) or not hasattr(video_file, "read"): | |
| raise HTTPException(status_code=400, detail="No video file provided") | |
| upload = cast(UploadFile, video_file) | |
| uploads_dir = Path(config.temp_dir) / "uploads" | |
| extension = MediaStorage.video_extension( | |
| upload.filename or "upload.mp4", | |
| upload.content_type or "", | |
| ) | |
| unique_filename = f"{uuid.uuid4()}{extension}" | |
| published = uploads_dir / unique_filename | |
| video_path = f"upload://{unique_filename}" | |
| digest, size = await publish_spooled_upload(upload, published, max_bytes) | |
| video_path, created = await claim_stored_upload( | |
| db, user_id, size, digest, video_path, idempotency_key, config, | |
| ) | |
| if not created and published is not None: | |
| published.unlink(missing_ok=True) | |
| published = None | |
| keep_published = True | |
| logger.info("Video stored as %s", video_path.removeprefix("upload://")) | |
| payload = {"message": "Video uploaded successfully", "video_path": video_path} | |
| if created and MediaStorage().enabled: | |
| return JSONResponse( | |
| payload, | |
| background=BackgroundTask(persist_upload_after_response, video_path), | |
| ) | |
| return payload | |
| except UploadRejected as exc: | |
| raise HTTPException(status_code=exc.status_code, detail=exc.detail) from None | |
| except UploadIdempotencyConflict as exc: | |
| raise HTTPException(status_code=409, detail=str(exc)) from None | |
| except UploadLimitReached as exc: | |
| raise HTTPException(429, str(exc)) from None | |
| except HTTPException: | |
| raise | |
| except Exception as e: | |
| logger.error("Error uploading video: %s", e) | |
| raise HTTPException(status_code=500, detail=f"Error uploading video: {str(e)}") | |
| finally: | |
| if form_data is not None: | |
| await form_data.close() | |
| if published is not None and not keep_published: | |
| published.unlink(missing_ok=True) | |
| async def authorize_upload(request: Request, db: AsyncSession = Depends(get_db)): | |
| user_id = await _get_authenticated_user_id(request, db) | |
| storage = MediaStorage() | |
| if not storage.enabled: | |
| return {"storage": "local"} | |
| try: | |
| payload = await request.json() | |
| size = payload.get("size") | |
| if not isinstance(size, int) or isinstance(size, bool): | |
| raise ValueError("A file size is required") | |
| filename = payload.get("filename") | |
| if not isinstance(filename, str): | |
| raise ValueError("A filename is required") | |
| content_type = payload.get("contentType") or "application/octet-stream" | |
| if not isinstance(content_type, str) or len(content_type) > 128: | |
| raise ValueError("Invalid content type") | |
| session_id = await reserve_upload(db, user_id, size, storage.config) | |
| authorization = {} | |
| def begin(): | |
| # Retain the SDK result even if cancellation prevents delivery of | |
| # the thread result to the awaiting handler. | |
| authorization.update(storage.begin_upload( | |
| user_id, filename, size, content_type, authorization=authorization)) | |
| return authorization | |
| try: | |
| await run_in_thread(begin) | |
| await bind_upload(db, session_id, authorization) | |
| return authorization | |
| except BaseException: | |
| await db.rollback() | |
| if authorization: | |
| try: | |
| await run_in_thread(storage.abort_upload, user_id, authorization) | |
| except STORAGE_ERRORS: | |
| await bind_upload(db, session_id, authorization) | |
| raise | |
| await finish_upload(db, session_id, 'aborted') | |
| raise | |
| except UploadLimitReached as exc: | |
| raise HTTPException(429, str(exc)) from None | |
| except (ValueError, TypeError, AttributeError) as exc: | |
| raise HTTPException(400, str(exc)) from None | |
| async def refresh_upload_parts(request: Request, db: AsyncSession = Depends(get_db)): | |
| user_id = await _get_authenticated_user_id(request, db) | |
| try: | |
| payload = await request.json() | |
| session = await get_upload(db, user_id, payload) | |
| return await run_in_thread(MediaStorage().refresh_upload_parts, user_id, payload, size=session.size) | |
| except (ValueError, TypeError, AttributeError, KeyError) as exc: | |
| raise HTTPException(400, str(exc)) from None | |
| async def complete_upload(request: Request, db: AsyncSession = Depends(get_db)): | |
| user_id = await _get_authenticated_user_id(request, db) | |
| try: | |
| payload = await request.json() | |
| session = await get_upload(db, user_id, payload) | |
| result = await run_in_thread(MediaStorage().complete_upload, user_id, payload, size=session.size) | |
| await finish_upload(db, session.id, 'completed') | |
| return result | |
| except (ValueError, TypeError, AttributeError, KeyError) as exc: | |
| raise HTTPException(400, str(exc)) from None | |
| async def abort_upload(request: Request, db: AsyncSession = Depends(get_db)): | |
| user_id = await _get_authenticated_user_id(request, db) | |
| try: | |
| payload = await request.json() | |
| # Unknown pre-upgrade uploads can still be aborted, but never refreshed. | |
| try: | |
| session = await get_upload(db, user_id, payload, for_abort=True) | |
| except ValueError: | |
| session = None | |
| from botocore.exceptions import ClientError | |
| try: | |
| await run_in_thread(MediaStorage().abort_upload, user_id, payload) | |
| except ClientError as exc: | |
| if exc.response['Error']['Code'] != 'NoSuchUpload': | |
| raise | |
| if session: | |
| await finish_upload(db, session.id, 'aborted') | |
| return {"message": "Upload cancelled"} | |
| except (ValueError, TypeError, AttributeError, KeyError) as exc: | |
| raise HTTPException(400, str(exc)) from None | |