+ Store a resized version of an image and return its pathname.
+
+ Arguments:
+ entry -- the entry for the image to resize
+ filename -- the filename of the original image being resized
+ new_path -- public file path for the new resized image
+ exif_tags -- EXIF data for the original image
+ workdir -- directory path for storing converted image files
+ new_size -- 2-tuple size for the resized image
+ """
+ try:
+ resized = Image.open(filename)
+ except IOError:
+ raise BadMediaFail()
+ resized = exif_fix_image_orientation(resized, exif_tags) # Fix orientation
+ resized.thumbnail(new_size, Image.ANTIALIAS)
+
+ # Copy the new file to the conversion subdir, then remotely.
+ tmp_resized_filename = os.path.join(workdir, new_path[-1])
+ with file(tmp_resized_filename, 'w') as resized_file:
+ resized.save(resized_file)
+ mgg.public_store.copy_local_to_storage(tmp_resized_filename, new_path)
+
+
+SUPPORTED_FILETYPES = ['png', 'gif', 'jpg', 'jpeg']
+
+
+def sniff_handler(media_file, **kw):
+ if kw.get('media') is not None: # That's a double negative!
+ name, ext = os.path.splitext(kw['media'].filename)
+ clean_ext = ext[1:].lower() # Strip the . from ext and make lowercase
+
+ if clean_ext in SUPPORTED_FILETYPES:
+ _log.info('Found file extension in supported filetypes')
+ return True
+ else:
+ _log.debug('Media present, extension not found in {0}'.format(
+ SUPPORTED_FILETYPES))
+ else:
+ _log.warning('Need additional information (keyword argument \'media\')'
+ ' to be able to handle sniffing')
+
+ return False
+
+
+@get_workbench
+def process_image(entry, workbench=None):
+ """Code to process an image. Will be run by celery.
+
+ A Workbench() represents a local tempory dir. It is automatically
+ cleaned up when this function exits.