Switch over media processor to use the get_workbench decorator (#565)
[mediagoblin.git] / mediagoblin / media_types / image / processing.py
index bbfcd32d91ecce8f4400548449096bd7985b7411..e6a34ca004e44a3f011f3fcdcba96b1d27629948 100644 (file)
@@ -19,6 +19,7 @@ import os
 import logging
 
 from mediagoblin import mg_globals as mgg
+from mediagoblin.decorators import get_workbench
 from mediagoblin.processing import BadMediaFail, \
     create_pub_filepath, FilenameBuilder
 from mediagoblin.tools.exif import exif_fix_image_orientation, \
@@ -30,7 +31,8 @@ _log = logging.getLogger(__name__)
 
 def resize_image(entry, filename, new_path, exif_tags, workdir, new_size,
                  size_limits=(0, 0)):
-    """Store a resized version of an image and return its pathname.
+    """
+    Store a resized version of an image and return its pathname.
 
     Arguments:
     entry -- the entry for the image to resize
@@ -62,11 +64,6 @@ def sniff_handler(media_file, **kw):
         name, ext = os.path.splitext(kw['media'].filename)
         clean_ext = ext[1:].lower()  # Strip the . from ext and make lowercase
 
-        _log.debug('name: {0}\next: {1}\nlower_ext: {2}'.format(
-                name,
-                ext,
-                clean_ext))
-
         if clean_ext in SUPPORTED_FILETYPES:
             _log.info('Found file extension in supported filetypes')
             return True
@@ -80,11 +77,13 @@ def sniff_handler(media_file, **kw):
     return False
 
 
-def process_image(entry):
-    """
-    Code to process an image
+@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.
     """
-    workbench = mgg.workbench_manager.create_workbench()
     # Conversions subdirectory to avoid collisions
     conversions_subdir = os.path.join(
         workbench.dir, 'conversions')
@@ -124,17 +123,10 @@ def process_image(entry):
     else:
         medium_filepath = None
 
-    # we have to re-read because unlike PIL, not everything reads
-    # things in string representation :)
-    queued_file = file(queued_filename, 'rb')
-
-    with queued_file:
-        original_filepath = create_pub_filepath(
+    # Copy our queued local workbench to its final destination
+    original_filepath = create_pub_filepath(
             entry, name_builder.fill('{basename}{ext}'))
-
-        with mgg.public_store.get_file(original_filepath, 'wb') \
-            as original_file:
-            original_file.write(queued_file.read())
+    mgg.public_store.copy_local_to_storage(queued_filename, original_filepath)
 
     # Remove queued media file from storage and database
     mgg.queue_store.delete_file(queued_filepath)
@@ -142,10 +134,10 @@ def process_image(entry):
 
     # Insert media file information into database
     media_files_dict = entry.setdefault('media_files', {})
-    media_files_dict['thumb'] = thumb_filepath
-    media_files_dict['original'] = original_filepath
+    media_files_dict[u'thumb'] = thumb_filepath
+    media_files_dict[u'original'] = original_filepath
     if medium_filepath:
-        media_files_dict['medium'] = medium_filepath
+        media_files_dict[u'medium'] = medium_filepath
 
     # Insert exif data into database
     exif_all = clean_exif(exif_tags)
@@ -158,8 +150,6 @@ def process_image(entry):
             gps_data['gps_' + key] = gps_data.pop(key)
         entry.media_data_init(**gps_data)
 
-    # clean up workbench
-    workbench.destroy_self()
 
 if __name__ == '__main__':
     import sys