backend: return error 500 instead of zero for images
This commit is contained in:
parent
4921d4bd6f
commit
da077519fe
1 changed files with 6 additions and 5 deletions
|
@ -1,4 +1,5 @@
|
||||||
from PIL import Image, UnidentifiedImageError
|
from PIL import Image, UnidentifiedImageError
|
||||||
|
from fastapi import HTTPException
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import io
|
import io
|
||||||
|
|
||||||
|
@ -13,13 +14,13 @@ def image_normalize(img: bytes) -> bytes:
|
||||||
img.save(byte_arr, format='WEBP')
|
img.save(byte_arr, format='WEBP')
|
||||||
return byte_arr.getvalue()
|
return byte_arr.getvalue()
|
||||||
except UnidentifiedImageError:
|
except UnidentifiedImageError:
|
||||||
return 0
|
raise HTTPException(status_code=500, detail="Image file cannot be readed!")
|
||||||
|
|
||||||
|
|
||||||
def save_image(img: bytes, dir: str, name: str) -> int:
|
def save_image(img: bytes, dir: str, name: str) -> int:
|
||||||
path = asset(dir)
|
path = asset(dir)
|
||||||
path.mkdir(exist_ok=True)
|
path.mkdir(exist_ok=True)
|
||||||
img = image_normalize(img)
|
img = image_normalize(img)
|
||||||
if img:
|
with open(Path(path, name+'.jpg'), "wb") as file:
|
||||||
with open(Path(path, name+'.webp'), "wb") as file:
|
file.write(img)
|
||||||
file.write(img)
|
return len(img)
|
||||||
return len(img)
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue