From 79368e7e84466799c174ec1db3fba624ff606138 Mon Sep 17 00:00:00 2001 From: MingxuanGame Date: Wed, 13 Aug 2025 08:34:17 +0000 Subject: [PATCH] fix(private-api): catch exception when opening image --- app/router/private/avatar.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/app/router/private/avatar.py b/app/router/private/avatar.py index 1e1a811..99d942f 100644 --- a/app/router/private/avatar.py +++ b/app/router/private/avatar.py @@ -44,13 +44,16 @@ async def upload_avatar( raise HTTPException(status_code=400, detail="File size exceeds 5MB limit") elif len(content) == 0: raise HTTPException(status_code=400, detail="File cannot be empty") - with Image.open(BytesIO(content)) as img: - if img.format not in ["PNG", "JPEG", "GIF"]: - raise HTTPException(status_code=400, detail="Invalid image format") - if img.size[0] > 256 or img.size[1] > 256: - raise HTTPException( - status_code=400, detail="Image size exceeds 256x256 pixels" - ) + try: + with Image.open(BytesIO(content)) as img: + if img.format not in ["PNG", "JPEG", "GIF"]: + raise HTTPException(status_code=400, detail="Invalid image format") + if img.size[0] > 256 or img.size[1] > 256: + raise HTTPException( + status_code=400, detail="Image size exceeds 256x256 pixels" + ) + except Exception as e: + raise HTTPException(status_code=400, detail=f"Error processing image: {e}") filehash = hashlib.sha256(content).hexdigest() storage_path = f"avatars/{current_user.id}_{filehash}.png"