From e2b56345498f248ad75d646def13ad068f17226c Mon Sep 17 00:00:00 2001 From: Rodney Ewing Date: Wed, 21 Aug 2013 14:37:44 -0700 Subject: [PATCH] skip image resizing if possible --- mediagoblin/db/models.py | 4 +- mediagoblin/media_types/image/processing.py | 41 +++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/mediagoblin/db/models.py b/mediagoblin/db/models.py index aa0c54d3..ca280b30 100644 --- a/mediagoblin/db/models.py +++ b/mediagoblin/db/models.py @@ -269,7 +269,7 @@ class MediaEntry(Base, MediaEntryMixin): return the value of the key. """ media_file = MediaFile.query.filter_by(media_entry=self.id, - name=file_key).first() + name=unicode(file_key)).first() if media_file: if metadata_key: @@ -282,7 +282,7 @@ class MediaEntry(Base, MediaEntryMixin): Update the file_metadata of a MediaFile. """ media_file = MediaFile.query.filter_by(media_entry=self.id, - name=file_key).first() + name=unicode(file_key)).first() file_metadata = media_file.file_metadata or {} diff --git a/mediagoblin/media_types/image/processing.py b/mediagoblin/media_types/image/processing.py index 088979bc..e02e11f6 100644 --- a/mediagoblin/media_types/image/processing.py +++ b/mediagoblin/media_types/image/processing.py @@ -76,6 +76,14 @@ def resize_image(entry, resized, keyname, target_name, new_size, resized.save(resized_file, quality=quality) store_public(entry, keyname, tmp_resized_filename, target_name) + # store the thumb/medium info + image_info = {'width': new_size[0], + 'height': new_size[1], + 'quality': quality, + 'filter': filter} + + entry.set_file_metadata(keyname, **image_info) + def resize_tool(entry, force, keyname, orig_file, target_name, @@ -86,6 +94,13 @@ def resize_tool(entry, max_height = mgg.global_config['media:' + keyname]['max_height'] new_size = (max_width, max_height) + # If thumb or medium is already the same quality and size, then don't + # reprocess + if _skip_resizing(entry, keyname, new_size, quality, filter): + _log.info('{0} of same size and quality already in use, skipping ' + 'resizing of media {1}.'.format(keyname, entry.id)) + return + # If the size of the original file exceeds the specified size for the desized # file, a target_name file is created and later associated with the media # entry. @@ -105,6 +120,32 @@ def resize_tool(entry, quality, filter) +def _skip_resizing(entry, keyname, size, quality, filter): + """ + Determines wither the saved thumb or medium is of the same quality and size + """ + image_info = entry.get_file_metadata(keyname) + + if not image_info: + return False + + skip = True + + if image_info.get('width') != size[0]: + skip = False + + elif image_info.get('height') != size[1]: + skip = False + + elif image_info.get('filter') != filter: + skip = False + + elif image_info.get('quality') != quality: + skip = False + + return skip + + SUPPORTED_FILETYPES = ['png', 'gif', 'jpg', 'jpeg', 'tiff'] -- 2.25.1