Files
home_hub/backend/app/api/media.py
T
2026-05-24 05:05:37 +02:00

33 lines
1.1 KiB
Python

from fastapi import APIRouter, UploadFile, File, Query, HTTPException
from fastapi.responses import Response
from app.schemas.media import MediaUploadResponse
from app.services.media import save_image, save_audio, delete_media, ALLOWED_IMAGE_TYPES, ALLOWED_AUDIO_TYPES
router = APIRouter()
@router.post("/upload", response_model=MediaUploadResponse)
async def upload_media(
file: UploadFile = File(...),
context: str = Query(default="note", pattern="^(product|note|attachment)$"),
):
if file.content_type in ALLOWED_IMAGE_TYPES:
result = await save_image(file, context=context)
elif file.content_type in ALLOWED_AUDIO_TYPES:
result = await save_audio(file)
else:
raise HTTPException(status_code=400, detail=f"Type de fichier non supporté : {file.content_type}")
return MediaUploadResponse(**result)
@router.delete("/{file_id}", status_code=204)
async def delete_media_endpoint(
file_id: str,
file_path: str = Query(...),
thumbnail_path: str | None = Query(default=None),
):
delete_media(file_id=file_id, file_path=file_path, thumbnail_path=thumbnail_path)
return Response(status_code=204)