Spaces:
Building
Building
from fastapi import APIRouter, UploadFile, Form, HTTPException, Depends | |
from sqlalchemy.ext.asyncio import AsyncSession | |
from sqlalchemy.future import select | |
from app.database import get_db | |
from app.models import VideoUpload | |
from .utils.s3 import upload_to_s3 | |
import uuid | |
import os | |
router = APIRouter() | |
# Accept any video file | |
ALLOWED_VIDEO_MIME_TYPES = { | |
"video/mp4", | |
"video/x-matroska", # mkv | |
"video/quicktime", # mov | |
"video/x-msvideo", # avi | |
"video/webm", | |
"video/mpeg", | |
} | |
async def upload_video( | |
user_id: int = Form(...), | |
file: UploadFile = Form(...), | |
db: AsyncSession = Depends(get_db), | |
): | |
if file.content_type not in ALLOWED_VIDEO_MIME_TYPES: | |
raise HTTPException( | |
status_code=400, detail=f"Unsupported file type: {file.content_type}" | |
) | |
try: | |
uid = str(uuid.uuid4()) | |
s3_key = f"videos/{uid}/{file.filename}" | |
video_url = upload_to_s3(file, s3_key) | |
new_video = VideoUpload( | |
user_id=user_id, | |
video_url=video_url, | |
pdf_url="", # will be set after analysis | |
status="pending", | |
) | |
db.add(new_video) | |
await db.commit() | |
await db.refresh(new_video) | |
return {"status": "uploaded", "video_url": video_url, "video_id": new_video.id} | |
except Exception as e: | |
await db.rollback() | |
raise HTTPException(status_code=500, detail=str(e)) | |