diff --git a/matemat/webserver/pagelets/admin.py b/matemat/webserver/pagelets/admin.py index d1866b2..0095b67 100644 --- a/matemat/webserver/pagelets/admin.py +++ b/matemat/webserver/pagelets/admin.py @@ -2,6 +2,8 @@ from typing import Any, Dict, Union import os import magic +from io import BytesIO +from PIL import Image from matemat.webserver import pagelet, RequestArguments, PageletResponse, RedirectResponse, TemplateResponse from matemat.db import MatematDatabase @@ -124,16 +126,20 @@ def handle_change(args: RequestArguments, user: User, db: MatematDatabase, confi return # Detect the MIME type filemagic: magic.FileMagic = magic.detect_from_content(avatar) - # Currently, only image/png is supported, don't process any other formats - if filemagic.mime_type != 'image/png': - # TODO: Optionally convert to png + if not filemagic.mime_type.startswith('image/'): return # Create the absolute path of the upload directory abspath: str = os.path.join(os.path.abspath(config['UploadDir']), 'thumbnails/users/') os.makedirs(abspath, exist_ok=True) - # Write the image to the file - with open(os.path.join(abspath, f'{user.id}.png'), 'wb') as f: - f.write(avatar) + try: + # Parse the image data + image: Image = Image.open(BytesIO(avatar)) + # Resize the image to 150x150 + image.thumbnail((150, 150), Image.LANCZOS) + # Write the image to the file + image.save(os.path.join(abspath, f'{user.id}.png'), 'PNG') + except OSError: + return except UnicodeDecodeError: raise ValueError('an argument not a string') @@ -182,22 +188,26 @@ def handle_admin_change(args: RequestArguments, db: MatematDatabase, config: Dic # If a new product image was uploaded, process it if 'image' in args: # Read the raw image data from the request - image = bytes(args.image) + avatar = bytes(args.image) # Only process the image, if its size is more than zero. Zero size means no new image was uploaded - if len(image) == 0: + if len(avatar) == 0: return # Detect the MIME type - filemagic: magic.FileMagic = magic.detect_from_content(image) - # Currently, only image/png is supported, don't process any other formats - if filemagic.mime_type != 'image/png': - # TODO: Optionally convert to png + filemagic: magic.FileMagic = magic.detect_from_content(avatar) + if not filemagic.mime_type.startswith('image/'): return # Create the absolute path of the upload directory abspath: str = os.path.join(os.path.abspath(config['UploadDir']), 'thumbnails/products/') os.makedirs(abspath, exist_ok=True) - # Write the image to the file - with open(os.path.join(abspath, f'{newproduct.id}.png'), 'wb') as f: - f.write(image) + try: + # Parse the image data + image: Image = Image.open(BytesIO(avatar)) + # Resize the image to 150x150 + image.thumbnail((150, 150), Image.LANCZOS) + # Write the image to the file + image.save(os.path.join(abspath, f'{newproduct.id}.png'), 'PNG') + except OSError: + return # The user requested to restock a product elif change == 'restock': diff --git a/matemat/webserver/pagelets/modproduct.py b/matemat/webserver/pagelets/modproduct.py index ef6acbd..7ba9c3e 100644 --- a/matemat/webserver/pagelets/modproduct.py +++ b/matemat/webserver/pagelets/modproduct.py @@ -2,6 +2,8 @@ from typing import Any, Dict, Union import os import magic +from PIL import Image +from io import BytesIO from matemat.webserver import pagelet, RequestArguments, PageletResponse, RedirectResponse, TemplateResponse from matemat.db import MatematDatabase @@ -101,19 +103,23 @@ def handle_change(args: RequestArguments, product: Product, db: MatematDatabase, # If a new product image was uploaded, process it if 'image' in args: # Read the raw image data from the request - image = bytes(args.image) + avatar = bytes(args.image) # Only process the image, if its size is more than zero. Zero size means no new image was uploaded - if len(image) == 0: + if len(avatar) == 0: return # Detect the MIME type - filemagic: magic.FileMagic = magic.detect_from_content(image) - # Currently, only image/png is supported, don't process any other formats - if filemagic.mime_type != 'image/png': - # TODO: Optionally convert to png + filemagic: magic.FileMagic = magic.detect_from_content(avatar) + if not filemagic.mime_type.startswith('image/'): return # Create the absolute path of the upload directory abspath: str = os.path.join(os.path.abspath(config['UploadDir']), 'thumbnails/products/') os.makedirs(abspath, exist_ok=True) - # Write the image to the file - with open(os.path.join(abspath, f'{product.id}.png'), 'wb') as f: - f.write(image) + try: + # Parse the image data + image: Image = Image.open(BytesIO(avatar)) + # Resize the image to 150x150 + image.thumbnail((150, 150), Image.LANCZOS) + # Write the image to the file + image.save(os.path.join(abspath, f'{product.id}.png'), 'PNG') + except OSError: + return diff --git a/matemat/webserver/pagelets/moduser.py b/matemat/webserver/pagelets/moduser.py index 8be086c..94bfa84 100644 --- a/matemat/webserver/pagelets/moduser.py +++ b/matemat/webserver/pagelets/moduser.py @@ -2,6 +2,8 @@ from typing import Any, Dict, Optional, Union import os import magic +from PIL import Image +from io import BytesIO from matemat.webserver import pagelet, RequestArguments, PageletResponse, RedirectResponse, TemplateResponse from matemat.db import MatematDatabase @@ -130,13 +132,17 @@ def handle_change(args: RequestArguments, user: User, authuser: User, db: Matema return # Detect the MIME type filemagic: magic.FileMagic = magic.detect_from_content(avatar) - # Currently, only image/png is supported, don't process any other formats - if filemagic.mime_type != 'image/png': - # TODO: Optionally convert to png + if not filemagic.mime_type.startswith('image/'): return # Create the absolute path of the upload directory abspath: str = os.path.join(os.path.abspath(config['UploadDir']), 'thumbnails/users/') os.makedirs(abspath, exist_ok=True) - # Write the image to the file - with open(os.path.join(abspath, f'{user.id}.png'), 'wb') as f: - f.write(avatar) + try: + # Parse the image data + image: Image = Image.open(BytesIO(avatar)) + # Resize the image to 150x150 + image.thumbnail((150, 150), Image.LANCZOS) + # Write the image to the file + image.save(os.path.join(abspath, f'{user.id}.png'), 'PNG') + except OSError: + return diff --git a/templates/admin_all.html b/templates/admin_all.html index b637cc7..d1aa1f2 100644 --- a/templates/admin_all.html +++ b/templates/admin_all.html @@ -34,7 +34,7 @@ Avatar of {{ authuser.name }}
-
+
diff --git a/templates/admin_restricted.html b/templates/admin_restricted.html index 36152c3..6b85a63 100644 --- a/templates/admin_restricted.html +++ b/templates/admin_restricted.html @@ -50,7 +50,7 @@
-
+
diff --git a/templates/modproduct.html b/templates/modproduct.html index 2158139..338df82 100644 --- a/templates/modproduct.html +++ b/templates/modproduct.html @@ -26,7 +26,7 @@
-
+

diff --git a/templates/moduser.html b/templates/moduser.html index 894d198..e40e393 100644 --- a/templates/moduser.html +++ b/templates/moduser.html @@ -44,7 +44,7 @@
-
+