]> git.ipfire.org Git - ipfire.org.git/commitdiff
users: Replace deprecated imghdr module with magic/mimetypes
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 6 Jan 2024 15:46:59 +0000 (15:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 6 Jan 2024 15:46:59 +0000 (15:46 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
configure.ac
src/web/users.py

index ae6a9af11894bbe25eb705674d755abf4b842b50..c336ab6835a6ab79a4f61af1e8cdeed9b83368a3 100644 (file)
@@ -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])
index d88051b04eb640236c3eccb3080e215ccc6c8ed6..aaaca82551162f3925e06658c9037cb414e4de66 100644 (file)
@@ -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