From bf8cfdf477cf4e79a1eff93f4b7f09be66516b3f Mon Sep 17 00:00:00 2001 From: Leonardo Lazzaro Date: Mon, 14 Mar 2022 20:30:16 +0100 Subject: [PATCH] fix(future-compat): remove imghdr Since PEP 594 was accepted, the standard module imghdr will be deprecated in Python 3.11 and it will be removed in Python 3.13. This PR removed the imghdr and it replaces it with python-magic-bin --- faraday/server/fields.py | 7 ++++--- requirements.txt | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/faraday/server/fields.py b/faraday/server/fields.py index e3b8ae337ae..69be1e9a841 100644 --- a/faraday/server/fields.py +++ b/faraday/server/fields.py @@ -6,7 +6,7 @@ """ import json -import imghdr +import magic from tempfile import SpooledTemporaryFile from PIL import Image @@ -47,8 +47,9 @@ class FaradayUploadedFile(UploadedFile): def process_content(self, content, filename=None, content_type=None): if isinstance(content, str): content = content.encode('utf-8') - image_format = imghdr.what(None, h=content[:32]) - if image_format: + mime_type = magic.from_buffer(content[:32], mime=True) + if "image" in mime_type: + image_format = mime_type.split('/')[-1] content_type = f'image/{image_format}' self.generate_thumbnail(content) return super().process_content( diff --git a/requirements.txt b/requirements.txt index 4c71014c616..3567c527186 100644 --- a/requirements.txt +++ b/requirements.txt @@ -42,3 +42,4 @@ pyotp>=2.6.0 Flask-Limiter Flask-Mail faraday_agent_parameters_types>=1.0.3 +python-magic-bin==0.4.14