fix(media): upload photo — taille, formats et nginx

- nginx : client_max_body_size 15m (photos smartphone > 1 Mo rejetées silencieusement)
- backend : redimensionnement original à 500×500 max (aspect ratio conservé) avant sauvegarde WEBP
- backend : thumbnail généré depuis l'image déjà redimensionnée (économie mémoire)
- backend : formats acceptés étendus — image/heic, image/heif, image/jpg
- backend : normalisation content-type en lowercase (robustesse navigateurs)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-25 13:29:45 +02:00
parent 1210d4f9f3
commit 3dbd554eeb
3 changed files with 24 additions and 11 deletions
+4 -3
View File
@@ -12,12 +12,13 @@ async def upload_media(
file: UploadFile = File(...),
context: str = Query(default="note", pattern="^(product|note|attachment)$"),
):
if file.content_type in ALLOWED_IMAGE_TYPES:
content_type = (file.content_type or "").lower()
if content_type in ALLOWED_IMAGE_TYPES:
result = await save_image(file, context=context)
elif file.content_type in ALLOWED_AUDIO_TYPES:
elif 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}")
raise HTTPException(status_code=400, detail=f"Type de fichier non supporté : {content_type}")
return MediaUploadResponse(**result)
+18 -8
View File
@@ -9,9 +9,14 @@ from app.core.config import settings
UPLOAD_DIR: Path = settings.upload_path
ALLOWED_IMAGE_TYPES = {"image/jpeg", "image/png", "image/webp", "image/svg+xml"}
ALLOWED_IMAGE_TYPES = {
"image/jpeg", "image/jpg", "image/png", "image/webp", "image/svg+xml",
"image/heic", "image/heif",
}
ALLOWED_AUDIO_TYPES = {"audio/webm", "audio/mp4"}
MAX_ORIG_SIZE = (500, 500)
THUMBNAIL_SIZES = {
"product": (150, 150),
"note": (300, 300),
@@ -20,17 +25,18 @@ THUMBNAIL_SIZES = {
async def save_image(file: UploadFile, context: str = "note") -> dict:
if file.content_type not in ALLOWED_IMAGE_TYPES:
raise HTTPException(status_code=400, detail=f"Format non supporté : {file.content_type}")
content_type = (file.content_type or "").lower()
if content_type not in ALLOWED_IMAGE_TYPES:
raise HTTPException(status_code=400, detail=f"Format non supporté : {content_type}")
file_id = str(uuid.uuid4())
content = await file.read()
orig_dir = UPLOAD_DIR / "images" / "originals"
orig_dir.mkdir(parents=True, exist_ok=True)
orig_path = orig_dir / f"{file_id}.webp"
if file.content_type == "image/svg+xml":
if content_type == "image/svg+xml":
orig_path = orig_dir / f"{file_id}.svg"
orig_path.write_bytes(content)
return {
"file_id": file_id,
@@ -39,16 +45,20 @@ async def save_image(file: UploadFile, context: str = "note") -> dict:
"file_type": "image",
}
orig_path = orig_dir / f"{file_id}.webp"
img = Image.open(io.BytesIO(content)).convert("RGB")
# Redimensionne l'original à 500×500 max en conservant l'aspect ratio
img.thumbnail(MAX_ORIG_SIZE, Image.LANCZOS)
img.save(orig_path, "WEBP", quality=85)
thumb_dir = UPLOAD_DIR / "images" / "thumbnails"
thumb_dir.mkdir(parents=True, exist_ok=True)
thumb_path = thumb_dir / f"{file_id}_thumb.webp"
size = THUMBNAIL_SIZES.get(context, (300, 300))
img_thumb = Image.open(io.BytesIO(content)).convert("RGB")
img_thumb.thumbnail(size, Image.LANCZOS)
thumb_size = THUMBNAIL_SIZES.get(context, (300, 300))
img_thumb = img.copy()
img_thumb.thumbnail(thumb_size, Image.LANCZOS)
img_thumb.save(thumb_path, "WEBP", quality=80)
return {
+2
View File
@@ -4,6 +4,8 @@ server {
root /usr/share/nginx/html;
index index.html;
client_max_body_size 15m;
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml image/svg+xml;