skip image resizing if possible
authorRodney Ewing <ewing.rj@gmail.com>
Wed, 21 Aug 2013 21:37:44 +0000 (14:37 -0700)
committerRodney Ewing <ewing.rj@gmail.com>
Wed, 21 Aug 2013 21:44:18 +0000 (14:44 -0700)
mediagoblin/db/models.py
mediagoblin/media_types/image/processing.py

index aa0c54d3310672b42198abb27db5048f033c89e5..ca280b3073a7f6f0671d8642e86912ce95c0748e 100644 (file)
@@ -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 {}
 
index 088979bc9212fafed4606bb051efd7ef90f554c8..e02e11f6f9abf5f0bb9e0bda76ec82b2f1530151 100644 (file)
@@ -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']