From 09c7f4a258baf4ed1ba6e4af1ba97005e0c87029 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Mon, 16 Nov 2020 13:35:23 +0100 Subject: [PATCH] [Plugin] auth_ldap: Use Pillow to convert avatar if installed --- flaschengeist/plugins/auth_ldap/__init__.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/flaschengeist/plugins/auth_ldap/__init__.py b/flaschengeist/plugins/auth_ldap/__init__.py index f421c19..e433fc7 100644 --- a/flaschengeist/plugins/auth_ldap/__init__.py +++ b/flaschengeist/plugins/auth_ldap/__init__.py @@ -1,5 +1,5 @@ """LDAP Authentication Provider Plugin""" - +import io import ssl from typing import Optional from flask_ldapconn import LDAPConn @@ -176,8 +176,21 @@ class AuthLDAP(AuthPlugin): def __save_avatar(self, avatar, user): if avatar.mimetype != "image/jpeg": - raise BadRequest("Unsupported image Format") - # Maybe use Pillow to convert here + # Try converting using Pillow (if installed) + try: + from PIL import Image + image = Image.open(io.BytesIO(avatar.binary)) + image_bytes = io.BytesIO() + image.save(image_bytes, format="JPEG") + avatar.binary = image_bytes.getvalue() + avatar.mimetype = "image/jpeg" + except ImportError: + logger.debug("Pillow not installed for image conversion") + raise BadRequest("Unsupported image format") + except IOError: + logger.debug(f"Could not convert avatar from '{avatar.mimetype}' to JPEG") + raise BadRequest("Unsupported image format") + if self.admin_dn is None: logger.error("admin_dn missing in ldap config!") raise InternalServerError