From 1fdb27c8ee5252776606ac18e0e985034f4124c6 Mon Sep 17 00:00:00 2001 From: Vasily Fedoseyev Date: Wed, 10 Apr 2024 01:55:43 +0300 Subject: [PATCH] =?UTF-8?q?=D0=90=D0=BA=D0=BA=D1=83=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BD=D0=B5=D0=B5=20=D0=B7=D0=B0=D0=BA=D1=80=D1=8B=D0=B2=D0=B0?= =?UTF-8?q?=D0=B5=D0=BC=20original=20+=20=D0=B5=D1=81=D0=BB=D0=B8=20=D0=BD?= =?UTF-8?q?=D0=B5=20=D1=83=D0=B4=D0=B0=D0=BB=D0=BE=D1=81=D1=8C=20=D0=BE?= =?UTF-8?q?=D0=BF=D1=82=D0=B8=D0=BC=D0=B8=D0=B7=D0=B8=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D1=82=D1=8C,=20=D0=BD=D0=B5=20=D0=BE=D1=81=D1=82=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D1=8F=D0=B5=D0=BC=20=D1=84=D0=B0=D0=B9=D0=BB=20?= =?UTF-8?q?=D0=B2=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=BD=D1=8B=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/paperclip/attachment.rb | 5 +++-- lib/paperclip/optimizer.rb | 14 ++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/paperclip/attachment.rb b/lib/paperclip/attachment.rb index a16f187..e7e346a 100644 --- a/lib/paperclip/attachment.rb +++ b/lib/paperclip/attachment.rb @@ -436,14 +436,14 @@ module Paperclip end def post_process_styles #:nodoc: + old_original = queued_for_write[:original] styles.each do |name, args| begin raise RuntimeError.new("Style #{name} has no processors defined.") if args[:processors].blank? queued_for_write[name] = args[:processors].inject(queued_for_write[:original]) do |file, processor| Paperclip.processor(processor).make(file, args, self).tap do |new_file| # closing intermediary tempfiles - file.close! if new_file != file && file.respond_to?(:close!) && - (name == :original || !queued_for_write.value?(file)) + file.close! if new_file != file && file.respond_to?(:close!) && !queued_for_write.value?(file) end end rescue PaperclipError => e @@ -451,6 +451,7 @@ module Paperclip (errors[:processing] ||= []) << e.message if self.class.whiny end end + old_original.close! if old_original.respond_to?(:close!) && !queued_for_write.value?(old_original) end def interpolate pattern, style = default_style #:nodoc: diff --git a/lib/paperclip/optimizer.rb b/lib/paperclip/optimizer.rb index 174eae4..ab546a2 100644 --- a/lib/paperclip/optimizer.rb +++ b/lib/paperclip/optimizer.rb @@ -3,10 +3,7 @@ require 'open3' module Paperclip class Optimizer < Processor def make - optimized_file = optimize(@file) - return @file unless optimized_file && optimized_file.size > 0 # rubocop:disable Style/ZeroLengthPredicate - - optimized_file + optimize(@file) || @file end def real_content_type @@ -24,7 +21,7 @@ module Paperclip when 'image/jpeg', 'image/jpg', 'image/pjpeg' # NB: --stdout не работает, там бывают пустые файлы если оно решило ничего не делать # нельзя `cp`, надо чтобы открытый файл указывал куда надо, поэтому `cat>` - "cat #{src_shell} > #{dst_file} && jpegoptim --all-progressive -q --strip-com --strip-exif --strip-iptc --stdout -- #{dst_shell}" + "cat #{src_shell} > #{dst_file} && jpegoptim --all-progressive -q --strip-com --strip-exif --strip-iptc -- #{dst_shell}" when 'image/png', 'image/x-png' "pngcrush -rem alla -q #{src_shell} #{dst_shell}" when 'image/gif' @@ -33,8 +30,13 @@ module Paperclip return end run_and_verify!(cmd) + + if dst_file.size == 0 # rubocop:disable Style/ZeroLengthPredicate + dst_file.close! + return nil + end + dst_file.tap(&:flush).tap(&:rewind) - dst_file rescue StandardError => e dst_file.close! Paperclip.log("Error: cannot optimize: #{e}")