From 6e0fea99815674da6df26568fa4afc3194786538 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 6 Jan 2024 15:46:59 +0000 Subject: [PATCH] users: Replace deprecated imghdr module with magic/mimetypes Signed-off-by: Michael Tremer --- configure.ac | 1 + src/web/users.py | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/configure.ac b/configure.ac index ae6a9af1..c336ab68 100644 --- a/configure.ac +++ b/configure.ac @@ -36,6 +36,7 @@ AX_PYTHON_MODULE([iso3166], [fatal]) AX_PYTHON_MODULE([jsonschema], [fatal]) AX_PYTHON_MODULE([kerberos], [fatal]) AX_PYTHON_MODULE([ldap], [fatal]) +AX_PYTHON_MODULE([magic], [fatal]) AX_PYTHON_MODULE([panoramisk], [fatal]) AX_PYTHON_MODULE([phonenumbers], [fatal]) AX_PYTHON_MODULE([psycopg], [fatal]) diff --git a/src/web/users.py b/src/web/users.py index d88051b0..aaaca825 100644 --- a/src/web/users.py +++ b/src/web/users.py @@ -1,10 +1,11 @@ #!/usr/bin/python import PIL -import imghdr import io import ldap import logging +import magic +import mimetypes import os.path import tornado.web @@ -74,19 +75,18 @@ class AvatarHandler(base.BaseHandler): # Generate a random avatar with only one letter avatar = await self._get_avatar(account, size=size) + m = magic.Magic(mime=True, uncompress=True) + # Guess content type - type = imghdr.what(None, avatar) + mimetype = m.from_buffer(avatar) - # If we could not guess the type, we will try something else - if not type: - # Could this be an SVG file? - if avatar.startswith(b"<"): - type = "svg+xml" + # Fetch the file extension + ext = mimetypes.guess_extension(mimetype) # Set headers about content - self.set_header("Content-Disposition", "inline; filename=\"%s.%s\"" % (account.uid, type)) - if type: - self.set_header("Content-Type", "image/%s" % type) + self.set_header("Content-Disposition", "inline; filename=\"%s%s\"" % (account.uid, ext)) + if mimetype: + self.set_header("Content-Type", mimetype) # Deliver payload self.finish(avatar) @@ -130,7 +130,7 @@ class AvatarHandler(base.BaseHandler): # Determine size of the printed letter w, h = font.getsize(letter) - # Mukta seems to be very broken and the height needs to be corrected + # Prompt seems to be very broken and the height needs to be corrected h //= 0.7 # Draw the letter in the center -- 2.47.3