提交 2a12b723 编写于 作者: J JvH 提交者: George Claghorn

Add web_image_content_types config option for ActiveStorage

Add `config.active_storage.web_image_content_types` to allow applications
to add content types (like `image/webp`) in which variants can be processed,
instead of letting those images be converted to the fallback PNG format.
上级 61d615c0
* Add `config.active_storage.web_image_content_types` to allow applications
to add content types (like `image/webp`) in which variants can be processed,
instead of letting those images be converted to the fallback PNG format.
*Jeroen van Haperen*
* Add support for creating variants of `WebP` images out of the box. * Add support for creating variants of `WebP` images out of the box.
*Dino Maric* *Dino Maric*
......
...@@ -53,8 +53,6 @@ ...@@ -53,8 +53,6 @@
# * {ImageProcessing::Vips}[https://github.com/janko-m/image_processing/blob/master/doc/vips.md#methods] # * {ImageProcessing::Vips}[https://github.com/janko-m/image_processing/blob/master/doc/vips.md#methods]
# * {ruby-vips reference}[http://www.rubydoc.info/gems/ruby-vips/Vips/Image] # * {ruby-vips reference}[http://www.rubydoc.info/gems/ruby-vips/Vips/Image]
class ActiveStorage::Variant class ActiveStorage::Variant
WEB_IMAGE_CONTENT_TYPES = %w[ image/png image/jpeg image/jpg image/gif ]
attr_reader :blob, :variation attr_reader :blob, :variation
delegate :service, to: :blob delegate :service, to: :blob
...@@ -106,7 +104,7 @@ def process ...@@ -106,7 +104,7 @@ def process
def specification def specification
@specification ||= @specification ||=
if WEB_IMAGE_CONTENT_TYPES.include?(blob.content_type) if ActiveStorage.web_image_content_types.include?(blob.content_type)
Specification.new \ Specification.new \
filename: blob.filename, filename: blob.filename,
content_type: blob.content_type, content_type: blob.content_type,
......
# frozen_string_literal: true # frozen_string_literal: true
class ActiveStorage::VariantWithRecord class ActiveStorage::VariantWithRecord
WEB_IMAGE_CONTENT_TYPES = %w[ image/png image/jpeg image/jpg image/gif ]
attr_reader :blob, :variation attr_reader :blob, :variation
def initialize(blob, variation) def initialize(blob, variation)
...@@ -36,7 +34,7 @@ def url(**options) ...@@ -36,7 +34,7 @@ def url(**options)
private private
def transform_blob def transform_blob
blob.open do |input| blob.open do |input|
if blob.content_type.in?(WEB_IMAGE_CONTENT_TYPES) if blob.content_type.in?(ActiveStorage.web_image_content_types)
variation.transform(input) do |output| variation.transform(input) do |output|
yield io: output, filename: blob.filename, content_type: blob.content_type, service_name: blob.service.name yield io: output, filename: blob.filename, content_type: blob.content_type, service_name: blob.service.name
end end
......
...@@ -53,6 +53,7 @@ module ActiveStorage ...@@ -53,6 +53,7 @@ module ActiveStorage
mattr_accessor :paths, default: {} mattr_accessor :paths, default: {}
mattr_accessor :variable_content_types, default: [] mattr_accessor :variable_content_types, default: []
mattr_accessor :web_image_content_types, default: []
mattr_accessor :binary_content_type, default: "application/octet-stream" mattr_accessor :binary_content_type, default: "application/octet-stream"
mattr_accessor :content_types_to_serve_as_binary, default: [] mattr_accessor :content_types_to_serve_as_binary, default: []
mattr_accessor :content_types_allowed_inline, default: [] mattr_accessor :content_types_allowed_inline, default: []
......
...@@ -41,6 +41,13 @@ class Engine < Rails::Engine # :nodoc: ...@@ -41,6 +41,13 @@ class Engine < Rails::Engine # :nodoc:
image/webp image/webp
) )
config.active_storage.web_image_content_types = %w(
image/png
image/jpeg
image/jpg
image/gif
)
config.active_storage.content_types_to_serve_as_binary = %w( config.active_storage.content_types_to_serve_as_binary = %w(
text/html text/html
text/javascript text/javascript
...@@ -79,6 +86,7 @@ class Engine < Rails::Engine # :nodoc: ...@@ -79,6 +86,7 @@ class Engine < Rails::Engine # :nodoc:
ActiveStorage.draw_routes = app.config.active_storage.draw_routes != false ActiveStorage.draw_routes = app.config.active_storage.draw_routes != false
ActiveStorage.variable_content_types = app.config.active_storage.variable_content_types || [] ActiveStorage.variable_content_types = app.config.active_storage.variable_content_types || []
ActiveStorage.web_image_content_types = app.config.active_storage.web_image_content_types || []
ActiveStorage.content_types_to_serve_as_binary = app.config.active_storage.content_types_to_serve_as_binary || [] ActiveStorage.content_types_to_serve_as_binary = app.config.active_storage.content_types_to_serve_as_binary || []
ActiveStorage.service_urls_expire_in = app.config.active_storage.service_urls_expire_in || 5.minutes ActiveStorage.service_urls_expire_in = app.config.active_storage.service_urls_expire_in || 5.minutes
ActiveStorage.content_types_allowed_inline = app.config.active_storage.content_types_allowed_inline || [] ActiveStorage.content_types_allowed_inline = app.config.active_storage.content_types_allowed_inline || []
......
...@@ -902,6 +902,8 @@ You can find more detailed configuration options in the ...@@ -902,6 +902,8 @@ You can find more detailed configuration options in the
* `config.active_storage.variable_content_types` accepts an array of strings indicating the content types that Active Storage can transform through ImageMagick. The default is `%w(image/png image/gif image/jpg image/jpeg image/pjpeg image/tiff image/bmp image/vnd.adobe.photoshop image/vnd.microsoft.icon image/webp)`. * `config.active_storage.variable_content_types` accepts an array of strings indicating the content types that Active Storage can transform through ImageMagick. The default is `%w(image/png image/gif image/jpg image/jpeg image/pjpeg image/tiff image/bmp image/vnd.adobe.photoshop image/vnd.microsoft.icon image/webp)`.
* `config.active_storage.web_image_content_types` accepts an array of strings regarded as web image content types in which variants can be processed without being converted to the fallback PNG format. If you want to use `WebP` variants in your application you can add `image/webp` to this array. The default is `%w(image/png image/jpeg image/jpg image/gif)`.
* `config.active_storage.content_types_to_serve_as_binary` accepts an array of strings indicating the content types that Active Storage will always serve as an attachment, rather than inline. The default is `%w(text/html * `config.active_storage.content_types_to_serve_as_binary` accepts an array of strings indicating the content types that Active Storage will always serve as an attachment, rather than inline. The default is `%w(text/html
text/javascript image/svg+xml application/postscript application/x-shockwave-flash text/xml application/xml application/xhtml+xml application/mathml+xml text/cache-manifest)`. text/javascript image/svg+xml application/postscript application/x-shockwave-flash text/xml application/xml application/xhtml+xml application/mathml+xml text/cache-manifest)`.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册