Fix unlinked file in processing image quality

This commit is contained in:
Jure Grabnar 2019-06-09 09:20:43 +02:00
parent e543a849d4
commit 27edafaafb
2 changed files with 17 additions and 3 deletions

View file

@ -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

View file

@ -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