FIX: properly downsize image on upload

FIX: properly downsize image on upload

Overwriting the same file with ‘convert’ is not always working as expected. Adding a temporary file as the destination of the downsize makes this operation much more reliable.

Also switched to using (the more aggressive) 50% resize instead of halving the number of pixels.

diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb
index 54abb69..632d7a7 100644
--- a/lib/upload_creator.rb
+++ b/lib/upload_creator.rb
@@ -197,6 +197,7 @@ class UploadCreator
     keep_jpeg &&= (filesize - new_size) > MIN_CONVERT_TO_JPEG_BYTES_SAVED
 
     if keep_jpeg
+      @file.close
       @file = jpeg_tempfile
       extract_image_info!
     else
@@ -227,17 +228,20 @@ class UploadCreator
   def downsize!
     3.times do
       original_size = filesize
-      downsized_pixels = [pixels, max_image_pixels].min / 2
+      down_tempfile = Tempfile.new(["down", ".#{@image_info.type}"])
 
       OptimizedImage.downsize(
         @file.path,
-        @file.path,
-        "#{downsized_pixels}@",
+        down_tempfile.path,
+        "50%",
         filename: @filename,
         allow_animation: allow_animation,
         raise_on_error: true
       )
 
+      @file.close
+      @file = down_tempfile
+
       extract_image_info!
 
       return if filesize >= original_size || pixels == 0 || !should_downsize?

GitHub sha: 46d12c5a

Is this leaving the old file around without nuking it leaking disk spac ?

FIX: ensure we remove tempfiles from disk when creating an upload