shrine 2.19.4 → 3.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +485 -43
- data/LICENSE.txt +1 -1
- data/README.md +81 -977
- data/doc/advantages.md +231 -204
- data/doc/attacher.md +304 -153
- data/doc/carrierwave.md +297 -226
- data/doc/changing_derivatives.md +308 -0
- data/doc/changing_location.md +102 -21
- data/doc/changing_storage.md +110 -0
- data/doc/creating_persistence_plugins.md +132 -0
- data/doc/creating_plugins.md +43 -23
- data/doc/creating_storages.md +19 -5
- data/doc/design.md +147 -97
- data/doc/direct_s3.md +38 -28
- data/doc/external/articles.md +63 -0
- data/doc/external/extensions.md +53 -0
- data/doc/external/misc.md +32 -0
- data/doc/getting_started.md +1115 -0
- data/doc/metadata.md +190 -109
- data/doc/multiple_files.md +62 -34
- data/doc/paperclip.md +384 -262
- data/doc/plugins/activerecord.md +177 -46
- data/doc/plugins/add_metadata.md +139 -38
- data/doc/plugins/atomic_helpers.md +217 -0
- data/doc/plugins/backgrounding.md +156 -98
- data/doc/plugins/cached_attachment_data.md +7 -5
- data/doc/plugins/column.md +121 -0
- data/doc/plugins/data_uri.md +23 -22
- data/doc/plugins/default_storage.md +36 -10
- data/doc/plugins/default_url.md +30 -13
- data/doc/plugins/delete_raw.md +4 -2
- data/doc/plugins/derivation_endpoint.md +162 -101
- data/doc/plugins/derivatives.md +829 -0
- data/doc/plugins/determine_mime_type.md +4 -2
- data/doc/plugins/download_endpoint.md +64 -8
- data/doc/plugins/dynamic_storage.md +5 -3
- data/doc/plugins/entity.md +263 -0
- data/doc/plugins/form_assign.md +55 -0
- data/doc/plugins/included.md +31 -8
- data/doc/plugins/infer_extension.md +21 -10
- data/doc/plugins/instrumentation.md +38 -16
- data/doc/plugins/keep_files.md +14 -17
- data/doc/plugins/metadata_attributes.md +42 -13
- data/doc/plugins/mirroring.md +118 -0
- data/doc/plugins/model.md +210 -0
- data/doc/plugins/module_include.md +4 -2
- data/doc/plugins/multi_cache.md +24 -0
- data/doc/plugins/persistence.md +101 -0
- data/doc/plugins/presign_endpoint.md +9 -4
- data/doc/plugins/pretty_location.md +16 -3
- data/doc/plugins/processing.md +4 -2
- data/doc/plugins/rack_file.md +8 -2
- data/doc/plugins/rack_response.md +6 -2
- data/doc/plugins/recache.md +4 -2
- data/doc/plugins/refresh_metadata.md +49 -9
- data/doc/plugins/remote_url.md +84 -47
- data/doc/plugins/remove_attachment.md +27 -6
- data/doc/plugins/remove_invalid.md +21 -6
- data/doc/plugins/restore_cached_data.md +11 -3
- data/doc/plugins/sequel.md +159 -35
- data/doc/plugins/signature.md +16 -5
- data/doc/plugins/store_dimensions.md +14 -2
- data/doc/plugins/tempfile.md +4 -2
- data/doc/plugins/type_predicates.md +96 -0
- data/doc/plugins/upload_endpoint.md +13 -13
- data/doc/plugins/upload_options.md +6 -4
- data/doc/plugins/{default_url_options.md → url_options.md} +9 -7
- data/doc/plugins/validation.md +97 -0
- data/doc/plugins/validation_helpers.md +16 -13
- data/doc/plugins/versions.md +15 -19
- data/doc/processing.md +438 -221
- data/doc/refile.md +185 -167
- data/doc/release_notes/1.0.0.md +4 -0
- data/doc/release_notes/1.1.0.md +6 -2
- data/doc/release_notes/1.2.0.md +4 -0
- data/doc/release_notes/1.3.0.md +4 -0
- data/doc/release_notes/1.4.0.md +4 -0
- data/doc/release_notes/1.4.1.md +4 -0
- data/doc/release_notes/1.4.2.md +4 -0
- data/doc/release_notes/2.0.0.md +4 -0
- data/doc/release_notes/2.0.1.md +4 -0
- data/doc/release_notes/2.1.0.md +4 -0
- data/doc/release_notes/2.1.1.md +4 -0
- data/doc/release_notes/2.10.0.md +4 -0
- data/doc/release_notes/2.10.1.md +4 -0
- data/doc/release_notes/2.11.0.md +4 -0
- data/doc/release_notes/2.12.0.md +4 -0
- data/doc/release_notes/2.13.0.md +4 -0
- data/doc/release_notes/2.14.0.md +5 -1
- data/doc/release_notes/2.15.0.md +11 -7
- data/doc/release_notes/2.16.0.md +4 -0
- data/doc/release_notes/2.17.0.md +4 -0
- data/doc/release_notes/2.18.0.md +4 -0
- data/doc/release_notes/2.19.0.md +6 -3
- data/doc/release_notes/2.2.0.md +4 -0
- data/doc/release_notes/2.3.0.md +4 -0
- data/doc/release_notes/2.3.1.md +4 -0
- data/doc/release_notes/2.4.0.md +4 -0
- data/doc/release_notes/2.4.1.md +4 -0
- data/doc/release_notes/2.5.0.md +4 -0
- data/doc/release_notes/2.6.0.md +4 -0
- data/doc/release_notes/2.6.1.md +4 -0
- data/doc/release_notes/2.7.0.md +4 -0
- data/doc/release_notes/2.8.0.md +4 -0
- data/doc/release_notes/2.9.0.md +4 -0
- data/doc/release_notes/3.0.0.md +981 -0
- data/doc/release_notes/3.0.1.md +22 -0
- data/doc/release_notes/3.1.0.md +73 -0
- data/doc/release_notes/3.2.0.md +96 -0
- data/doc/release_notes/3.2.1.md +31 -0
- data/doc/release_notes/3.2.2.md +14 -0
- data/doc/release_notes/3.3.0.md +105 -0
- data/doc/release_notes/3.4.0.md +35 -0
- data/doc/retrieving_uploads.md +4 -1
- data/doc/securing_uploads.md +60 -37
- data/doc/storage/file_system.md +20 -3
- data/doc/storage/memory.md +19 -0
- data/doc/storage/s3.md +117 -83
- data/doc/testing.md +124 -144
- data/doc/upgrading_to_3.md +710 -0
- data/doc/validation.md +54 -90
- data/lib/shrine/attacher.rb +287 -171
- data/lib/shrine/attachment.rb +13 -46
- data/lib/shrine/plugins/_persistence.rb +93 -0
- data/lib/shrine/plugins/activerecord.rb +77 -34
- data/lib/shrine/plugins/add_metadata.rb +25 -17
- data/lib/shrine/plugins/atomic_helpers.rb +119 -0
- data/lib/shrine/plugins/backgrounding.rb +77 -113
- data/lib/shrine/plugins/cached_attachment_data.rb +6 -15
- data/lib/shrine/plugins/column.rb +102 -0
- data/lib/shrine/plugins/data_uri.rb +38 -36
- data/lib/shrine/plugins/default_storage.rb +45 -15
- data/lib/shrine/plugins/default_url.rb +12 -24
- data/lib/shrine/plugins/default_url_options.rb +3 -30
- data/lib/shrine/plugins/delete_raw.rb +10 -16
- data/lib/shrine/plugins/derivation_endpoint.rb +89 -134
- data/lib/shrine/plugins/derivatives.rb +637 -0
- data/lib/shrine/plugins/determine_mime_type.rb +9 -21
- data/lib/shrine/plugins/download_endpoint.rb +109 -133
- data/lib/shrine/plugins/dynamic_storage.rb +5 -11
- data/lib/shrine/plugins/entity.rb +152 -0
- data/lib/shrine/plugins/form_assign.rb +108 -0
- data/lib/shrine/plugins/included.rb +6 -6
- data/lib/shrine/plugins/infer_extension.rb +13 -20
- data/lib/shrine/plugins/instrumentation.rb +54 -42
- data/lib/shrine/plugins/keep_files.rb +3 -15
- data/lib/shrine/plugins/metadata_attributes.rb +28 -19
- data/lib/shrine/plugins/mirroring.rb +142 -0
- data/lib/shrine/plugins/model.rb +158 -0
- data/lib/shrine/plugins/module_include.rb +3 -3
- data/lib/shrine/plugins/multi_cache.rb +27 -0
- data/lib/shrine/plugins/presign_endpoint.rb +18 -22
- data/lib/shrine/plugins/pretty_location.rb +15 -9
- data/lib/shrine/plugins/processing.rb +22 -9
- data/lib/shrine/plugins/rack_file.rb +2 -42
- data/lib/shrine/plugins/rack_response.rb +15 -10
- data/lib/shrine/plugins/recache.rb +6 -5
- data/lib/shrine/plugins/refresh_metadata.rb +13 -11
- data/lib/shrine/plugins/remote_url.rb +49 -49
- data/lib/shrine/plugins/remove_attachment.rb +10 -6
- data/lib/shrine/plugins/remove_invalid.rb +19 -8
- data/lib/shrine/plugins/restore_cached_data.rb +13 -7
- data/lib/shrine/plugins/sequel.rb +86 -36
- data/lib/shrine/plugins/signature.rb +10 -16
- data/lib/shrine/plugins/store_dimensions.rb +35 -40
- data/lib/shrine/plugins/tempfile.rb +1 -3
- data/lib/shrine/plugins/type_predicates.rb +113 -0
- data/lib/shrine/plugins/upload_endpoint.rb +25 -23
- data/lib/shrine/plugins/upload_options.rb +14 -15
- data/lib/shrine/plugins/url_options.rb +31 -0
- data/lib/shrine/plugins/validation.rb +80 -0
- data/lib/shrine/plugins/validation_helpers.rb +34 -57
- data/lib/shrine/plugins/versions.rb +107 -87
- data/lib/shrine/plugins.rb +22 -0
- data/lib/shrine/storage/file_system.rb +46 -64
- data/lib/shrine/storage/linter.rb +42 -7
- data/lib/shrine/storage/memory.rb +49 -0
- data/lib/shrine/storage/s3.rb +154 -158
- data/lib/shrine/uploaded_file.rb +28 -30
- data/lib/shrine/version.rb +3 -3
- data/lib/shrine.rb +86 -149
- data/shrine.gemspec +9 -10
- metadata +79 -83
- data/doc/migrating_storage.md +0 -76
- data/doc/plugins/backup.md +0 -31
- data/doc/plugins/copy.md +0 -24
- data/doc/plugins/delete_promoted.md +0 -12
- data/doc/plugins/direct_upload.md +0 -172
- data/doc/plugins/hooks.md +0 -58
- data/doc/plugins/logging.md +0 -42
- data/doc/plugins/migration_helpers.md +0 -60
- data/doc/plugins/moving.md +0 -19
- data/doc/plugins/multi_delete.md +0 -20
- data/doc/plugins/parallelize.md +0 -16
- data/doc/plugins/parsed_json.md +0 -23
- data/doc/regenerating_versions.md +0 -143
- data/lib/shrine/plugins/background_helpers.rb +0 -5
- data/lib/shrine/plugins/backup.rb +0 -90
- data/lib/shrine/plugins/copy.rb +0 -50
- data/lib/shrine/plugins/delete_promoted.rb +0 -20
- data/lib/shrine/plugins/direct_upload.rb +0 -217
- data/lib/shrine/plugins/hooks.rb +0 -90
- data/lib/shrine/plugins/logging.rb +0 -142
- data/lib/shrine/plugins/migration_helpers.rb +0 -70
- data/lib/shrine/plugins/moving.rb +0 -57
- data/lib/shrine/plugins/multi_delete.rb +0 -32
- data/lib/shrine/plugins/parallelize.rb +0 -78
- data/lib/shrine/plugins/parsed_json.rb +0 -29
@@ -0,0 +1,710 @@
|
|
1
|
+
---
|
2
|
+
id: upgrading-to-3
|
3
|
+
title: Upgrading to Shrine 3.x
|
4
|
+
---
|
5
|
+
|
6
|
+
This guide provides instructions for upgrading Shrine in your apps to version
|
7
|
+
3.x. If you're looking for a full list of changes, see the **[3.0 release
|
8
|
+
notes]**.
|
9
|
+
|
10
|
+
If you would like assistance with the upgrade, I'm available for consultation,
|
11
|
+
you can email me at <janko.marohnic@gmail.com>.
|
12
|
+
|
13
|
+
## Attacher
|
14
|
+
|
15
|
+
The `Shrine::Attacher` class has been rewritten in Shrine 3.0, though much of
|
16
|
+
the main API remained the same.
|
17
|
+
|
18
|
+
### Model
|
19
|
+
|
20
|
+
The main change is that `Attacher.new` is now used for initializing the
|
21
|
+
attacher without a model:
|
22
|
+
|
23
|
+
```rb
|
24
|
+
attacher = Shrine::Attacher.new
|
25
|
+
#=> #<Shrine::Attacher>
|
26
|
+
|
27
|
+
attacher = Shrine::Attacher.new(photo, :image)
|
28
|
+
# ~> ArgumentError: invalid number of arguments
|
29
|
+
```
|
30
|
+
|
31
|
+
To initialize an attacher with a model, use `Attacher.from_model` provided by
|
32
|
+
the new [`model`][model] plugin (which is automatically loaded by
|
33
|
+
`activerecord` and `sequel` plugins):
|
34
|
+
|
35
|
+
```rb
|
36
|
+
attacher = Shrine::Attacher.from_model(photo, :image)
|
37
|
+
# ...
|
38
|
+
```
|
39
|
+
|
40
|
+
If you're using the `Shrine::Attachment` module with POROs, make sure to load
|
41
|
+
the `model` plugin.
|
42
|
+
|
43
|
+
```rb
|
44
|
+
Shrine.plugin :model
|
45
|
+
```
|
46
|
+
```rb
|
47
|
+
class Photo < Struct.new(:image_data)
|
48
|
+
include Shrine::Attachment(:image)
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
### Data attribute
|
53
|
+
|
54
|
+
The `Attacher#read` method has been removed. If you want to generate serialized
|
55
|
+
attachment data, use `Attacher#column_data`. Otherwise if you want to generate
|
56
|
+
hash attachment data, use `Attacher#data`.
|
57
|
+
|
58
|
+
```rb
|
59
|
+
attacher.column_data #=> '{"id":"...","storage":"...","metadata":{...}}'
|
60
|
+
attacher.data #=> { "id" => "...", "storage" => "...", "metadata" => { ... } }
|
61
|
+
```
|
62
|
+
|
63
|
+
The `Attacher#data_attribute` has been renamed to `Attacher#attribute`.
|
64
|
+
|
65
|
+
### State
|
66
|
+
|
67
|
+
The attacher now maintains its own state, so if you've previously modified the
|
68
|
+
`#<name>_data` record attribute and expected the changes to be picked up by the
|
69
|
+
attacher, you'll now need to call `Attacher#reload` for that:
|
70
|
+
|
71
|
+
```rb
|
72
|
+
attacher.file #=> nil
|
73
|
+
record.image_data = '{"id":"...","storage":"...","metadata":{...}}'
|
74
|
+
attacher.file #=> nil
|
75
|
+
attacher.reload
|
76
|
+
attacher.file #=> #<Shrine::UploadedFile ...>
|
77
|
+
```
|
78
|
+
|
79
|
+
### Assigning
|
80
|
+
|
81
|
+
The `Attacher#assign` method now raises an exception when non-cached uploaded
|
82
|
+
file data is assigned:
|
83
|
+
|
84
|
+
```rb
|
85
|
+
# Shrine 2.x
|
86
|
+
attacher.assign('{"id": "...", "storage": "store", "metadata": {...}}') # ignored
|
87
|
+
|
88
|
+
# Shrine 3.0
|
89
|
+
attacher.assign('{"id": "...", "storage": "store", "metadata": {...}}')
|
90
|
+
#~> Shrine::Error: expected cached file, got #<Shrine::UploadedFile storage=:store ...>
|
91
|
+
```
|
92
|
+
|
93
|
+
### Validation
|
94
|
+
|
95
|
+
The validation functionality has been extracted into the `validation` plugin.
|
96
|
+
If you're using the `validation_helpers` plugin, it will automatically load
|
97
|
+
`validation` for you. Otherwise you'll have to load it explicitly:
|
98
|
+
|
99
|
+
```rb
|
100
|
+
Shrine.plugin :validation
|
101
|
+
```
|
102
|
+
```rb
|
103
|
+
class MyUploader < Shrine
|
104
|
+
Attacher.validate do
|
105
|
+
# ...
|
106
|
+
end
|
107
|
+
end
|
108
|
+
```
|
109
|
+
|
110
|
+
### Setting
|
111
|
+
|
112
|
+
The `Attacher#set` method has been renamed to `Attacher#change`, and the
|
113
|
+
private `Attacher#_set` method has been renamed to `Attacher#set` and made
|
114
|
+
public:
|
115
|
+
|
116
|
+
```rb
|
117
|
+
attacher.change(uploaded_file) # sets file, remembers previous file, runs validations
|
118
|
+
attacher.set(uploaded_file) # sets file
|
119
|
+
```
|
120
|
+
|
121
|
+
If you've previously used `Attacher#replace` directly to delete previous file,
|
122
|
+
it has now been renamed to `Attacher#destroy_previous`.
|
123
|
+
|
124
|
+
Also note that `Attacher#attached?` now returns whether a file is attached,
|
125
|
+
while `Attacher#changed?` continues to return whether the attachment has
|
126
|
+
changed.
|
127
|
+
|
128
|
+
### Uploading and deleting
|
129
|
+
|
130
|
+
The `Attacher#store!` and `Attacher#cache!` methods have been removed, you
|
131
|
+
should now use `Attacher#upload` instead:
|
132
|
+
|
133
|
+
```rb
|
134
|
+
attacher.upload(io) # uploads to permanent storage
|
135
|
+
attacher.upload(io, :cache) # uploads to temporary storage
|
136
|
+
attacher.upload(io, :other_store) # uploads to another storage
|
137
|
+
```
|
138
|
+
|
139
|
+
The `Attacher#delete!` method has been removed as well, you should instead just
|
140
|
+
delete the file directly via `UploadedFile#delete`.
|
141
|
+
|
142
|
+
### Promoting
|
143
|
+
|
144
|
+
If you were promoting manually, the `Attacher#promote` method will now only
|
145
|
+
save promoted file in memory, it won't persist the changes.
|
146
|
+
|
147
|
+
```rb
|
148
|
+
attacher.promote
|
149
|
+
# ...
|
150
|
+
record.save # you need to persist the changes
|
151
|
+
```
|
152
|
+
|
153
|
+
If you want the concurrenct-safe promotion with persistence, use the new
|
154
|
+
`Attacher#atomic_promote` method.
|
155
|
+
|
156
|
+
```rb
|
157
|
+
attacher.atomic_promote
|
158
|
+
```
|
159
|
+
|
160
|
+
The `Attacher#swap` method has been removed. If you were using it directly, you
|
161
|
+
can use `Attacher#set` and `Attacher#atomic_persist` instead:
|
162
|
+
|
163
|
+
```rb
|
164
|
+
current_file = attacher.file
|
165
|
+
attacher.set(new_file)
|
166
|
+
attacher.atomic_persist(current_file)
|
167
|
+
```
|
168
|
+
|
169
|
+
## Backgrounding
|
170
|
+
|
171
|
+
The `backgrounding` plugin has been rewritten in Shrine 3.0 and has a new API.
|
172
|
+
|
173
|
+
```rb
|
174
|
+
Shrine.plugin :backgrounding
|
175
|
+
Shrine::Attacher.promote_block do
|
176
|
+
PromoteJob.perform_async(self.class.name, record.class.name, record.id, name, file_data)
|
177
|
+
end
|
178
|
+
Shrine::Attacher.destroy_block do
|
179
|
+
DestroyJob.perform_async(self.class.name, data)
|
180
|
+
end
|
181
|
+
```
|
182
|
+
```rb
|
183
|
+
class PromoteJob
|
184
|
+
include Sidekiq::Worker
|
185
|
+
|
186
|
+
def perform(attacher_class, record_class, record_id, name, file_data)
|
187
|
+
attacher_class = Object.const_get(attacher_class)
|
188
|
+
record = Object.const_get(record_class).find(record_id) # if using Active Record
|
189
|
+
|
190
|
+
attacher = attacher_class.retrieve(model: record, name: name, file: file_data)
|
191
|
+
attacher.atomic_promote
|
192
|
+
rescue Shrine::AttachmentChanged, ActiveRecord::RecordNotFound
|
193
|
+
# attachment has changed or record has been deleted, nothing to do
|
194
|
+
end
|
195
|
+
end
|
196
|
+
```
|
197
|
+
```rb
|
198
|
+
class DestroyJob
|
199
|
+
include Sidekiq::Worker
|
200
|
+
|
201
|
+
def perform(attacher_class, data)
|
202
|
+
attacher_class = Object.const_get(attacher_class)
|
203
|
+
|
204
|
+
attacher = attacher_class.from_data(data)
|
205
|
+
attacher.destroy
|
206
|
+
end
|
207
|
+
end
|
208
|
+
```
|
209
|
+
|
210
|
+
### Dual support
|
211
|
+
|
212
|
+
When you're making the switch in production, there might still be jobs in the
|
213
|
+
queue that have the old argument format. So, we'll initially want to handle
|
214
|
+
both argument formats, and then switch to the new one once the jobs with old
|
215
|
+
format have been drained.
|
216
|
+
|
217
|
+
```rb
|
218
|
+
class PromoteJob
|
219
|
+
include Sidekiq::Worker
|
220
|
+
|
221
|
+
def perform(*args)
|
222
|
+
if args.one?
|
223
|
+
file_data, (record_class, record_id), name, shrine_class =
|
224
|
+
args.first.values_at("attachment", "record", "name", "shrine_class")
|
225
|
+
|
226
|
+
record = Object.const_get(record_class).find(record_id) # if using Active Record
|
227
|
+
attacher_class = Object.const_get(shrine_class)::Attacher
|
228
|
+
else
|
229
|
+
attacher_class, record_class, record_id, name, file_data = args
|
230
|
+
|
231
|
+
attacher_class = Object.const_get(attacher_class)
|
232
|
+
record = Object.const_get(record_class).find(record_id) # if using Active Record
|
233
|
+
end
|
234
|
+
|
235
|
+
attacher = attacher_class.retrieve(model: record, name: name, file: file_data)
|
236
|
+
attacher.atomic_promote
|
237
|
+
rescue Shrine::AttachmentChanged, ActiveRecord::RecordNotFound
|
238
|
+
# attachment has changed or record has been deleted, nothing to do
|
239
|
+
end
|
240
|
+
end
|
241
|
+
```
|
242
|
+
```rb
|
243
|
+
class DestroyJob
|
244
|
+
include Sidekiq::Worker
|
245
|
+
|
246
|
+
def perform(*args)
|
247
|
+
if args.one?
|
248
|
+
data, shrine_class = args.first.values_at("attachment", "shrine_class")
|
249
|
+
|
250
|
+
data = JSON.parse(data)
|
251
|
+
attacher_class = Object.const_get(shrine_class)::Attacher
|
252
|
+
else
|
253
|
+
attacher_class, data = args
|
254
|
+
|
255
|
+
attacher_class = Object.const_get(attacher_class)
|
256
|
+
end
|
257
|
+
|
258
|
+
attacher = attacher_class.from_data(data)
|
259
|
+
attacher.destroy
|
260
|
+
end
|
261
|
+
end
|
262
|
+
```
|
263
|
+
|
264
|
+
### Attacher backgrounding
|
265
|
+
|
266
|
+
In Shrine 2.x, `Attacher#_promote` and `Attacher#_delete` methods could be used
|
267
|
+
to spawn promote and delete jobs. This is now done by `Attacher#promote_cached`
|
268
|
+
and `Attacher#destroy_attached`:
|
269
|
+
|
270
|
+
```rb
|
271
|
+
attacher.promote_cached # will spawn background job if registered
|
272
|
+
attacher.destroy_attached # will spawn background job if registered
|
273
|
+
```
|
274
|
+
|
275
|
+
If you want to explicitly call backgrounding blocks, you can use
|
276
|
+
`Attacher#promote_background` and `Attacher#destroy_background`:
|
277
|
+
|
278
|
+
```rb
|
279
|
+
attacher.promote_background # calls promote block
|
280
|
+
attacher.destroy_background # calls destroy block
|
281
|
+
```
|
282
|
+
|
283
|
+
## Versions
|
284
|
+
|
285
|
+
The `versions`, `processing`, `recache`, and `delete_raw` plugins have been
|
286
|
+
deprecated in favour of the new **[`derivatives`][derivatives]** plugin.
|
287
|
+
|
288
|
+
Let's assume you have the following `versions` configuration:
|
289
|
+
|
290
|
+
```rb
|
291
|
+
class ImageUploader < Shrine
|
292
|
+
plugin :processing
|
293
|
+
plugin :versions
|
294
|
+
plugin :delete_raw
|
295
|
+
|
296
|
+
process(:store) do |file, context|
|
297
|
+
versions = { original: file }
|
298
|
+
|
299
|
+
file.download do |original|
|
300
|
+
magick = ImageProcessing::MiniMagick.source(original)
|
301
|
+
|
302
|
+
versions[:large] = magick.resize_to_limit!(800, 800)
|
303
|
+
versions[:medium] = magick.resize_to_limit!(500, 500)
|
304
|
+
versions[:small] = magick.resize_to_limit!(300, 300)
|
305
|
+
end
|
306
|
+
|
307
|
+
versions
|
308
|
+
end
|
309
|
+
end
|
310
|
+
```
|
311
|
+
|
312
|
+
When an attached file is promoted to permanent storage, the versions would
|
313
|
+
automatically get generated:
|
314
|
+
|
315
|
+
```rb
|
316
|
+
photo = Photo.new(photo_params)
|
317
|
+
|
318
|
+
if photo.valid?
|
319
|
+
photo.save # generates versions on promotion
|
320
|
+
# ...
|
321
|
+
else
|
322
|
+
# ...
|
323
|
+
end
|
324
|
+
```
|
325
|
+
|
326
|
+
With `derivatives`, the original file is automatically downloaded and retained
|
327
|
+
during processing, so the setup is simpler:
|
328
|
+
|
329
|
+
```rb
|
330
|
+
Shrine.plugin :derivatives,
|
331
|
+
create_on_promote: true, # automatically create derivatives on promotion
|
332
|
+
versions_compatibility: true # handle versions column format
|
333
|
+
```
|
334
|
+
```rb
|
335
|
+
class ImageUploader < Shrine
|
336
|
+
Attacher.derivatives do |original|
|
337
|
+
magick = ImageProcessing::MiniMagick.source(original)
|
338
|
+
|
339
|
+
# the :original file should NOT be included anymore
|
340
|
+
{
|
341
|
+
large: magick.resize_to_limit!(800, 800),
|
342
|
+
medium: magick.resize_to_limit!(500, 500),
|
343
|
+
small: magick.resize_to_limit!(300, 300),
|
344
|
+
}
|
345
|
+
end
|
346
|
+
end
|
347
|
+
```
|
348
|
+
```rb
|
349
|
+
photo = Photo.new(photo_params)
|
350
|
+
|
351
|
+
if photo.valid?
|
352
|
+
photo.save # creates derivatives on promotion
|
353
|
+
# ...
|
354
|
+
else
|
355
|
+
# ...
|
356
|
+
end
|
357
|
+
```
|
358
|
+
|
359
|
+
### Accessing derivatives
|
360
|
+
|
361
|
+
The derivative URLs are accessed in the same way as versions:
|
362
|
+
|
363
|
+
```rb
|
364
|
+
photo.image_url(:small)
|
365
|
+
```
|
366
|
+
|
367
|
+
But the files themselves are accessed differently:
|
368
|
+
|
369
|
+
```rb
|
370
|
+
# versions
|
371
|
+
photo.image #=>
|
372
|
+
# {
|
373
|
+
# original: #<Shrine::UploadedFile ...>,
|
374
|
+
# large: #<Shrine::UploadedFile ...>,
|
375
|
+
# medium: #<Shrine::UploadedFile ...>,
|
376
|
+
# small: #<Shrine::UploadedFile ...>,
|
377
|
+
# }
|
378
|
+
photo.image[:medium] #=> #<Shrine::UploadedFile ...>
|
379
|
+
```
|
380
|
+
```rb
|
381
|
+
# derivatives
|
382
|
+
photo.image_derivatives #=>
|
383
|
+
# {
|
384
|
+
# large: #<Shrine::UploadedFile ...>,
|
385
|
+
# medium: #<Shrine::UploadedFile ...>,
|
386
|
+
# small: #<Shrine::UploadedFile ...>,
|
387
|
+
# }
|
388
|
+
photo.image(:medium) #=> #<Shrine::UploadedFile ...>
|
389
|
+
```
|
390
|
+
|
391
|
+
### Migrating versions
|
392
|
+
|
393
|
+
The `versions` and `derivatives` plugins save processed file data to the
|
394
|
+
database column in different formats:
|
395
|
+
|
396
|
+
```rb
|
397
|
+
# versions
|
398
|
+
{
|
399
|
+
"original": { "id": "...", "storage": "...", "metadata": { ... } },
|
400
|
+
"large": { "id": "...", "storage": "...", "metadata": { ... } },
|
401
|
+
"medium": { "id": "...", "storage": "...", "metadata": { ... } },
|
402
|
+
"small": { "id": "...", "storage": "...", "metadata": { ... } }
|
403
|
+
}
|
404
|
+
```
|
405
|
+
```rb
|
406
|
+
# derivatives
|
407
|
+
{
|
408
|
+
"id": "...",
|
409
|
+
"storage": "...",
|
410
|
+
"metadata": { ... },
|
411
|
+
"derivatives": {
|
412
|
+
"large": { "id": "...", "storage": "...", "metadata": { ... } },
|
413
|
+
"medium": { "id": "...", "storage": "...", "metadata": { ... } },
|
414
|
+
"small": { "id": "...", "storage": "...", "metadata": { ... } }
|
415
|
+
}
|
416
|
+
}
|
417
|
+
```
|
418
|
+
|
419
|
+
The `:versions_compatibility` flag to the `derivatives` plugin enables it to
|
420
|
+
read the `versions` format, which aids in transition. Once the `derivatives`
|
421
|
+
plugin has been deployed to production, you can update existing records with
|
422
|
+
the new column format:
|
423
|
+
|
424
|
+
```rb
|
425
|
+
Photo.find_each do |photo|
|
426
|
+
photo.image_attacher.write
|
427
|
+
photo.image_attacher.atomic_persist
|
428
|
+
end
|
429
|
+
```
|
430
|
+
|
431
|
+
Afterwards you should be able to remove the `:versions_compatibility` flag.
|
432
|
+
|
433
|
+
### Backgrounding derivatives
|
434
|
+
|
435
|
+
If you're using the `backgrounding` plugin, you can trigger derivatives
|
436
|
+
creation in the `PromoteJob` instead of the controller:
|
437
|
+
|
438
|
+
```rb
|
439
|
+
class PromoteJob
|
440
|
+
include Sidekiq::Worker
|
441
|
+
|
442
|
+
def perform(attacher_class, record_class, record_id, name, file_data)
|
443
|
+
attacher_class = Object.const_get(attacher_class)
|
444
|
+
record = Object.const_get(record_class).find(record_id) # if using Active Record
|
445
|
+
|
446
|
+
attacher = attacher_class.retrieve(model: record, name: name, file: file_data)
|
447
|
+
attacher.create_derivatives # call derivatives processor
|
448
|
+
attacher.atomic_promote
|
449
|
+
rescue Shrine::AttachmentChanged, ActiveRecord::RecordNotFound
|
450
|
+
# attachment has changed or record has beeen deleted, nothing to do
|
451
|
+
end
|
452
|
+
end
|
453
|
+
```
|
454
|
+
|
455
|
+
#### Recache
|
456
|
+
|
457
|
+
If you were using the `recache` plugin, you can replicate the behaviour by
|
458
|
+
creating another derivatives processor that you will trigger in the controller:
|
459
|
+
|
460
|
+
```rb
|
461
|
+
class ImageUploader < Shrine
|
462
|
+
Attacher.derivatives do |original|
|
463
|
+
# this will be triggered in the background job
|
464
|
+
end
|
465
|
+
|
466
|
+
Attacher.derivatives :foreground do |original|
|
467
|
+
# this will be triggered in the controller
|
468
|
+
end
|
469
|
+
end
|
470
|
+
```
|
471
|
+
```rb
|
472
|
+
photo = Photo.new(photo_params)
|
473
|
+
|
474
|
+
if photo.valid?
|
475
|
+
photo.image_derivatives!(:foreground) if photo.image_changed?
|
476
|
+
photo.save
|
477
|
+
# ...
|
478
|
+
else
|
479
|
+
# ...
|
480
|
+
end
|
481
|
+
```
|
482
|
+
|
483
|
+
### Default URL
|
484
|
+
|
485
|
+
If you were using the `default_url` plugin, the `Attacher.default_url` now
|
486
|
+
receives a `:derivative` option:
|
487
|
+
|
488
|
+
```rb
|
489
|
+
Attacher.default_url do |derivative: nil, **|
|
490
|
+
"https://my-app.com/fallbacks/#{derivative}.jpg" if derivative
|
491
|
+
end
|
492
|
+
```
|
493
|
+
|
494
|
+
#### Fallback to original
|
495
|
+
|
496
|
+
With the `versions` plugin, a missing version URL would automatically fall back
|
497
|
+
to the original file. The `derivatives` plugin has no such fallback, but you
|
498
|
+
can configure it manually:
|
499
|
+
|
500
|
+
```rb
|
501
|
+
Attacher.default_url do |derivative: nil, **|
|
502
|
+
file&.url if derivative
|
503
|
+
end
|
504
|
+
```
|
505
|
+
|
506
|
+
#### Fallback to version
|
507
|
+
|
508
|
+
The `versions` plugin had the ability to fall back missing version URL to
|
509
|
+
another version that already exists. The `derivatives` plugin doesn't have this
|
510
|
+
built in, but you can implement it as follows:
|
511
|
+
|
512
|
+
```rb
|
513
|
+
DERIVATIVE_FALLBACKS = { foo: :bar, ... }
|
514
|
+
|
515
|
+
Attacher.default_url do |derivative: nil, **|
|
516
|
+
derivatives[DERIVATIVE_FALLBACKS[derivative]]&.url if derivative
|
517
|
+
end
|
518
|
+
```
|
519
|
+
|
520
|
+
### Location
|
521
|
+
|
522
|
+
The `Shrine#generate_location` method will now receive a `:derivative`
|
523
|
+
parameter instead of `:version`:
|
524
|
+
|
525
|
+
```rb
|
526
|
+
class MyUploader < Shrine
|
527
|
+
def generate_location(io, derivative: nil, **)
|
528
|
+
derivative #=> :large, :medium, :small, ...
|
529
|
+
# ...
|
530
|
+
end
|
531
|
+
end
|
532
|
+
```
|
533
|
+
|
534
|
+
### Overwriting original
|
535
|
+
|
536
|
+
With the `derivatives` plugin, saving processed files separately from the
|
537
|
+
original file, so the original file is automatically kept. This means it's not
|
538
|
+
possible anymore to overwrite the original file as part of processing.
|
539
|
+
|
540
|
+
However, **it's highly recommended to always keep the original file**, even if
|
541
|
+
you don't plan to use it. That way, if there is ever a need to reprocess
|
542
|
+
derivatives, you have the original file to use as a base.
|
543
|
+
|
544
|
+
That being said, if you still want to overwrite the original file, [this
|
545
|
+
thread][overwriting original] has some tips.
|
546
|
+
|
547
|
+
## Other
|
548
|
+
|
549
|
+
### Processing
|
550
|
+
|
551
|
+
The `processing` plugin has been deprecated over the new
|
552
|
+
[`derivatives`][derivatives] plugin. If you were previously replacing the
|
553
|
+
original file:
|
554
|
+
|
555
|
+
```rb
|
556
|
+
class MyUploader < Shrine
|
557
|
+
plugin :processing
|
558
|
+
|
559
|
+
process(:store) do |io, context|
|
560
|
+
ImageProcessing::MiniMagick
|
561
|
+
.source(io.download)
|
562
|
+
.resize_to_limit!(1600, 1600)
|
563
|
+
end
|
564
|
+
end
|
565
|
+
```
|
566
|
+
|
567
|
+
you should now add the processed file as a derivative:
|
568
|
+
|
569
|
+
```rb
|
570
|
+
class MyUploader < Shrine
|
571
|
+
plugin :derivatives
|
572
|
+
|
573
|
+
Attacher.derivatives do |original|
|
574
|
+
magick = ImageProcessing::MiniMagick.source(original)
|
575
|
+
|
576
|
+
{ normalized: magick.resize_to_limit!(1600, 1600) }
|
577
|
+
end
|
578
|
+
end
|
579
|
+
```
|
580
|
+
|
581
|
+
### Parallelize
|
582
|
+
|
583
|
+
The `parallelize` plugin has been removed. With `derivatives` plugin you can
|
584
|
+
parallelize uploading processed files manually:
|
585
|
+
|
586
|
+
```rb
|
587
|
+
# Gemfile
|
588
|
+
gem "concurrent-ruby"
|
589
|
+
```
|
590
|
+
```rb
|
591
|
+
require "concurrent"
|
592
|
+
|
593
|
+
attacher = photo.image_attacher
|
594
|
+
derivatives = attacher.process_derivatives
|
595
|
+
|
596
|
+
tasks = derivatives.map do |name, file|
|
597
|
+
Concurrent::Promises.future(name, file) do |name, file|
|
598
|
+
attacher.add_derivative(name, file)
|
599
|
+
end
|
600
|
+
end
|
601
|
+
|
602
|
+
Concurrent::Promises.zip(*tasks).wait!
|
603
|
+
```
|
604
|
+
|
605
|
+
### Logging
|
606
|
+
|
607
|
+
The `logging` plugin has been removed in favour of the
|
608
|
+
[`instrumentation`][instrumentation] plugin. You can replace code like
|
609
|
+
|
610
|
+
```rb
|
611
|
+
Shrine.plugin :logging, logger: Rails.logger
|
612
|
+
```
|
613
|
+
|
614
|
+
with
|
615
|
+
|
616
|
+
```rb
|
617
|
+
Shrine.logger = Rails.logger
|
618
|
+
|
619
|
+
Shrine.plugin :instrumentation
|
620
|
+
```
|
621
|
+
|
622
|
+
### Backup
|
623
|
+
|
624
|
+
The `backup` plugin has been removed in favour of the new
|
625
|
+
[`mirroring`][mirroring] plugin. You can replace code like
|
626
|
+
|
627
|
+
```rb
|
628
|
+
Shrine.plugin :backup, storage: :backup_store
|
629
|
+
```
|
630
|
+
|
631
|
+
with
|
632
|
+
|
633
|
+
```rb
|
634
|
+
Shrine.plugin :mirroring, mirror: { store: :backup_store }
|
635
|
+
```
|
636
|
+
|
637
|
+
### Copy
|
638
|
+
|
639
|
+
The `copy` plugin has been removed as its behaviour can now be achieved easily.
|
640
|
+
You can replace code like
|
641
|
+
|
642
|
+
```rb
|
643
|
+
Shrine.plugin :copy
|
644
|
+
```
|
645
|
+
```rb
|
646
|
+
attacher.copy(other_attacher)
|
647
|
+
```
|
648
|
+
|
649
|
+
with
|
650
|
+
|
651
|
+
```rb
|
652
|
+
attacher.set attacher.upload(other_attacher.file)
|
653
|
+
attacher.add_derivatives other_attacher.derivatives # if using derivatives
|
654
|
+
```
|
655
|
+
|
656
|
+
### Moving
|
657
|
+
|
658
|
+
The `moving` plugin has been removed in favour of the `:move` option for
|
659
|
+
`FileSystem#upload`. You can set this option as default using the
|
660
|
+
`upload_options` plugin (the example assumes both `:cache` and `:store` are
|
661
|
+
FileSystem storages):
|
662
|
+
|
663
|
+
```rb
|
664
|
+
Shrine.plugin :upload_options, cache: { move: true }, store: { move: true }
|
665
|
+
```
|
666
|
+
|
667
|
+
### Parsed JSON
|
668
|
+
|
669
|
+
The `parsed_json` plugin has been removed as it's now the default behaviour.
|
670
|
+
|
671
|
+
```rb
|
672
|
+
# this now works by default
|
673
|
+
photo.image = { "id" => "d7e54d6ef2.jpg", "storage" => "cache", "metadata" => { ... } }
|
674
|
+
```
|
675
|
+
|
676
|
+
### Module Include
|
677
|
+
|
678
|
+
The `module_include` plugin has been deprecated over overriding core classes
|
679
|
+
directly. You can replace code like
|
680
|
+
|
681
|
+
```rb
|
682
|
+
class MyUploader < Shrine
|
683
|
+
plugin :module_include
|
684
|
+
|
685
|
+
file_methods do
|
686
|
+
def image?
|
687
|
+
mime_type.start_with?("image")
|
688
|
+
end
|
689
|
+
end
|
690
|
+
end
|
691
|
+
```
|
692
|
+
|
693
|
+
with
|
694
|
+
|
695
|
+
```rb
|
696
|
+
class MyUploader < Shrine
|
697
|
+
class UploadedFile
|
698
|
+
def image?
|
699
|
+
mime_type.start_with?("image")
|
700
|
+
end
|
701
|
+
end
|
702
|
+
end
|
703
|
+
```
|
704
|
+
|
705
|
+
[3.0 release notes]: https://shrinerb.com/docs/release_notes/3.0.0
|
706
|
+
[model]: https://shrinerb.com/docs/plugins/model
|
707
|
+
[derivatives]: https://shrinerb.com/docs/plugins/derivatives
|
708
|
+
[instrumentation]: https://shrinerb.com/docs/plugins/instrumentation
|
709
|
+
[mirroring]: https://shrinerb.com/docs/plugins/mirroring
|
710
|
+
[overwriting original]: https://discourse.shrinerb.com/t/keep-original-file-after-processing/50/4
|