diff options
author | Starfall <us@starfall.systems> | 2020-07-01 16:04:26 -0500 |
---|---|---|
committer | Starfall <us@starfall.systems> | 2020-07-01 16:04:26 -0500 |
commit | 9b8ab85007cc9b527ba8c59a3624731cd8e46ffe (patch) | |
tree | 28cf79b4c73ec6d328136ea04a63321862096e5a /lib/paperclip/image_extractor.rb | |
parent | 6ec1f4c99e51a13ca56d639b85168dfb874daf75 (diff) | |
parent | 665eb1affe7ba765af476e6f00096aad3af70735 (diff) |
Merge branch 'glitch' into main
Diffstat (limited to 'lib/paperclip/image_extractor.rb')
-rw-r--r-- | lib/paperclip/image_extractor.rb | 45 |
1 files changed, 25 insertions, 20 deletions
diff --git a/lib/paperclip/image_extractor.rb b/lib/paperclip/image_extractor.rb index 114852e8b..f5a54d1a5 100644 --- a/lib/paperclip/image_extractor.rb +++ b/lib/paperclip/image_extractor.rb @@ -4,28 +4,10 @@ require 'mime/types/columnar' module Paperclip class ImageExtractor < Paperclip::Processor - IMAGE_EXTRACTION_OPTIONS = { - convert_options: { - output: { - 'loglevel' => 'fatal', - vf: 'scale=\'min(400\, iw):min(400\, ih)\':force_original_aspect_ratio=decrease', - }.freeze, - }.freeze, - format: 'png', - time: -1, - file_geometry_parser: FastGeometryParser, - }.freeze - def make return @file unless options[:style] == :original - image = begin - begin - Paperclip::Transcoder.make(file, IMAGE_EXTRACTION_OPTIONS.dup, attachment) - rescue Paperclip::Error, ::Av::CommandError - nil - end - end + image = extract_image_from_file! unless image.nil? begin @@ -36,7 +18,7 @@ module Paperclip # to make sure it's cleaned up begin - FileUtils.rm(image) + image.close(true) rescue Errno::ENOENT nil end @@ -45,5 +27,28 @@ module Paperclip @file end + + private + + def extract_image_from_file! + ::Av.logger = Paperclip.logger + + cli = ::Av.cli + dst = Tempfile.new([File.basename(@file.path, '.*'), '.png']) + dst.binmode + + cli.add_source(@file.path) + cli.add_destination(dst.path) + cli.add_output_param loglevel: 'fatal' + + begin + cli.run + rescue Cocaine::ExitStatusError + dst.close(true) + return nil + end + + dst + end end end |