fix(private-api): catch exception when opening image
This commit is contained in:
@@ -44,13 +44,16 @@ 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")
|
||||||
with Image.open(BytesIO(content)) as img:
|
try:
|
||||||
if img.format not in ["PNG", "JPEG", "GIF"]:
|
with Image.open(BytesIO(content)) as img:
|
||||||
raise HTTPException(status_code=400, detail="Invalid image format")
|
if img.format not in ["PNG", "JPEG", "GIF"]:
|
||||||
if img.size[0] > 256 or img.size[1] > 256:
|
raise HTTPException(status_code=400, detail="Invalid image format")
|
||||||
raise HTTPException(
|
if img.size[0] > 256 or img.size[1] > 256:
|
||||||
status_code=400, detail="Image size exceeds 256x256 pixels"
|
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()
|
filehash = hashlib.sha256(content).hexdigest()
|
||||||
storage_path = f"avatars/{current_user.id}_{filehash}.png"
|
storage_path = f"avatars/{current_user.id}_{filehash}.png"
|
||||||
|
|||||||
Reference in New Issue
Block a user