From 2e642ec7439aab1848e21813ba76d978867edc66 Mon Sep 17 00:00:00 2001
From: Ben Sturmfels <ben@sturm.com.au>
Date: Fri, 1 Nov 2019 17:58:33 +1100
Subject: [PATCH] Identify image format using Imaging library, rather than by
file extension.
---
mediagoblin/media_types/image/processing.py | 24 ++++++++++-----------
1 file changed, 11 insertions(+), 13 deletions(-)
diff --git a/mediagoblin/media_types/image/processing.py b/mediagoblin/media_types/image/processing.py
index 7ddf3f35..d2397348 100644
a
|
b
|
def resize_image(entry, resized, keyname, target_name, new_size,
|
77 | 77 | |
78 | 78 | # Copy the new file to the conversion subdir, then remotely. |
79 | 79 | tmp_resized_filename = os.path.join(workdir, target_name) |
80 | | resized.save(tmp_resized_filename, quality=quality) |
| 80 | # TODO: Currently does not add a file extension if one wasn't provided in |
| 81 | # the upload, eg. myfile.medium. The "format" parameter is required below |
| 82 | # for this reason. Should probably add a default extension. |
| 83 | resized.save(tmp_resized_filename, format=resized.format, quality=quality) |
81 | 84 | store_public(entry, keyname, tmp_resized_filename, target_name) |
82 | 85 | |
83 | 86 | # store the thumb/medium info |
… |
… |
def _skip_resizing(entry, keyname, size, quality, filter):
|
150 | 153 | return skip |
151 | 154 | |
152 | 155 | |
153 | | SUPPORTED_FILETYPES = ['png', 'gif', 'jpg', 'jpeg', 'tiff'] |
154 | | |
155 | | |
156 | 156 | def sniff_handler(media_file, filename): |
157 | 157 | _log.info('Sniffing {0}'.format(MEDIA_TYPE)) |
158 | | name, ext = os.path.splitext(filename) |
159 | | clean_ext = ext[1:].lower() # Strip the . from ext and make lowercase |
160 | 158 | |
161 | | if clean_ext in SUPPORTED_FILETYPES: |
162 | | _log.info('Found file extension in supported filetypes') |
163 | | return MEDIA_TYPE |
| 159 | try: |
| 160 | Image.open(media_file) |
| 161 | except: |
| 162 | _log.debug('Media present, could not be opened by imaging library') |
| 163 | return None |
164 | 164 | else: |
165 | | _log.debug('Media present, extension not found in {0}'.format( |
166 | | SUPPORTED_FILETYPES)) |
167 | | |
168 | | return None |
| 165 | _log.info('Media was identified by imaging library') |
| 166 | return MEDIA_TYPE |
169 | 167 | |
170 | 168 | |
171 | 169 | class CommonImageProcessor(MediaProcessor): |