Made the image processing use intermediary conversion file.
authorChristopher Allan Webber <cwebber@dustycloud.org>
Mon, 21 Nov 2011 04:03:38 +0000 (22:03 -0600)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Mon, 21 Nov 2011 04:12:51 +0000 (22:12 -0600)
This should fix the problem with PIL and the cloudfiles storage system
fighting.

mediagoblin/process_media/__init__.py

index 34d83e54bd5df6268f23c4d39be52e80871c1847..54c0c493d97d9e4f21f55cc51bedccdd51693a81 100644 (file)
@@ -118,6 +118,10 @@ def process_image(entry):
     Code to process an image
     """
     workbench = mgg.workbench_manager.create_workbench()
+    # Conversions subdirectory to avoid collisions
+    conversions_subdir = os.path.join(
+        workbench.dir, 'conversions')
+    os.mkdir(conversions_subdir)
 
     queued_filepath = entry['queued_media_file']
     queued_filename = workbench.localized_file(
@@ -133,11 +137,15 @@ def process_image(entry):
 
     thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
 
-    thumb_filepath = create_pub_filepath(entry, 'thumbnail' + extension)
-    thumb_file = mgg.public_store.get_file(thumb_filepath, 'w')
-
-    with thumb_file:
+    # Copy the thumb to the conversion subdir, then remotely.
+    thumb_filename = 'thumbnail' + extension
+    thumb_filepath = create_pub_filepath(entry, thumb_filename)
+    tmp_thumb_filename = os.path.join(
+        conversions_subdir, thumb_filename)
+    with file(tmp_thumb_filename, 'w') as thumb_file:
         thumb.save(thumb_file)
+    mgg.public_store.copy_local_to_storage(
+        tmp_thumb_filename, thumb_filepath)
 
     # If the size of the original file exceeds the specified size of a `medium`
     # file, a `medium.jpg` files is created and later associated with the media
@@ -148,12 +156,18 @@ def process_image(entry):
     if medium.size[0] > MEDIUM_SIZE[0] or medium.size[1] > MEDIUM_SIZE[1]:
         medium.thumbnail(MEDIUM_SIZE, Image.ANTIALIAS)
 
-        medium_filepath = create_pub_filepath(entry, 'medium' + extension)
-        medium_file = mgg.public_store.get_file(medium_filepath, 'w')
+        medium_filename = 'medium' + extension
+        medium_filepath = create_pub_filepath(entry, medium_filename)
+        tmp_medium_filename = os.path.join(
+            conversions_subdir, medium_filename)
 
-        with medium_file:
+        with file(tmp_medium_filename, 'w') as medium_file:
             medium.save(medium_file)
-            medium_processed = True
+
+        mgg.public_store.copy_local_to_storage(
+            tmp_medium_filename, medium_filepath)
+
+        medium_processed = True
 
     # we have to re-read because unlike PIL, not everything reads
     # things in string representation :)