From 830b4c7d4ed8536b699a11879503da6b28d3287f Mon Sep 17 00:00:00 2001 From: Giovanni Date: Mon, 5 Oct 2026 21:42:40 +0200 Subject: [PATCH] implement S3 garage on the backend --- apps/backend/app/routers/media.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/apps/backend/app/routers/media.py b/apps/backend/app/routers/media.py index c963671..a3be92e 100644 --- a/apps/backend/app/routers/media.py +++ b/apps/backend/app/routers/media.py @@ -1,16 +1,20 @@ +from urllib.parse import unquote from botocore.exceptions import ClientError from fastapi import APIRouter, HTTPException from fastapi.responses import StreamingResponse from app.s3 import BUCKET_NAME, s3_client -router = APIRouter(prefix="/media", tags=["Media"]) +router = APIRouter(prefix="/media", tags=["media"]) @router.get("/{file_path:path}") async def get_media(file_path: str): + decoded_path = unquote(file_path) + clean_key = decoded_path.lstrip("/") + try: - s3_object = s3_client.get_object(Bucket=BUCKET_NAME, Key=file_path) + s3_object = s3_client.get_object(Bucket=BUCKET_NAME, Key=clean_key) return StreamingResponse( content=s3_object["Body"].iter_chunks(), @@ -18,6 +22,12 @@ async def get_media(file_path: str): headers={"Cache-Control": "public, max-age=86400"}, ) except ClientError as e: - if e.response["Error"]["Code"] in ["NoSuchKey", "404"]: - raise HTTPException(status_code=404, detail="Fichier introuvable") - raise HTTPException(status_code=500, detail="Erreur serveur S3") \ No newline at end of file + error_code = e.response["Error"]["Code"] + if error_code in ["NoSuchKey", "404"]: + raise HTTPException( + status_code=404, + detail=f"Fichier introuvable sur S3 pour la clé : '{clean_key}'", + ) + raise HTTPException( + status_code=500, detail=f"Erreur serveur S3: {str(e)}" + ) \ No newline at end of file