Files
privatestudio/privatestudio/web/routes/profiles.py
T
Shaun KrugerandCopilot e59a622c1e Add FastAPI web service for video transcoding management
Implements a complete video transcoding web service with:
- FastAPI framework for REST API
- SQLAlchemy ORM with SQLite database
- API key authentication using SHA256 hashing
- Per-user resource isolation for all assets and jobs
- S3-compatible file upload/download/delete/list interface
- YAML-based API for transcode job requests
- Transcode profile management (stored and inline)
- Database models for Users, APIKeys, Assets, Jobs, and Profiles
- CLI entry point for database initialization and server startup
- Comprehensive integration tests with 100% pass rate
- Deployment documentation with examples

New files:
- privatestudio/web/: Web service package
- privatestudio/web/models.py: SQLAlchemy ORM models
- privatestudio/web/auth.py: API key authentication
- privatestudio/web/database.py: Database configuration
- privatestudio/web/app.py: FastAPI application
- privatestudio/web/s3_interface.py: File storage backend
- privatestudio/web/cli.py: CLI commands
- privatestudio/web/routes/: API endpoint handlers
- tests/test_web.py: Integration tests
- DEPLOYMENT.md: Deployment guide

Updated files:
- pyproject.toml: Added dependencies and CLI entry point

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-22 15:22:01 -07:00

119 lines
3.3 KiB
Python

"""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(),
)