forked from rails/rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load configured Active Storage plugins during boot
Previously, the parts of Active Storage using ruby-vips, mini_magick, or image_processing would try to load gems only when used. This strategy works well because it allows apps that don't need these features to easily ignore them and not have to depend on gems they don't need. However, the downsides to this strategy are that it requires loading code during requests and that it moves potential error messages into request logs instead of those errors being immediately visible on boot. This commit addresses these issues by restructing how the Vips and Image Magick transformers/analyzers are organized so that they will be loaded (if configured) on boot along with whatever dependencies they need. For now, if :vips or :mini_magick is specified as the Active Storage :variant_processor, not having the required gem in the Gemfile will print a deprecation warning instead of erroring because it is likely many apps are currently configured this way.
- Loading branch information
1 parent
1edb808
commit 7c8e351
Showing
12 changed files
with
167 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 6 additions & 11 deletions
17
activestorage/lib/active_storage/analyzer/image_analyzer/image_magick.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
activestorage/lib/active_storage/transformers/image_magick.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveStorage | ||
module Transformers | ||
class ImageMagick < ImageProcessingTransformer | ||
private | ||
def processor | ||
ImageProcessing::MiniMagick | ||
end | ||
|
||
def validate_transformation(name, argument) | ||
method_name = name.to_s.tr("-", "_") | ||
|
||
unless ActiveStorage.supported_image_processing_methods.include?(method_name) | ||
raise UnsupportedImageProcessingMethod, <<~ERROR.squish | ||
The provided transformation method is not supported: #{method_name}. | ||
ERROR | ||
end | ||
|
||
if argument.present? | ||
if argument.is_a?(String) || argument.is_a?(Symbol) | ||
validate_arg_string(argument) | ||
elsif argument.is_a?(Array) | ||
validate_arg_array(argument) | ||
elsif argument.is_a?(Hash) | ||
validate_arg_hash(argument) | ||
end | ||
end | ||
|
||
super | ||
end | ||
|
||
def validate_arg_string(argument) | ||
unsupported_arguments = ActiveStorage.unsupported_image_processing_arguments.any? do |bad_arg| | ||
argument.to_s.downcase.include?(bad_arg) | ||
end | ||
|
||
raise UnsupportedImageProcessingArgument if unsupported_arguments | ||
end | ||
|
||
def validate_arg_array(argument) | ||
argument.each do |arg| | ||
if arg.is_a?(Integer) || arg.is_a?(Float) | ||
next | ||
elsif arg.is_a?(String) || arg.is_a?(Symbol) | ||
validate_arg_string(arg) | ||
elsif arg.is_a?(Array) | ||
validate_arg_array(arg) | ||
elsif arg.is_a?(Hash) | ||
validate_arg_hash(arg) | ||
end | ||
end | ||
end | ||
|
||
def validate_arg_hash(argument) | ||
argument.each do |key, value| | ||
validate_arg_string(key) | ||
|
||
if value.is_a?(Integer) || value.is_a?(Float) | ||
next | ||
elsif value.is_a?(String) || value.is_a?(Symbol) | ||
validate_arg_string(value) | ||
elsif value.is_a?(Array) | ||
validate_arg_array(value) | ||
elsif value.is_a?(Hash) | ||
validate_arg_hash(value) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveStorage | ||
module Transformers | ||
class Vips < ImageProcessingTransformer | ||
def processor | ||
ImageProcessing::Vips | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.