activestorage 6.0.3.2 → 6.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of activestorage might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +149 -148
- data/MIT-LICENSE +1 -1
- data/README.md +35 -3
- data/app/controllers/active_storage/base_controller.rb +11 -0
- data/app/controllers/active_storage/blobs/proxy_controller.rb +14 -0
- data/app/controllers/active_storage/{blobs_controller.rb → blobs/redirect_controller.rb} +2 -2
- data/app/controllers/active_storage/direct_uploads_controller.rb +1 -1
- data/app/controllers/active_storage/disk_controller.rb +8 -20
- data/app/controllers/active_storage/representations/proxy_controller.rb +19 -0
- data/app/controllers/active_storage/{representations_controller.rb → representations/redirect_controller.rb} +2 -2
- data/app/controllers/concerns/active_storage/file_server.rb +18 -0
- data/app/controllers/concerns/active_storage/set_blob.rb +1 -1
- data/app/controllers/concerns/active_storage/set_current.rb +2 -2
- data/app/controllers/concerns/active_storage/set_headers.rb +12 -0
- data/app/jobs/active_storage/mirror_job.rb +15 -0
- data/app/models/active_storage/attachment.rb +18 -10
- data/app/models/active_storage/blob.rb +114 -59
- data/app/models/active_storage/blob/analyzable.rb +6 -2
- data/app/models/active_storage/blob/identifiable.rb +7 -6
- data/app/models/active_storage/blob/representable.rb +34 -4
- data/app/models/active_storage/preview.rb +31 -10
- data/app/models/active_storage/record.rb +7 -0
- data/app/models/active_storage/variant.rb +28 -41
- data/app/models/active_storage/variant_record.rb +8 -0
- data/app/models/active_storage/variant_with_record.rb +54 -0
- data/app/models/active_storage/variation.rb +25 -20
- data/config/routes.rb +58 -8
- data/db/migrate/20170806125915_create_active_storage_tables.rb +14 -5
- data/db/update_migrate/20190112182829_add_service_name_to_active_storage_blobs.rb +17 -0
- data/db/update_migrate/20191206030411_create_active_storage_variant_records.rb +11 -0
- data/lib/active_storage.rb +5 -2
- data/lib/active_storage/analyzer.rb +6 -0
- data/lib/active_storage/analyzer/image_analyzer.rb +3 -0
- data/lib/active_storage/analyzer/null_analyzer.rb +4 -0
- data/lib/active_storage/analyzer/video_analyzer.rb +14 -3
- data/lib/active_storage/attached/changes/create_many.rb +1 -0
- data/lib/active_storage/attached/changes/create_one.rb +17 -4
- data/lib/active_storage/attached/many.rb +4 -3
- data/lib/active_storage/attached/model.rb +77 -16
- data/lib/active_storage/attached/one.rb +4 -3
- data/lib/active_storage/engine.rb +25 -27
- data/lib/active_storage/gem_version.rb +3 -3
- data/lib/active_storage/log_subscriber.rb +6 -0
- data/lib/active_storage/previewer.rb +3 -2
- data/lib/active_storage/previewer/mupdf_previewer.rb +3 -3
- data/lib/active_storage/previewer/poppler_pdf_previewer.rb +3 -3
- data/lib/active_storage/previewer/video_previewer.rb +2 -2
- data/lib/active_storage/service.rb +36 -7
- data/lib/active_storage/service/azure_storage_service.rb +40 -35
- data/lib/active_storage/service/configurator.rb +3 -1
- data/lib/active_storage/service/disk_service.rb +36 -31
- data/lib/active_storage/service/gcs_service.rb +18 -16
- data/lib/active_storage/service/mirror_service.rb +31 -7
- data/lib/active_storage/service/registry.rb +32 -0
- data/lib/active_storage/service/s3_service.rb +53 -23
- data/lib/active_storage/transformers/image_processing_transformer.rb +13 -7
- data/lib/active_storage/transformers/transformer.rb +0 -3
- metadata +56 -20
- data/db/update_migrate/20180723000244_add_foreign_key_constraint_to_active_storage_attachments_for_blob_id.rb +0 -9
- data/lib/active_storage/downloading.rb +0 -47
- data/lib/active_storage/transformers/mini_magick_transformer.rb +0 -38
@@ -29,12 +29,16 @@ module ActiveStorage::Blob::Analyzable
|
|
29
29
|
update! metadata: metadata.merge(extract_metadata_via_analyzer)
|
30
30
|
end
|
31
31
|
|
32
|
-
# Enqueues an ActiveStorage::AnalyzeJob which calls #analyze.
|
32
|
+
# Enqueues an ActiveStorage::AnalyzeJob which calls #analyze, or calls #analyze inline based on analyzer class configuration.
|
33
33
|
#
|
34
34
|
# This method is automatically called for a blob when it's attached for the first time. You can call it to analyze a blob
|
35
35
|
# again (e.g. if you add a new analyzer or modify an existing one).
|
36
36
|
def analyze_later
|
37
|
-
|
37
|
+
if analyzer_class.analyze_later?
|
38
|
+
ActiveStorage::AnalyzeJob.perform_later(self)
|
39
|
+
else
|
40
|
+
analyze
|
41
|
+
end
|
38
42
|
end
|
39
43
|
|
40
44
|
# Returns true if the blob has been analyzed.
|
@@ -2,9 +2,14 @@
|
|
2
2
|
|
3
3
|
module ActiveStorage::Blob::Identifiable
|
4
4
|
def identify
|
5
|
+
identify_without_saving
|
6
|
+
save!
|
7
|
+
end
|
8
|
+
|
9
|
+
def identify_without_saving
|
5
10
|
unless identified?
|
6
|
-
|
7
|
-
|
11
|
+
self.content_type = identify_content_type
|
12
|
+
self.identified = true
|
8
13
|
end
|
9
14
|
end
|
10
15
|
|
@@ -24,8 +29,4 @@ module ActiveStorage::Blob::Identifiable
|
|
24
29
|
""
|
25
30
|
end
|
26
31
|
end
|
27
|
-
|
28
|
-
def update_service_metadata
|
29
|
-
service.update_metadata key, **service_metadata if service_metadata.any?
|
30
|
-
end
|
31
32
|
end
|
@@ -1,16 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "mimemagic"
|
4
|
+
|
3
5
|
module ActiveStorage::Blob::Representable
|
4
6
|
extend ActiveSupport::Concern
|
5
7
|
|
6
8
|
included do
|
9
|
+
has_many :variant_records, class_name: "ActiveStorage::VariantRecord", dependent: false
|
10
|
+
before_destroy { variant_records.destroy_all if ActiveStorage.track_variants }
|
11
|
+
|
7
12
|
has_one_attached :preview_image
|
8
13
|
end
|
9
14
|
|
10
15
|
# Returns an ActiveStorage::Variant instance with the set of +transformations+ provided. This is only relevant for image
|
11
16
|
# files, and it allows any image to be transformed for size, colors, and the like. Example:
|
12
17
|
#
|
13
|
-
# avatar.variant(resize_to_limit: [100, 100]).processed.
|
18
|
+
# avatar.variant(resize_to_limit: [100, 100]).processed.url
|
14
19
|
#
|
15
20
|
# This will create and process a variant of the avatar blob that's constrained to a height and width of 100px.
|
16
21
|
# Then it'll upload said variant to the service according to a derivative key of the blob and the transformations.
|
@@ -27,7 +32,7 @@ module ActiveStorage::Blob::Representable
|
|
27
32
|
# variable, call ActiveStorage::Blob#variable?.
|
28
33
|
def variant(transformations)
|
29
34
|
if variable?
|
30
|
-
|
35
|
+
variant_class.new(self, ActiveStorage::Variation.wrap(transformations).default_to(default_variant_transformations))
|
31
36
|
else
|
32
37
|
raise ActiveStorage::InvariableError
|
33
38
|
end
|
@@ -43,7 +48,7 @@ module ActiveStorage::Blob::Representable
|
|
43
48
|
# from a non-image blob. Active Storage comes with built-in previewers for videos and PDF documents. The video previewer
|
44
49
|
# extracts the first frame from a video and the PDF previewer extracts the first page from a PDF document.
|
45
50
|
#
|
46
|
-
# blob.preview(resize_to_limit: [100, 100]).processed.
|
51
|
+
# blob.preview(resize_to_limit: [100, 100]).processed.url
|
47
52
|
#
|
48
53
|
# Avoid processing previews synchronously in views. Instead, link to a controller action that processes them on demand.
|
49
54
|
# Active Storage provides one, but you may want to create your own (for example, if you need authentication). Here’s
|
@@ -69,7 +74,7 @@ module ActiveStorage::Blob::Representable
|
|
69
74
|
|
70
75
|
# Returns an ActiveStorage::Preview for a previewable blob or an ActiveStorage::Variant for a variable image blob.
|
71
76
|
#
|
72
|
-
# blob.representation(resize_to_limit: [100, 100]).processed.
|
77
|
+
# blob.representation(resize_to_limit: [100, 100]).processed.url
|
73
78
|
#
|
74
79
|
# Raises ActiveStorage::UnrepresentableError if the receiving blob is neither variable nor previewable. Call
|
75
80
|
# ActiveStorage::Blob#representable? to determine whether a blob is representable.
|
@@ -90,4 +95,29 @@ module ActiveStorage::Blob::Representable
|
|
90
95
|
def representable?
|
91
96
|
variable? || previewable?
|
92
97
|
end
|
98
|
+
|
99
|
+
private
|
100
|
+
def default_variant_transformations
|
101
|
+
{ format: default_variant_format }
|
102
|
+
end
|
103
|
+
|
104
|
+
def default_variant_format
|
105
|
+
if web_image?
|
106
|
+
format || :png
|
107
|
+
else
|
108
|
+
:png
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def format
|
113
|
+
if filename.extension.present? && MimeMagic.by_extension(filename.extension)&.to_s == content_type
|
114
|
+
filename.extension
|
115
|
+
else
|
116
|
+
MimeMagic.new(content_type).extensions.first
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def variant_class
|
121
|
+
ActiveStorage.track_variants ? ActiveStorage::VariantWithRecord : ActiveStorage::Variant
|
122
|
+
end
|
93
123
|
end
|
@@ -3,8 +3,9 @@
|
|
3
3
|
# Some non-image blobs can be previewed: that is, they can be presented as images. A video blob can be previewed by
|
4
4
|
# extracting its first frame, and a PDF blob can be previewed by extracting its first page.
|
5
5
|
#
|
6
|
-
# A previewer extracts a preview image from a blob. Active Storage provides previewers for videos and PDFs
|
7
|
-
# ActiveStorage::Previewer::VideoPreviewer
|
6
|
+
# A previewer extracts a preview image from a blob. Active Storage provides previewers for videos and PDFs.
|
7
|
+
# ActiveStorage::Previewer::VideoPreviewer is used for videos whereas ActiveStorage::Previewer::PopplerPDFPreviewer
|
8
|
+
# and ActiveStorage::Previewer::MuPDFPreviewer are used for PDFs. Build custom previewers by
|
8
9
|
# subclassing ActiveStorage::Previewer and implementing the requisite methods. Consult the ActiveStorage::Previewer
|
9
10
|
# documentation for more details on what's required of previewers.
|
10
11
|
#
|
@@ -13,11 +14,11 @@
|
|
13
14
|
# by manipulating +Rails.application.config.active_storage.previewers+ in an initializer:
|
14
15
|
#
|
15
16
|
# Rails.application.config.active_storage.previewers
|
16
|
-
# # => [ ActiveStorage::Previewer::
|
17
|
+
# # => [ ActiveStorage::Previewer::PopplerPDFPreviewer, ActiveStorage::Previewer::MuPDFPreviewer, ActiveStorage::Previewer::VideoPreviewer ]
|
17
18
|
#
|
18
19
|
# # Add a custom previewer for Microsoft Office documents:
|
19
20
|
# Rails.application.config.active_storage.previewers << DOCXPreviewer
|
20
|
-
# # => [ ActiveStorage::Previewer::
|
21
|
+
# # => [ ActiveStorage::Previewer::PopplerPDFPreviewer, ActiveStorage::Previewer::MuPDFPreviewer, ActiveStorage::Previewer::VideoPreviewer, DOCXPreviewer ]
|
21
22
|
#
|
22
23
|
# Outside of a Rails application, modify +ActiveStorage.previewers+ instead.
|
23
24
|
#
|
@@ -38,7 +39,7 @@ class ActiveStorage::Preview
|
|
38
39
|
|
39
40
|
# Processes the preview if it has not been processed yet. Returns the receiving Preview instance for convenience:
|
40
41
|
#
|
41
|
-
# blob.preview(resize_to_limit: [100, 100]).processed.
|
42
|
+
# blob.preview(resize_to_limit: [100, 100]).processed.url
|
42
43
|
#
|
43
44
|
# Processing a preview generates an image from its blob and attaches the preview image to the blob. Because the preview
|
44
45
|
# image is stored with the blob, it is only generated once.
|
@@ -56,10 +57,30 @@ class ActiveStorage::Preview
|
|
56
57
|
# preview has not been processed yet.
|
57
58
|
#
|
58
59
|
# This method synchronously processes a variant of the preview image, so do not call it in views. Instead, generate
|
59
|
-
# a stable URL that redirects to the
|
60
|
-
def
|
60
|
+
# a stable URL that redirects to the URL returned by this method.
|
61
|
+
def url(**options)
|
61
62
|
if processed?
|
62
|
-
variant.
|
63
|
+
variant.url(**options)
|
64
|
+
else
|
65
|
+
raise UnprocessedError
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
alias_method :service_url, :url
|
70
|
+
deprecate service_url: :url
|
71
|
+
|
72
|
+
# Returns a combination key of the blob and the variation that together identifies a specific variant.
|
73
|
+
def key
|
74
|
+
if processed?
|
75
|
+
variant.key
|
76
|
+
else
|
77
|
+
raise UnprocessedError
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def download(&block)
|
82
|
+
if processed?
|
83
|
+
variant.download(&block)
|
63
84
|
else
|
64
85
|
raise UnprocessedError
|
65
86
|
end
|
@@ -71,7 +92,7 @@ class ActiveStorage::Preview
|
|
71
92
|
end
|
72
93
|
|
73
94
|
def process
|
74
|
-
previewer.preview do |attachable|
|
95
|
+
previewer.preview(service_name: blob.service_name) do |attachable|
|
75
96
|
ActiveRecord::Base.connected_to(role: ActiveRecord::Base.writing_role) do
|
76
97
|
image.attach(attachable)
|
77
98
|
end
|
@@ -79,7 +100,7 @@ class ActiveStorage::Preview
|
|
79
100
|
end
|
80
101
|
|
81
102
|
def variant
|
82
|
-
|
103
|
+
image.variant(variation).processed
|
83
104
|
end
|
84
105
|
|
85
106
|
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "ostruct"
|
4
|
-
|
5
3
|
# Image blobs can have variants that are the result of a set of transformations applied to the original.
|
6
4
|
# These variants are used to create thumbnails, fixed-size avatars, or any other derivative image from the
|
7
5
|
# original.
|
@@ -10,7 +8,7 @@ require "ostruct"
|
|
10
8
|
# of the file, so you must add <tt>gem "image_processing"</tt> to your Gemfile if you wish to use variants. By
|
11
9
|
# default, images will be processed with {ImageMagick}[http://imagemagick.org] using the
|
12
10
|
# {MiniMagick}[https://github.com/minimagick/minimagick] gem, but you can also switch to the
|
13
|
-
# {libvips}[http://
|
11
|
+
# {libvips}[http://libvips.github.io/libvips/] processor operated by the {ruby-vips}[https://github.com/libvips/ruby-vips]
|
14
12
|
# gem).
|
15
13
|
#
|
16
14
|
# Rails.application.config.active_storage.variant_processor
|
@@ -36,7 +34,7 @@ require "ostruct"
|
|
36
34
|
# has already been processed and uploaded to the service, and, if so, just return that. Otherwise it will perform
|
37
35
|
# the transformations, upload the variant to the service, and return itself again. Example:
|
38
36
|
#
|
39
|
-
# avatar.variant(resize_to_limit: [100, 100]).processed.
|
37
|
+
# avatar.variant(resize_to_limit: [100, 100]).processed.url
|
40
38
|
#
|
41
39
|
# This will create and process a variant of the avatar blob that's constrained to a height and width of 100.
|
42
40
|
# Then it'll upload said variant to the service according to a derivative key of the blob and the transformations.
|
@@ -53,10 +51,9 @@ require "ostruct"
|
|
53
51
|
# * {ImageProcessing::Vips}[https://github.com/janko-m/image_processing/blob/master/doc/vips.md#methods]
|
54
52
|
# * {ruby-vips reference}[http://www.rubydoc.info/gems/ruby-vips/Vips/Image]
|
55
53
|
class ActiveStorage::Variant
|
56
|
-
WEB_IMAGE_CONTENT_TYPES = %w[ image/png image/jpeg image/jpg image/gif ]
|
57
|
-
|
58
54
|
attr_reader :blob, :variation
|
59
55
|
delegate :service, to: :blob
|
56
|
+
delegate :content_type, to: :variation
|
60
57
|
|
61
58
|
def initialize(blob, variation_or_variation_key)
|
62
59
|
@blob, @variation = blob, ActiveStorage::Variation.wrap(variation_or_variation_key)
|
@@ -73,18 +70,34 @@ class ActiveStorage::Variant
|
|
73
70
|
"variants/#{blob.key}/#{Digest::SHA256.hexdigest(variation.key)}"
|
74
71
|
end
|
75
72
|
|
76
|
-
# Returns the URL of the variant on the service.
|
77
|
-
# with users. Instead, the +service_url+ should only be exposed as a redirect from a stable, possibly authenticated URL.
|
78
|
-
# Hiding the +service_url+ behind a redirect also gives you the power to change services without updating all URLs. And
|
79
|
-
# it allows permanent URLs that redirect to the +service_url+ to be cached in the view.
|
73
|
+
# Returns the URL of the blob variant on the service. See {ActiveStorage::Blob#url} for details.
|
80
74
|
#
|
81
75
|
# Use <tt>url_for(variant)</tt> (or the implied form, like +link_to variant+ or +redirect_to variant+) to get the stable URL
|
82
76
|
# for a variant that points to the ActiveStorage::RepresentationsController, which in turn will use this +service_call+ method
|
83
77
|
# for its redirection.
|
84
|
-
def
|
78
|
+
def url(expires_in: ActiveStorage.service_urls_expire_in, disposition: :inline)
|
85
79
|
service.url key, expires_in: expires_in, disposition: disposition, filename: filename, content_type: content_type
|
86
80
|
end
|
87
81
|
|
82
|
+
alias_method :service_url, :url
|
83
|
+
deprecate service_url: :url
|
84
|
+
|
85
|
+
# Downloads the file associated with this variant. If no block is given, the entire file is read into memory and returned.
|
86
|
+
# That'll use a lot of RAM for very large files. If a block is given, then the download is streamed and yielded in chunks.
|
87
|
+
def download(&block)
|
88
|
+
service.download key, &block
|
89
|
+
end
|
90
|
+
|
91
|
+
def filename
|
92
|
+
ActiveStorage::Filename.new "#{blob.filename.base}.#{variation.format}"
|
93
|
+
end
|
94
|
+
|
95
|
+
alias_method :content_type_for_serving, :content_type
|
96
|
+
|
97
|
+
def forced_disposition_for_serving #:nodoc:
|
98
|
+
nil
|
99
|
+
end
|
100
|
+
|
88
101
|
# Returns the receiving variant. Allows ActiveStorage::Variant and ActiveStorage::Preview instances to be used interchangeably.
|
89
102
|
def image
|
90
103
|
self
|
@@ -96,36 +109,10 @@ class ActiveStorage::Variant
|
|
96
109
|
end
|
97
110
|
|
98
111
|
def process
|
99
|
-
blob.open do |
|
100
|
-
transform(
|
101
|
-
|
102
|
-
end
|
103
|
-
|
104
|
-
def transform(image, &block)
|
105
|
-
variation.transform(image, format: format, &block)
|
106
|
-
end
|
107
|
-
|
108
|
-
def upload(file)
|
109
|
-
service.upload(key, file)
|
110
|
-
end
|
111
|
-
|
112
|
-
|
113
|
-
def specification
|
114
|
-
@specification ||=
|
115
|
-
if WEB_IMAGE_CONTENT_TYPES.include?(blob.content_type)
|
116
|
-
Specification.new \
|
117
|
-
filename: blob.filename,
|
118
|
-
content_type: blob.content_type,
|
119
|
-
format: nil
|
120
|
-
else
|
121
|
-
Specification.new \
|
122
|
-
filename: ActiveStorage::Filename.new("#{blob.filename.base}.png"),
|
123
|
-
content_type: "image/png",
|
124
|
-
format: "png"
|
112
|
+
blob.open do |input|
|
113
|
+
variation.transform(input) do |output|
|
114
|
+
service.upload(key, output, content_type: content_type)
|
125
115
|
end
|
116
|
+
end
|
126
117
|
end
|
127
|
-
|
128
|
-
delegate :filename, :content_type, :format, to: :specification
|
129
|
-
|
130
|
-
class Specification < OpenStruct; end
|
131
118
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class ActiveStorage::VariantWithRecord
|
4
|
+
attr_reader :blob, :variation
|
5
|
+
|
6
|
+
def initialize(blob, variation)
|
7
|
+
@blob, @variation = blob, ActiveStorage::Variation.wrap(variation)
|
8
|
+
end
|
9
|
+
|
10
|
+
def processed
|
11
|
+
process
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
def process
|
16
|
+
transform_blob { |image| create_or_find_record(image: image) } unless processed?
|
17
|
+
end
|
18
|
+
|
19
|
+
def processed?
|
20
|
+
record.present?
|
21
|
+
end
|
22
|
+
|
23
|
+
def image
|
24
|
+
record&.image
|
25
|
+
end
|
26
|
+
|
27
|
+
delegate :key, :url, :download, to: :image, allow_nil: true
|
28
|
+
|
29
|
+
alias_method :service_url, :url
|
30
|
+
deprecate service_url: :url
|
31
|
+
|
32
|
+
private
|
33
|
+
def transform_blob
|
34
|
+
blob.open do |input|
|
35
|
+
variation.transform(input) do |output|
|
36
|
+
yield io: output, filename: "#{blob.filename.base}.#{variation.format}",
|
37
|
+
content_type: variation.content_type, service_name: blob.service.name
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def create_or_find_record(image:)
|
43
|
+
@record =
|
44
|
+
ActiveRecord::Base.connected_to(role: ActiveRecord::Base.writing_role) do
|
45
|
+
blob.variant_records.create_or_find_by!(variation_digest: variation.digest) do |record|
|
46
|
+
record.image.attach(image)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def record
|
52
|
+
@record ||= blob.variant_records.find_by(variation_digest: variation.digest)
|
53
|
+
end
|
54
|
+
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "mimemagic"
|
4
|
+
|
3
5
|
# A set of transformations that can be applied to a blob to create a variant. This class is exposed via
|
4
6
|
# the ActiveStorage::Blob#variant method and should rarely be used directly.
|
5
7
|
#
|
@@ -43,38 +45,41 @@ class ActiveStorage::Variation
|
|
43
45
|
@transformations = transformations.deep_symbolize_keys
|
44
46
|
end
|
45
47
|
|
48
|
+
def default_to(defaults)
|
49
|
+
self.class.new transformations.reverse_merge(defaults)
|
50
|
+
end
|
51
|
+
|
46
52
|
# Accepts a File object, performs the +transformations+ against it, and
|
47
|
-
# saves the transformed image into a temporary file.
|
48
|
-
|
49
|
-
# retains the source format.
|
50
|
-
def transform(file, format: nil, &block)
|
53
|
+
# saves the transformed image into a temporary file.
|
54
|
+
def transform(file, &block)
|
51
55
|
ActiveSupport::Notifications.instrument("transform.active_storage") do
|
52
56
|
transformer.transform(file, format: format, &block)
|
53
57
|
end
|
54
58
|
end
|
55
59
|
|
60
|
+
def format
|
61
|
+
transformations.fetch(:format, :png).tap do |format|
|
62
|
+
if MimeMagic.by_extension(format).nil?
|
63
|
+
raise ArgumentError, "Invalid variant format (#{format.inspect})"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def content_type
|
69
|
+
MimeMagic.by_extension(format).to_s
|
70
|
+
end
|
71
|
+
|
56
72
|
# Returns a signed key for all the +transformations+ that this variation was instantiated with.
|
57
73
|
def key
|
58
74
|
self.class.encode(transformations)
|
59
75
|
end
|
60
76
|
|
77
|
+
def digest
|
78
|
+
Digest::SHA1.base64digest Marshal.dump(transformations)
|
79
|
+
end
|
80
|
+
|
61
81
|
private
|
62
82
|
def transformer
|
63
|
-
|
64
|
-
begin
|
65
|
-
require "image_processing"
|
66
|
-
rescue LoadError
|
67
|
-
ActiveSupport::Deprecation.warn <<~WARNING.squish
|
68
|
-
Generating image variants will require the image_processing gem in Rails 6.1.
|
69
|
-
Please add `gem 'image_processing', '~> 1.2'` to your Gemfile.
|
70
|
-
WARNING
|
71
|
-
|
72
|
-
ActiveStorage::Transformers::MiniMagickTransformer.new(transformations)
|
73
|
-
else
|
74
|
-
ActiveStorage::Transformers::ImageProcessingTransformer.new(transformations)
|
75
|
-
end
|
76
|
-
else
|
77
|
-
ActiveStorage::Transformers::MiniMagickTransformer.new(transformations)
|
78
|
-
end
|
83
|
+
ActiveStorage::Transformers::ImageProcessingTransformer.new(transformations.except(:format))
|
79
84
|
end
|
80
85
|
end
|