Don't fail ImageAnalyzer on unsupported types

Fix: #36065

The IamgeAnalyzer passes a image to ImageMagick without checking if the
image is supported by ImageMagick. This patch checks that image is
supported and if not logs an error and returns an empty hash instead of
raising an error. This is the same error handling we do when we
encounter a LoadError when mini_magick is not installed.
This commit is contained in:
Guilherme Mansur 2019-04-23 16:11:10 -04:00
parent df35204ed1
commit 6133dad869
3 changed files with 24 additions and 4 deletions

@ -1,3 +1,8 @@
* Don't raise when analyzing an unsupported image type by ImageMagick
Fixes: #36065
*Guilherme Mansur*
* Permit generating variants of BMP images.
*Younes Serraj*

@ -25,17 +25,24 @@ def metadata
{ width: image.width, height: image.height }
end
end
rescue LoadError
logger.info "Skipping image analysis because the mini_magick gem isn't installed"
{}
end
private
def read_image
download_blob_to_tempfile do |file|
require "mini_magick"
yield MiniMagick::Image.new(file.path)
image = MiniMagick::Image.new(file.path)
if image.valid?
yield image
else
logger.info "Skipping image analysis because ImageMagick doesn't support the file"
{}
end
end
rescue LoadError
logger.info "Skipping image analysis because the mini_magick gem isn't installed"
{}
end
def rotated_image?(image)

@ -29,4 +29,12 @@ class ActiveStorage::Analyzer::ImageAnalyzerTest < ActiveSupport::TestCase
assert_equal 792, metadata[:width]
assert_equal 584, metadata[:height]
end
test "analyzing an unsupported image type" do
blob = create_blob(data: "bad", filename: "bad_file.bad", content_type: "image/bad_type")
metadata = extract_metadata_from(blob)
assert_nil metadata[:width]
assert_nil metadata[:heigh]
end
end