diff --git a/config/initializers/paperclip.rb b/config/initializers/paperclip.rb index 868ff2c55..7ac23bbe9 100644 --- a/config/initializers/paperclip.rb +++ b/config/initializers/paperclip.rb @@ -80,7 +80,7 @@ Paperclip::Attachment.class_eval do end def previewable_image? - content_type == %r{^image/#{Regexp.union(Constants::WHITELISTED_IMAGE_TYPES)}} + content_type =~ %r{^image/#{Regexp.union(Constants::WHITELISTED_IMAGE_TYPES)}} end end diff --git a/lib/paperclip/image_quality_calculate.rb b/lib/paperclip/image_quality_calculate.rb index 8927e5391..7a3f185ac 100644 --- a/lib/paperclip/image_quality_calculate.rb +++ b/lib/paperclip/image_quality_calculate.rb @@ -12,8 +12,22 @@ module Paperclip @attachment.instance.file_image_quality = quality.to_i # Asset will be save after all processors finished end - # We need again open file after read quality - File.new(File.expand_path(@file.path)) + + # We have to create a new temp file otherwise the postprocessing logic will + # delete the original file, leaving no files to postprocess for styles + current_format = File.extname(attachment.instance.file_file_name) + basename = File.basename(@file.path, current_format) + tempfile = Tempfile.new([basename, current_format].compact.join('.')) + + begin + tempfile.write(File.read(@file.path)) + tempfile.flush + tempfile + rescue StandardError => e + tempfile.close + tempfile.unlink + raise Paperclip::Error, "There was an error writing to tempfile - #{e}" + end rescue StandardError => e raise Paperclip::Error, "There was an error processing the image - #{e}" end