fix(private-api): catch exception when opening image

This commit is contained in:
MingxuanGame
2025-08-13 08:34:17 +00:00
parent f19aaa6233
commit 79368e7e84

View File

@@ -44,6 +44,7 @@ async def upload_avatar(
raise HTTPException(status_code=400, detail="File size exceeds 5MB limit") raise HTTPException(status_code=400, detail="File size exceeds 5MB limit")
elif len(content) == 0: elif len(content) == 0:
raise HTTPException(status_code=400, detail="File cannot be empty") raise HTTPException(status_code=400, detail="File cannot be empty")
try:
with Image.open(BytesIO(content)) as img: with Image.open(BytesIO(content)) as img:
if img.format not in ["PNG", "JPEG", "GIF"]: if img.format not in ["PNG", "JPEG", "GIF"]:
raise HTTPException(status_code=400, detail="Invalid image format") raise HTTPException(status_code=400, detail="Invalid image format")
@@ -51,6 +52,8 @@ async def upload_avatar(
raise HTTPException( raise HTTPException(
status_code=400, detail="Image size exceeds 256x256 pixels" 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() filehash = hashlib.sha256(content).hexdigest()
storage_path = f"avatars/{current_user.id}_{filehash}.png" storage_path = f"avatars/{current_user.id}_{filehash}.png"