39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
import uuid
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from fastapi.responses import Response
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from botocore.exceptions import ClientError
|
|
|
|
from ..auth import verify_token
|
|
from ..database import get_db
|
|
from ..models import Job
|
|
from ..storage import download_audio
|
|
|
|
router = APIRouter(prefix="/media", tags=["media"])
|
|
|
|
|
|
@router.get("/{filename:path}")
|
|
async def get_media_file(
|
|
filename: str,
|
|
db: AsyncSession = Depends(get_db),
|
|
token_data: dict = Depends(verify_token),
|
|
) -> Response:
|
|
user_id = uuid.UUID(token_data["sub"])
|
|
|
|
result = await db.execute(
|
|
select(Job).where(Job.audio_url == filename, Job.user_id == user_id)
|
|
)
|
|
job = result.scalar_one_or_none()
|
|
if job is None:
|
|
raise HTTPException(status_code=404, detail="File not found")
|
|
|
|
try:
|
|
audio_bytes, content_type = download_audio(filename)
|
|
except ClientError as e:
|
|
if e.response["Error"]["Code"] in ("NoSuchKey", "404"):
|
|
raise HTTPException(status_code=404, detail="File not found")
|
|
raise HTTPException(status_code=500, detail="Storage error")
|
|
|
|
return Response(content=audio_bytes, media_type=content_type)
|