diff --git a/app/api/profile.py b/app/api/profile.py index eb3b0e5b..a12324d8 100644 --- a/app/api/profile.py +++ b/app/api/profile.py @@ -231,6 +231,14 @@ async def upload_avatar( detail=f"Unsupported image type '{content_type}'. Allowed: JPEG, PNG, GIF, WebP.", ) + # Check declared size first (available when the client sends a Content-Length header) + if file.size is not None and file.size > _MAX_AVATAR_BYTES: + raise HTTPException( + status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, + detail="Avatar image must be 2 MB or smaller.", + ) + + # Read up to one byte past the limit so we can detect oversized uploads raw = await file.read(_MAX_AVATAR_BYTES + 1) if len(raw) > _MAX_AVATAR_BYTES: raise HTTPException( diff --git a/frontend/templates/profile.html b/frontend/templates/profile.html index 75c04d39..cd506cfb 100644 --- a/frontend/templates/profile.html +++ b/frontend/templates/profile.html @@ -77,8 +77,8 @@

- Upload a JPEG, PNG, GIF, or WebP image up to 2 MB. - If no custom picture is set, your Gravatar is shown. + Upload a JPEG, PNG, GIF, or WebP image up to 2 MB. + If no custom picture is set, your Gravatar is shown.