[Plugin] auth_ldap: Use Pillow to convert avatar if installed

This commit is contained in:
Ferdinand Thiessen 2020-11-16 13:35:23 +01:00
parent a270857c41
commit 09c7f4a258
1 changed files with 16 additions and 3 deletions

View File

@ -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