Require ruby-vips before downloading blob

This ensures that if `download_blob_to_tempfile` raises an error before
evaluating its block, `rescue ::Vips::Error` will not cause a
`NameError` due to `ruby-vips` not being loaded yet.

This also prevents the blob from being downloaded if `ruby-vips` is not
available to analyze it.

A similar change was made for `Analyzer::ImageAnalyzer::ImageMagick`
in #45420.
This commit is contained in:
Jonathan Hefner 2022-06-23 12:35:33 -05:00
parent 9badb0f794
commit c5133f0010
1 changed files with 9 additions and 7 deletions

View File

@ -10,9 +10,14 @@ module ActiveStorage
private
def read_image
download_blob_to_tempfile do |file|
begin
require "ruby-vips"
rescue LoadError
logger.info "Skipping image analysis because the ruby-vips gem isn't installed"
return {}
end
download_blob_to_tempfile do |file|
image = instrument("vips") do
::Vips::Image.new_from_file(file.path, access: :sequential)
end
@ -23,13 +28,10 @@ module ActiveStorage
logger.info "Skipping image analysis because Vips doesn't support the file"
{}
end
rescue ::Vips::Error => error
logger.error "Skipping image analysis due to an Vips error: #{error.message}"
{}
end
rescue LoadError
logger.info "Skipping image analysis because the ruby-vips gem isn't installed"
{}
rescue ::Vips::Error => error
logger.error "Skipping image analysis due to an Vips error: #{error.message}"
{}
end
ROTATIONS = /Right-top|Left-bottom|Top-right|Bottom-left/