34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
import uuid
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from fastapi.responses import Response
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from botocore.exceptions import ClientError
|
|
|
|
from ..auth import verify_token
|
|
from ..outbound.postgres.database import get_db
|
|
from ..outbound.postgres.repositories.summarise_job_repository import PostgresSummariseJobRepository
|
|
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),
|
|
) -> Response:
|
|
repository = PostgresSummariseJobRepository(db)
|
|
job = await repository.get_by_audio_url(filename)
|
|
|
|
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)
|