"""Transcode profile management endpoints.""" from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy.orm import Session from pydantic import BaseModel import yaml from privatestudio.web.database import get_db from privatestudio.web.models import User, TranscodeProfile from privatestudio.web.auth import get_current_user router = APIRouter() class ProfileResponse(BaseModel): """Response model for transcode profile.""" id: int name: str profile_definition: str created_at: str class Config: from_attributes = True @router.post("/profiles") def create_profile( name: str, body: str, # Raw YAML body current_user: User = Depends(get_current_user), db: Session = Depends(get_db), ): """ Create a new transcode profile. The request body should contain the YAML profile definition. """ # Validate YAML try: profile_data = yaml.safe_load(body) if not isinstance(profile_data, dict): raise ValueError("Profile definition must be a YAML mapping") if "profiles" not in profile_data: raise ValueError("Profile definition must contain a 'profiles' section") except yaml.YAMLError as e: raise HTTPException(status_code=400, detail=f"Invalid YAML: {e}") # Check if profile name already exists for this user existing = db.query(TranscodeProfile).filter( TranscodeProfile.user_id == current_user.id, TranscodeProfile.name == name, ).first() if existing: raise HTTPException( status_code=status.HTTP_409_CONFLICT, detail=f"Profile '{name}' already exists", ) # Create profile profile = TranscodeProfile( user_id=current_user.id, name=name, profile_definition=body, ) db.add(profile) db.commit() db.refresh(profile) return ProfileResponse( id=profile.id, name=profile.name, profile_definition=profile.profile_definition, created_at=profile.created_at.isoformat(), ) @router.get("/profiles", response_model=list[ProfileResponse]) def list_profiles( current_user: User = Depends(get_current_user), db: Session = Depends(get_db), ): """List all transcode profiles for the current user.""" profiles = db.query(TranscodeProfile).filter( TranscodeProfile.user_id == current_user.id ).all() return [ ProfileResponse( id=p.id, name=p.name, profile_definition=p.profile_definition, created_at=p.created_at.isoformat(), ) for p in profiles ] @router.get("/profiles/{profile_id}", response_model=ProfileResponse) def get_profile( profile_id: int, current_user: User = Depends(get_current_user), db: Session = Depends(get_db), ): """Get a specific transcode profile.""" profile = db.query(TranscodeProfile).filter( TranscodeProfile.id == profile_id, TranscodeProfile.user_id == current_user.id, ).first() if not profile: raise HTTPException(status_code=404, detail="Profile not found") return ProfileResponse( id=profile.id, name=profile.name, profile_definition=profile.profile_definition, created_at=profile.created_at.isoformat(), )