activestorage 5.2.7.1 → 6.1.4.6
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activestorage might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +225 -93
- data/MIT-LICENSE +1 -1
- data/README.md +43 -8
- data/app/assets/javascripts/activestorage.js +5 -2
- data/app/controllers/active_storage/base_controller.rb +13 -4
- data/app/controllers/active_storage/blobs/proxy_controller.rb +14 -0
- data/app/controllers/active_storage/{blobs_controller.rb → blobs/redirect_controller.rb} +3 -3
- data/app/controllers/active_storage/direct_uploads_controller.rb +2 -2
- data/app/controllers/active_storage/disk_controller.rb +13 -22
- data/app/controllers/active_storage/representations/base_controller.rb +14 -0
- data/app/controllers/active_storage/representations/proxy_controller.rb +13 -0
- data/app/controllers/active_storage/{representations_controller.rb → representations/redirect_controller.rb} +3 -5
- 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 +15 -0
- data/app/controllers/concerns/active_storage/set_headers.rb +12 -0
- data/app/javascript/activestorage/blob_record.js +7 -2
- data/app/jobs/active_storage/analyze_job.rb +5 -0
- data/app/jobs/active_storage/base_job.rb +0 -1
- data/app/jobs/active_storage/mirror_job.rb +15 -0
- data/app/jobs/active_storage/purge_job.rb +3 -0
- data/app/models/active_storage/attachment.rb +35 -16
- 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 +36 -6
- data/app/models/active_storage/blob.rb +186 -68
- data/app/models/active_storage/filename.rb +0 -6
- data/app/models/active_storage/preview.rb +37 -12
- data/app/models/active_storage/record.rb +7 -0
- data/app/models/active_storage/variant.rb +53 -67
- 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 +30 -94
- data/config/routes.rb +66 -15
- 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/analyzer/image_analyzer.rb +14 -4
- data/lib/active_storage/analyzer/null_analyzer.rb +4 -0
- data/lib/active_storage/analyzer/video_analyzer.rb +17 -8
- data/lib/active_storage/analyzer.rb +15 -4
- data/lib/active_storage/attached/changes/create_many.rb +47 -0
- data/lib/active_storage/attached/changes/create_one.rb +82 -0
- data/lib/active_storage/attached/changes/create_one_of_many.rb +10 -0
- data/lib/active_storage/attached/changes/delete_many.rb +27 -0
- data/lib/active_storage/attached/changes/delete_one.rb +19 -0
- data/lib/active_storage/attached/changes.rb +16 -0
- data/lib/active_storage/attached/many.rb +19 -12
- data/lib/active_storage/attached/model.rb +212 -0
- data/lib/active_storage/attached/one.rb +19 -21
- data/lib/active_storage/attached.rb +7 -22
- data/lib/active_storage/downloader.rb +43 -0
- data/lib/active_storage/engine.rb +60 -38
- data/lib/active_storage/errors.rb +25 -3
- data/lib/active_storage/gem_version.rb +4 -4
- data/lib/active_storage/log_subscriber.rb +6 -0
- 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 +17 -10
- data/lib/active_storage/previewer.rb +34 -14
- data/lib/active_storage/reflection.rb +64 -0
- data/lib/active_storage/service/azure_storage_service.rb +65 -44
- data/lib/active_storage/service/configurator.rb +6 -2
- data/lib/active_storage/service/disk_service.rb +57 -44
- data/lib/active_storage/service/gcs_service.rb +68 -64
- 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 +56 -24
- data/lib/active_storage/service.rb +44 -12
- data/lib/active_storage/transformers/image_processing_transformer.rb +45 -0
- data/lib/active_storage/transformers/transformer.rb +39 -0
- data/lib/active_storage.rb +31 -296
- data/lib/tasks/activestorage.rake +11 -0
- metadata +82 -16
- data/app/models/active_storage/filename/parameters.rb +0 -36
- data/lib/active_storage/attached/macros.rb +0 -110
- data/lib/active_storage/downloading.rb +0 -39
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4f78821f730cf6d374a408a0b130b92437182d3026404916ca79618e7c8b2ffd
|
4
|
+
data.tar.gz: 613fab9e9ce486a0897f55c0cf654e0edd98549cdc963813367289651f1bb03e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf9329ba6d4500c9f31b0390fabd11854354d3ad6b131280e148912487deb119168dfe35a4fb92db4ee55708c665065b76845f815c96b26557405eb0e13a71a3
|
7
|
+
data.tar.gz: 88cbbc25f7b4d8cbeb5eb57805d79f4d7df2288391835a9abe3aace1680a265edb369bf284dfc99713be94e74998323474f87bb70cb89b7ba7a01273ced37b3d
|
data/CHANGELOG.md
CHANGED
@@ -1,204 +1,336 @@
|
|
1
|
-
## Rails
|
1
|
+
## Rails 6.1.4.6 (February 11, 2022) ##
|
2
2
|
|
3
3
|
* No changes.
|
4
4
|
|
5
5
|
|
6
|
-
## Rails
|
6
|
+
## Rails 6.1.4.5 (February 11, 2022) ##
|
7
7
|
|
8
|
-
*
|
9
|
-
`ActiveStorage.unsupported_image_processing_arguments` that were not being applied.
|
8
|
+
* No changes.
|
10
9
|
|
11
|
-
*Rafael Mendonça França*
|
12
10
|
|
11
|
+
## Rails 6.1.4.4 (December 15, 2021) ##
|
13
12
|
|
14
|
-
|
13
|
+
* No changes.
|
15
14
|
|
16
|
-
* Added image transformation validation via configurable allow-list.
|
17
15
|
|
18
|
-
|
19
|
-
transformation methods in addition to a configurable deny-list for arguments.
|
16
|
+
## Rails 6.1.4.3 (December 14, 2021) ##
|
20
17
|
|
21
|
-
|
18
|
+
* No changes.
|
22
19
|
|
23
20
|
|
24
|
-
## Rails
|
21
|
+
## Rails 6.1.4.2 (December 14, 2021) ##
|
25
22
|
|
26
23
|
* No changes.
|
27
24
|
|
28
25
|
|
29
|
-
## Rails
|
26
|
+
## Rails 6.1.4.1 (August 19, 2021) ##
|
30
27
|
|
31
28
|
* No changes.
|
32
29
|
|
33
30
|
|
34
|
-
## Rails
|
31
|
+
## Rails 6.1.4 (June 24, 2021) ##
|
35
32
|
|
36
|
-
*
|
33
|
+
* The parameters sent to `ffmpeg` for generating a video preview image are now
|
34
|
+
configurable under `config.active_storage.video_preview_arguments`.
|
37
35
|
|
36
|
+
*Brendon Muir*
|
38
37
|
|
39
|
-
|
38
|
+
* Fix Active Storage update task when running in an engine.
|
40
39
|
|
41
|
-
|
42
|
-
mime types data.
|
40
|
+
Justin Malčić*
|
43
41
|
|
44
|
-
|
42
|
+
* Don't raise an error if the mime type is not recognized.
|
45
43
|
|
46
|
-
|
47
|
-
document's crop box rather than its media box, hiding print margins. This
|
48
|
-
matches the behavior of the MuPDF previewer.
|
44
|
+
Fixes #41777.
|
49
45
|
|
50
|
-
*
|
46
|
+
*Alex Ghiculescu*
|
51
47
|
|
48
|
+
* `ActiveStorage::PreviewError` is raised when a previewer is unable to generate a preview image.
|
52
49
|
|
53
|
-
|
50
|
+
*Alex Robbin*
|
54
51
|
|
55
|
-
*
|
52
|
+
* respond with 404 given invalid variation key when asking for representations.
|
56
53
|
|
54
|
+
*George Claghorn*
|
57
55
|
|
58
|
-
|
56
|
+
* `Blob` creation shouldn't crash if no service selected.
|
59
57
|
|
60
|
-
*
|
58
|
+
*Alex Ghiculescu*
|
61
59
|
|
62
60
|
|
63
|
-
## Rails
|
61
|
+
## Rails 6.1.3.2 (May 05, 2021) ##
|
64
62
|
|
65
63
|
* No changes.
|
66
64
|
|
67
65
|
|
68
|
-
## Rails
|
66
|
+
## Rails 6.1.3.1 (March 26, 2021) ##
|
67
|
+
|
68
|
+
* Marcel is upgraded to version 1.0.0 to avoid a dependency on GPL-licensed
|
69
|
+
mime types data.
|
69
70
|
|
70
|
-
*
|
71
|
+
*George Claghorn*
|
71
72
|
|
72
73
|
|
73
|
-
## Rails
|
74
|
+
## Rails 6.1.3 (February 17, 2021) ##
|
74
75
|
|
75
76
|
* No changes.
|
76
77
|
|
77
78
|
|
78
|
-
## Rails
|
79
|
+
## Rails 6.1.2.1 (February 10, 2021) ##
|
79
80
|
|
80
81
|
* No changes.
|
81
82
|
|
82
83
|
|
83
|
-
## Rails
|
84
|
+
## Rails 6.1.2 (February 09, 2021) ##
|
84
85
|
|
85
86
|
* No changes.
|
86
87
|
|
87
88
|
|
88
|
-
## Rails
|
89
|
+
## Rails 6.1.1 (January 07, 2021) ##
|
89
90
|
|
90
|
-
*
|
91
|
+
* Fix S3 multipart uploads when threshold is larger than file.
|
91
92
|
|
93
|
+
*Matt Muller*
|
92
94
|
|
93
|
-
## Rails 5.2.2.1 (March 11, 2019) ##
|
94
95
|
|
95
|
-
|
96
|
+
## Rails 6.1.0 (December 09, 2020) ##
|
97
|
+
|
98
|
+
* Change default queue name of the analysis (`:active_storage_analysis`) and
|
99
|
+
purge (`:active_storage_purge`) jobs to be the job adapter's default (`:default`).
|
100
|
+
|
101
|
+
*Rafael Mendonça França*
|
96
102
|
|
103
|
+
* Implement `strict_loading` on ActiveStorage associations.
|
97
104
|
|
98
|
-
|
105
|
+
*David Angulo*
|
99
106
|
|
100
|
-
*
|
107
|
+
* Remove deprecated support to pass `:combine_options` operations to `ActiveStorage::Transformers::ImageProcessing`.
|
101
108
|
|
102
|
-
*
|
109
|
+
*Rafael Mendonça França*
|
103
110
|
|
104
|
-
*
|
111
|
+
* Remove deprecated `ActiveStorage::Transformers::MiniMagickTransformer`.
|
105
112
|
|
106
|
-
*
|
113
|
+
*Rafael Mendonça França*
|
107
114
|
|
108
|
-
*
|
115
|
+
* Remove deprecated `config.active_storage.queue`.
|
109
116
|
|
110
|
-
*
|
117
|
+
*Rafael Mendonça França*
|
111
118
|
|
112
|
-
*
|
119
|
+
* Remove deprecated `ActiveStorage::Downloading`.
|
113
120
|
|
114
|
-
*
|
121
|
+
*Rafael Mendonça França*
|
115
122
|
|
116
|
-
*
|
123
|
+
* Add per-environment configuration support
|
117
124
|
|
118
|
-
*
|
125
|
+
*Pietro Moro*
|
126
|
+
|
127
|
+
* The Poppler PDF previewer renders a preview image using the original
|
128
|
+
document's crop box rather than its media box, hiding print margins. This
|
129
|
+
matches the behavior of the MuPDF previewer.
|
130
|
+
|
131
|
+
*Vincent Robert*
|
132
|
+
|
133
|
+
* Touch parent model when an attachment is purged.
|
134
|
+
|
135
|
+
*Víctor Pérez Rodríguez*
|
136
|
+
|
137
|
+
* Files can now be served by proxying them from the underlying storage service
|
138
|
+
instead of redirecting to a signed service URL. Use the
|
139
|
+
`rails_storage_proxy_path` and `_url` helpers to proxy an attached file:
|
140
|
+
|
141
|
+
```erb
|
142
|
+
<%= image_tag rails_storage_proxy_path(@user.avatar) %>
|
143
|
+
```
|
119
144
|
|
145
|
+
To proxy by default, set `config.active_storage.resolve_model_to_route`:
|
120
146
|
|
121
|
-
|
147
|
+
```ruby
|
148
|
+
# Proxy attached files instead.
|
149
|
+
config.active_storage.resolve_model_to_route = :rails_storage_proxy
|
150
|
+
```
|
122
151
|
|
123
|
-
|
152
|
+
```erb
|
153
|
+
<%= image_tag @user.avatar %>
|
154
|
+
```
|
124
155
|
|
125
|
-
|
156
|
+
To redirect to a signed service URL when the default file serving strategy
|
157
|
+
is set to proxying, use the `rails_storage_redirect_path` and `_url` helpers:
|
126
158
|
|
127
|
-
|
159
|
+
```erb
|
160
|
+
<%= image_tag rails_storage_redirect_path(@user.avatar) %>
|
161
|
+
```
|
128
162
|
|
163
|
+
*Jonathan Fleckenstein*
|
129
164
|
|
130
|
-
|
165
|
+
* Add `config.active_storage.web_image_content_types` to allow applications
|
166
|
+
to add content types (like `image/webp`) in which variants can be processed,
|
167
|
+
instead of letting those images be converted to the fallback PNG format.
|
131
168
|
|
132
|
-
*
|
169
|
+
*Jeroen van Haperen*
|
170
|
+
|
171
|
+
* Add support for creating variants of `WebP` images out of the box.
|
172
|
+
|
173
|
+
*Dino Maric*
|
174
|
+
|
175
|
+
* Only enqueue analysis jobs for blobs with non-null analyzer classes.
|
176
|
+
|
177
|
+
*Gannon McGibbon*
|
178
|
+
|
179
|
+
* Previews are created on the same service as the original blob.
|
180
|
+
|
181
|
+
*Peter Zhu*
|
182
|
+
|
183
|
+
* Remove unused `disposition` and `content_type` query parameters for `DiskService`.
|
184
|
+
|
185
|
+
*Peter Zhu*
|
186
|
+
|
187
|
+
* Use `DiskController` for both public and private files.
|
188
|
+
|
189
|
+
`DiskController` is able to handle multiple services by adding a
|
190
|
+
`service_name` field in the generated URL in `DiskService`.
|
191
|
+
|
192
|
+
*Peter Zhu*
|
193
|
+
|
194
|
+
* Variants are tracked in the database to avoid existence checks in the storage service.
|
133
195
|
|
134
196
|
*George Claghorn*
|
135
197
|
|
136
|
-
*
|
198
|
+
* Deprecate `service_url` methods in favour of `url`.
|
199
|
+
|
200
|
+
Deprecate `Variant#service_url` and `Preview#service_url` to instead use
|
201
|
+
`#url` method to be consistent with `Blob`.
|
202
|
+
|
203
|
+
*Peter Zhu*
|
137
204
|
|
138
|
-
|
205
|
+
* Permanent URLs for public storage blobs.
|
139
206
|
|
207
|
+
Services can be configured in `config/storage.yml` with a new key
|
208
|
+
`public: true | false` to indicate whether a service holds public
|
209
|
+
blobs or private blobs. Public services will always return a permanent URL.
|
140
210
|
|
141
|
-
|
211
|
+
Deprecates `Blob#service_url` in favor of `Blob#url`.
|
142
212
|
|
143
|
-
*
|
144
|
-
explicit AWS key pair and/or region is not provided in `storage.yml`,
|
145
|
-
attempt to use environment variables, shared credentials, or IAM
|
146
|
-
(instance or task) role credentials. Order of precedence is determined
|
147
|
-
by the [AWS SDK](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/setup-config.html).
|
213
|
+
*Peter Zhu*
|
148
214
|
|
149
|
-
|
215
|
+
* Make services aware of configuration names.
|
150
216
|
|
151
|
-
*
|
217
|
+
*Gannon McGibbon*
|
152
218
|
|
153
|
-
|
154
|
-
that is ambiguous in meaning. It needs to be set to the primary blob
|
155
|
-
storage endpoint but that can be determined from the blobs client anyway.
|
219
|
+
* The `Content-Type` header is set on image variants when they're uploaded to third-party storage services.
|
156
220
|
|
157
|
-
|
158
|
-
now get the endpoint from the blobs client instead.
|
221
|
+
*Kyle Ribordy*
|
159
222
|
|
160
|
-
|
223
|
+
* Allow storage services to be configured per attachment.
|
161
224
|
|
162
|
-
|
225
|
+
```ruby
|
226
|
+
class User < ActiveRecord::Base
|
227
|
+
has_one_attached :avatar, service: :s3
|
228
|
+
end
|
163
229
|
|
164
|
-
|
230
|
+
class Gallery < ActiveRecord::Base
|
231
|
+
has_many_attached :photos, service: :s3
|
232
|
+
end
|
233
|
+
```
|
165
234
|
|
166
|
-
|
235
|
+
*Dmitry Tsepelev*
|
236
|
+
|
237
|
+
* You can optionally provide a custom blob key when attaching a new file:
|
238
|
+
|
239
|
+
```ruby
|
240
|
+
user.avatar.attach key: "avatars/#{user.id}.jpg",
|
241
|
+
io: io, content_type: "image/jpeg", filename: "avatar.jpg"
|
242
|
+
```
|
243
|
+
|
244
|
+
Active Storage will store the blob's data on the configured service at the provided key.
|
167
245
|
|
168
246
|
*George Claghorn*
|
169
247
|
|
170
|
-
*
|
248
|
+
* Replace `Blob.create_after_upload!` with `Blob.create_and_upload!` and deprecate the former.
|
249
|
+
|
250
|
+
`create_after_upload!` has been removed since it could lead to data
|
251
|
+
corruption by uploading to a key on the storage service which happened to
|
252
|
+
be already taken. Creating the record would then correctly raise a
|
253
|
+
database uniqueness exception but the stored object would already have
|
254
|
+
overwritten another. `create_and_upload!` swaps the order of operations
|
255
|
+
so that the key gets reserved up-front or the uniqueness error gets raised,
|
256
|
+
before the upload to a key takes place.
|
171
257
|
|
172
|
-
|
173
|
-
rather than the compiled code, which can produce smaller javascript bundles.
|
258
|
+
*Julik Tarkhanov*
|
174
259
|
|
175
|
-
|
260
|
+
* Set content disposition in direct upload using `filename` and `disposition` parameters to `ActiveStorage::Service#headers_for_direct_upload`.
|
176
261
|
|
177
|
-
*
|
178
|
-
with rectangular samples in `ActiveStorage::Analyzer::VideoAnalyzer`.
|
262
|
+
*Peter Zhu*
|
179
263
|
|
180
|
-
|
181
|
-
|
182
|
-
|
264
|
+
* Allow record to be optionally passed to blob finders to make sharding
|
265
|
+
easier.
|
266
|
+
|
267
|
+
*Gannon McGibbon*
|
268
|
+
|
269
|
+
* Switch from `azure-storage` gem to `azure-storage-blob` gem for Azure service.
|
270
|
+
|
271
|
+
*Peter Zhu*
|
272
|
+
|
273
|
+
* Add `config.active_storage.draw_routes` to disable Active Storage routes.
|
274
|
+
|
275
|
+
*Gannon McGibbon*
|
276
|
+
|
277
|
+
* Image analysis is skipped if ImageMagick returns an error.
|
278
|
+
|
279
|
+
`ActiveStorage::Analyzer::ImageAnalyzer#metadata` would previously raise a
|
280
|
+
`MiniMagick::Error`, which caused persistent `ActiveStorage::AnalyzeJob`
|
281
|
+
failures. It now logs the error and returns `{}`, resulting in no metadata
|
282
|
+
being added to the offending image blob.
|
183
283
|
|
184
284
|
*George Claghorn*
|
185
285
|
|
186
|
-
*
|
187
|
-
|
286
|
+
* Method calls on singular attachments return `nil` when no file is attached.
|
287
|
+
|
288
|
+
Previously, assuming the following User model, `user.avatar.filename` would
|
289
|
+
raise a `Module::DelegationError` if no avatar was attached:
|
290
|
+
|
291
|
+
```ruby
|
292
|
+
class User < ApplicationRecord
|
293
|
+
has_one_attached :avatar
|
294
|
+
end
|
295
|
+
```
|
188
296
|
|
189
|
-
|
297
|
+
They now return `nil`.
|
190
298
|
|
191
|
-
*
|
192
|
-
This mitigates possible security issues such as XSS or phishing when
|
193
|
-
serving them inline. A list of such content types is included by default,
|
194
|
-
and can be configured via `content_types_to_serve_as_binary`.
|
299
|
+
*Matthew Tanous*
|
195
300
|
|
196
|
-
|
301
|
+
* The mirror service supports direct uploads.
|
197
302
|
|
198
|
-
|
303
|
+
New files are directly uploaded to the primary service. When a
|
304
|
+
directly-uploaded file is attached to a record, a background job is enqueued
|
305
|
+
to copy it to each secondary service.
|
199
306
|
|
200
|
-
|
307
|
+
Configure the queue used to process mirroring jobs by setting
|
308
|
+
`config.active_storage.queues.mirror`. The default is `:active_storage_mirror`.
|
309
|
+
|
310
|
+
*George Claghorn*
|
311
|
+
|
312
|
+
* The S3 service now permits uploading files larger than 5 gigabytes.
|
313
|
+
|
314
|
+
When uploading a file greater than 100 megabytes in size, the service
|
315
|
+
transparently switches to [multipart uploads](https://docs.aws.amazon.com/AmazonS3/latest/dev/mpuoverview.html)
|
316
|
+
using a part size computed from the file's total size and S3's part count limit.
|
317
|
+
|
318
|
+
No application changes are necessary to take advantage of this feature. You
|
319
|
+
can customize the default 100 MB multipart upload threshold in your S3
|
320
|
+
service's configuration:
|
321
|
+
|
322
|
+
```yaml
|
323
|
+
production:
|
324
|
+
service: s3
|
325
|
+
access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
|
326
|
+
secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
|
327
|
+
region: us-east-1
|
328
|
+
bucket: my-bucket
|
329
|
+
upload:
|
330
|
+
multipart_threshold: <%= 250.megabytes %>
|
331
|
+
```
|
332
|
+
|
333
|
+
*George Claghorn*
|
201
334
|
|
202
|
-
* Added to Rails.
|
203
335
|
|
204
|
-
|
336
|
+
Please check [6-0-stable](https://github.com/rails/rails/blob/6-0-stable/activestorage/CHANGELOG.md) for previous changes.
|
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
@@ -4,17 +4,21 @@ Active Storage makes it simple to upload and reference files in cloud services l
|
|
4
4
|
|
5
5
|
Files can be uploaded from the server to the cloud or directly from the client to the cloud.
|
6
6
|
|
7
|
-
Image files can furthermore be transformed using on-demand variants for quality, aspect ratio, size, or any other [MiniMagick](https://github.com/minimagick/minimagick) supported transformation.
|
7
|
+
Image files can furthermore be transformed using on-demand variants for quality, aspect ratio, size, or any other [MiniMagick](https://github.com/minimagick/minimagick) or [Vips](https://www.rubydoc.info/gems/ruby-vips/Vips/Image) supported transformation.
|
8
|
+
|
9
|
+
You can read more about Active Storage in the [Active Storage Overview](https://edgeguides.rubyonrails.org/active_storage_overview.html) guide.
|
8
10
|
|
9
11
|
## Compared to other storage solutions
|
10
12
|
|
11
|
-
A key difference to how Active Storage works compared to other attachment solutions in Rails is through the use of built-in [Blob](https://github.com/rails/rails/blob/
|
13
|
+
A key difference to how Active Storage works compared to other attachment solutions in Rails is through the use of built-in [Blob](https://github.com/rails/rails/blob/main/activestorage/app/models/active_storage/blob.rb) and [Attachment](https://github.com/rails/rails/blob/main/activestorage/app/models/active_storage/attachment.rb) models (backed by Active Record). This means existing application models do not need to be modified with additional columns to associate with files. Active Storage uses polymorphic associations via the `Attachment` join model, which then connects to the actual `Blob`.
|
12
14
|
|
13
15
|
`Blob` models store attachment metadata (filename, content-type, etc.), and their identifier key in the storage service. Blob models do not store the actual binary data. They are intended to be immutable in spirit. One file, one blob. You can associate the same blob with multiple application models as well. And if you want to do transformations of a given `Blob`, the idea is that you'll simply create a new one, rather than attempt to mutate the existing one (though of course you can delete the previous version later if you don't need it).
|
14
16
|
|
15
17
|
## Installation
|
16
18
|
|
17
|
-
Run `rails active_storage:install` to copy over active_storage migrations.
|
19
|
+
Run `bin/rails active_storage:install` to copy over active_storage migrations.
|
20
|
+
|
21
|
+
NOTE: If the task cannot be found, verify that `require "active_storage/engine"` is present in `config/application.rb`.
|
18
22
|
|
19
23
|
## Examples
|
20
24
|
|
@@ -51,7 +55,7 @@ url_for(user.avatar)
|
|
51
55
|
|
52
56
|
class AvatarsController < ApplicationController
|
53
57
|
def update
|
54
|
-
# params[:avatar] contains
|
58
|
+
# params[:avatar] contains an ActionDispatch::Http::UploadedFile object
|
55
59
|
Current.user.avatar.attach(params.require(:avatar))
|
56
60
|
redirect_to Current.user
|
57
61
|
end
|
@@ -99,7 +103,38 @@ Variation of image attachment:
|
|
99
103
|
|
100
104
|
```erb
|
101
105
|
<%# Hitting the variant URL will lazy transform the original blob and then redirect to its new service location %>
|
102
|
-
<%= image_tag user.avatar.variant(
|
106
|
+
<%= image_tag user.avatar.variant(resize_to_limit: [100, 100]) %>
|
107
|
+
```
|
108
|
+
|
109
|
+
## File serving strategies
|
110
|
+
|
111
|
+
Active Storage supports two ways to serve files: redirecting and proxying.
|
112
|
+
|
113
|
+
### Redirecting
|
114
|
+
|
115
|
+
Active Storage generates stable application URLs for files which, when accessed, redirect to signed, short-lived service URLs. This relieves application servers of the burden of serving file data. It is the default file serving strategy.
|
116
|
+
|
117
|
+
When the application is configured to proxy files by default, use the `rails_storage_redirect_path` and `_url` route helpers to redirect instead:
|
118
|
+
|
119
|
+
```erb
|
120
|
+
<%= image_tag rails_storage_redirect_path(@user.avatar) %>
|
121
|
+
```
|
122
|
+
|
123
|
+
### Proxying
|
124
|
+
|
125
|
+
Optionally, files can be proxied instead. This means that your application servers will download file data from the storage service in response to requests. This can be useful for serving files from a CDN.
|
126
|
+
|
127
|
+
Explicitly proxy attachments using the `rails_storage_proxy_path` and `_url` route helpers:
|
128
|
+
|
129
|
+
```erb
|
130
|
+
<%= image_tag rails_storage_proxy_path(@user.avatar) %>
|
131
|
+
```
|
132
|
+
|
133
|
+
Or configure Active Storage to use proxying by default:
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
# config/initializers/active_storage.rb
|
137
|
+
Rails.application.config.active_storage.resolve_model_to_route = :rails_storage_proxy
|
103
138
|
```
|
104
139
|
|
105
140
|
## Direct uploads
|
@@ -116,7 +151,7 @@ Active Storage, with its included JavaScript library, supports uploading directl
|
|
116
151
|
```
|
117
152
|
Using the npm package:
|
118
153
|
```js
|
119
|
-
import * as ActiveStorage from "activestorage"
|
154
|
+
import * as ActiveStorage from "@rails/activestorage"
|
120
155
|
ActiveStorage.start()
|
121
156
|
```
|
122
157
|
2. Annotate file inputs with the direct upload URL.
|
@@ -148,7 +183,7 @@ Active Storage is released under the [MIT License](https://opensource.org/licens
|
|
148
183
|
|
149
184
|
API documentation is at:
|
150
185
|
|
151
|
-
*
|
186
|
+
* https://api.rubyonrails.org
|
152
187
|
|
153
188
|
Bug reports for the Ruby on Rails project can be filed here:
|
154
189
|
|
@@ -156,4 +191,4 @@ Bug reports for the Ruby on Rails project can be filed here:
|
|
156
191
|
|
157
192
|
Feature requests should be discussed on the rails-core mailing list here:
|
158
193
|
|
159
|
-
* https://
|
194
|
+
* https://discuss.rubyonrails.org/c/rubyonrails-core
|
@@ -550,7 +550,7 @@
|
|
550
550
|
this.file = file;
|
551
551
|
this.attributes = {
|
552
552
|
filename: file.name,
|
553
|
-
content_type: file.type,
|
553
|
+
content_type: file.type || "application/octet-stream",
|
554
554
|
byte_size: file.size,
|
555
555
|
checksum: checksum
|
556
556
|
};
|
@@ -560,7 +560,10 @@
|
|
560
560
|
this.xhr.setRequestHeader("Content-Type", "application/json");
|
561
561
|
this.xhr.setRequestHeader("Accept", "application/json");
|
562
562
|
this.xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
563
|
-
|
563
|
+
var csrfToken = getMetaValue("csrf-token");
|
564
|
+
if (csrfToken != undefined) {
|
565
|
+
this.xhr.setRequestHeader("X-CSRF-Token", csrfToken);
|
566
|
+
}
|
564
567
|
this.xhr.addEventListener("load", function(event) {
|
565
568
|
return _this.requestDidLoad(event);
|
566
569
|
});
|
@@ -1,10 +1,19 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# The base
|
3
|
+
# The base class for all Active Storage controllers.
|
4
4
|
class ActiveStorage::BaseController < ActionController::Base
|
5
|
+
include ActiveStorage::SetCurrent
|
6
|
+
|
5
7
|
protect_from_forgery with: :exception
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
|
9
|
+
self.etag_with_template_digest = false
|
10
|
+
|
11
|
+
private
|
12
|
+
def stream(blob)
|
13
|
+
blob.download do |chunk|
|
14
|
+
response.stream.write chunk
|
15
|
+
end
|
16
|
+
ensure
|
17
|
+
response.stream.close
|
18
|
+
end
|
10
19
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Proxy files through application. This avoids having a redirect and makes files easier to cache.
|
4
|
+
class ActiveStorage::Blobs::ProxyController < ActiveStorage::BaseController
|
5
|
+
include ActiveStorage::SetBlob
|
6
|
+
include ActiveStorage::SetHeaders
|
7
|
+
|
8
|
+
def show
|
9
|
+
http_cache_forever public: true do
|
10
|
+
set_content_headers_from @blob
|
11
|
+
stream @blob
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -4,11 +4,11 @@
|
|
4
4
|
# Note: These URLs are publicly accessible. If you need to enforce access protection beyond the
|
5
5
|
# security-through-obscurity factor of the signed blob references, you'll need to implement your own
|
6
6
|
# authenticated redirection controller.
|
7
|
-
class ActiveStorage::
|
7
|
+
class ActiveStorage::Blobs::RedirectController < ActiveStorage::BaseController
|
8
8
|
include ActiveStorage::SetBlob
|
9
9
|
|
10
10
|
def show
|
11
|
-
expires_in ActiveStorage
|
12
|
-
redirect_to @blob.
|
11
|
+
expires_in ActiveStorage.service_urls_expire_in
|
12
|
+
redirect_to @blob.url(disposition: params[:disposition])
|
13
13
|
end
|
14
14
|
end
|
@@ -5,13 +5,13 @@
|
|
5
5
|
# the blob that was created up front.
|
6
6
|
class ActiveStorage::DirectUploadsController < ActiveStorage::BaseController
|
7
7
|
def create
|
8
|
-
blob = ActiveStorage::Blob.create_before_direct_upload!(blob_args)
|
8
|
+
blob = ActiveStorage::Blob.create_before_direct_upload!(**blob_args)
|
9
9
|
render json: direct_upload_json(blob)
|
10
10
|
end
|
11
11
|
|
12
12
|
private
|
13
13
|
def blob_args
|
14
|
-
params.require(:blob).permit(:filename, :byte_size, :checksum, :content_type, :
|
14
|
+
params.require(:blob).permit(:filename, :byte_size, :checksum, :content_type, metadata: {}).to_h.symbolize_keys
|
15
15
|
end
|
16
16
|
|
17
17
|
def direct_upload_json(blob)
|