Fix gif pfp uploading

This commit is contained in:
Michał Gdula 2023-06-23 21:58:07 +00:00
parent 961759e40b
commit 193c7d9d07

View file

@ -42,7 +42,7 @@ def settings():
if "file" in request.files and request.files['file'].filename: if "file" in request.files and request.files['file'].filename:
picture = request.files["file"] picture = request.files["file"]
file_ext = picture.filename.split(".")[-1] file_ext = picture.filename.split(".")[-1].lower()
file_name = f"{user.id}.{file_ext}" file_name = f"{user.id}.{file_ext}"
if file_ext not in UPLOAD_EXTENSIONS: if file_ext not in UPLOAD_EXTENSIONS:
@ -51,6 +51,9 @@ def settings():
error.append(f"Picture must be less than {UPLOAD_EXTENSIONS / 1000000}MB!") error.append(f"Picture must be less than {UPLOAD_EXTENSIONS / 1000000}MB!")
image = Image.open(picture.stream) image = Image.open(picture.stream)
# Resizing gifs is more work than it's worth
if file_ext != "gif":
image_x, image_y = image.size image_x, image_y = image.size
image.thumbnail(( image.thumbnail((
min(image_x, UPLOAD_RESOLUTION), min(image_x, UPLOAD_RESOLUTION),
@ -66,7 +69,12 @@ def settings():
os.remove(os.path.join(UPLOAD_DIR, user.picture)) os.remove(os.path.join(UPLOAD_DIR, user.picture))
user.picture = file_name user.picture = file_name
if file_ext == "gif":
image.save(os.path.join(UPLOAD_DIR, file_name), save_all=True)
else:
image.save(os.path.join(UPLOAD_DIR, file_name)) image.save(os.path.join(UPLOAD_DIR, file_name))
image.close() image.close()
if username: if username: