From 348430838e03517d215d2b0700106867ebc74202 Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 9 Dec 2018 19:21:06 -0300 Subject: [PATCH] attachments: add sanitizer for pillow format kwarg --- litecord/blueprints/attachments.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/litecord/blueprints/attachments.py b/litecord/blueprints/attachments.py index 2ece3e0..28a1582 100644 --- a/litecord/blueprints/attachments.py +++ b/litecord/blueprints/attachments.py @@ -47,6 +47,22 @@ async def _resize_gif(attach_id: int, resized_path: Path, return str(resized_path) +FORMAT_HARDCODE = { + 'jpg': 'jpeg' + 'jpe': 'jpeg' +} + + +def to_format(ext: str) -> str: + """Return a proper format string for Pillow consumption.""" + ext = ext.lower() + + if ext in FORMAT_HARDCODE: + return FORMAT_HARDCODE[ext] + + return ext + + async def _resize(image, attach_id: int, ext: str, width: int, height: int) -> str: """Resize an image.""" @@ -70,7 +86,7 @@ async def _resize(image, attach_id: int, ext: str, # NOTE: this is the same resize mode for icons. resized = image.resize((width, height), resample=Image.LANCZOS) - resized.save(resized_path_s, format=ext) + resized.save(resized_path_s, format=to_format(ext)) return resized_path_s