Skip to content

Commit c04430d

Browse files
committed
Updated env, secured wrong file type
1 parent f935fb7 commit c04430d

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

backend/PyMatcha/routes/api/profile/images.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ def add_image_profile():
2020
is_primary = request.args.get("is_primary", "false") == "true"
2121
# check if the post request has the file part
2222
if "file[]" not in request.files:
23-
raise BadRequestError("No file passed in request")
23+
raise BadRequestError("No file body passed in request form data")
2424
file = request.files["file[]"]
2525
# if user does not select file, browser also
2626
# submit an empty part without filename
2727
if file.filename == "":
28-
raise BadRequestError("No filename passed in request")
28+
raise BadRequestError("No file passed in request")
2929
if file:
3030
if is_primary:
3131
try:
@@ -34,7 +34,10 @@ def add_image_profile():
3434
# That means there was no primary image before
3535
tmp_img = BytesIO()
3636
file.save(tmp_img)
37-
link = upload_image(tmp_img, current_user.username)
37+
try:
38+
link = upload_image(tmp_img, current_user.username)
39+
except BadRequestError as e:
40+
raise e
3841
Image.create(current_user.id, link, is_primary=True)
3942
return SuccessOutput("image", link)
4043
else:
@@ -45,7 +48,10 @@ def add_image_profile():
4548
raise BadRequestError("There's already enough images for this account")
4649
tmp_img = BytesIO()
4750
file.save(tmp_img)
48-
link = upload_image(tmp_img, current_user.username)
51+
try:
52+
link = upload_image(tmp_img, current_user.username)
53+
except BadRequestError as e:
54+
raise e
4955
Image.create(current_user.id, link, is_primary=False)
5056
return SuccessOutput("image", link)
5157
else:

backend/PyMatcha/utils/images.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
from io import BytesIO
33

44
from PIL import Image
5+
from PIL import UnidentifiedImageError
56
from pyimgur import Imgur
7+
from PyMatcha.utils.errors import BadRequestError
68
from PyMatcha.utils.static import IMGUR_CLIENT_ID
79
from PyMatcha.utils.static import IMGUR_CLIENT_SECRET
810

@@ -11,7 +13,10 @@
1113

1214
def upload_image(bytesio_img: BytesIO, username: str):
1315
path = f"{username}.png"
14-
Image.open(bytesio_img).convert("RGB").save(path)
16+
try:
17+
Image.open(bytesio_img).convert("RGB").save(path)
18+
except UnidentifiedImageError:
19+
raise BadRequestError("Wrong file format. This isn't an image")
1520
uploaded_image = imgur_client.upload_image(path=path, title=username)
1621
os.remove(path)
1722
return uploaded_image.link

0 commit comments

Comments
 (0)