shrine 3.0.0 → 3.2.2
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 shrine might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +87 -33
- data/LICENSE.txt +1 -1
- data/README.md +94 -4
- data/doc/advantages.md +35 -18
- data/doc/attacher.md +16 -17
- data/doc/carrierwave.md +75 -34
- data/doc/changing_derivatives.md +39 -39
- data/doc/design.md +134 -85
- data/doc/external/articles.md +56 -41
- data/doc/external/extensions.md +38 -34
- data/doc/getting_started.md +182 -112
- data/doc/metadata.md +79 -43
- data/doc/multiple_files.md +5 -3
- data/doc/paperclip.md +110 -42
- data/doc/plugins/activerecord.md +5 -5
- data/doc/plugins/add_metadata.md +92 -35
- data/doc/plugins/backgrounding.md +12 -2
- data/doc/plugins/column.md +36 -7
- data/doc/plugins/data_uri.md +2 -2
- data/doc/plugins/default_url.md +6 -3
- data/doc/plugins/derivation_endpoint.md +26 -28
- data/doc/plugins/derivatives.md +205 -169
- data/doc/plugins/determine_mime_type.md +2 -2
- data/doc/plugins/entity.md +3 -3
- data/doc/plugins/form_assign.md +5 -5
- data/doc/plugins/included.md +25 -5
- data/doc/plugins/infer_extension.md +2 -2
- data/doc/plugins/instrumentation.md +1 -1
- data/doc/plugins/metadata_attributes.md +21 -10
- data/doc/plugins/model.md +4 -4
- data/doc/plugins/persistence.md +1 -0
- data/doc/plugins/refresh_metadata.md +5 -4
- data/doc/plugins/remote_url.md +8 -3
- data/doc/plugins/remove_invalid.md +9 -1
- data/doc/plugins/sequel.md +4 -4
- data/doc/plugins/signature.md +11 -2
- data/doc/plugins/store_dimensions.md +2 -2
- data/doc/plugins/type_predicates.md +96 -0
- data/doc/plugins/upload_endpoint.md +7 -11
- data/doc/plugins/upload_options.md +1 -1
- data/doc/plugins/url_options.md +2 -2
- data/doc/plugins/validation.md +14 -4
- data/doc/plugins/validation_helpers.md +3 -3
- data/doc/plugins/versions.md +11 -11
- data/doc/processing.md +289 -125
- data/doc/refile.md +39 -18
- data/doc/release_notes/2.19.0.md +1 -1
- data/doc/release_notes/3.0.0.md +275 -258
- 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 +32 -0
- data/doc/release_notes/3.2.2.md +14 -0
- data/doc/securing_uploads.md +3 -3
- data/doc/storage/file_system.md +1 -1
- data/doc/storage/memory.md +19 -0
- data/doc/storage/s3.md +105 -86
- data/doc/testing.md +2 -2
- data/doc/upgrading_to_3.md +115 -33
- data/doc/validation.md +3 -2
- data/lib/shrine.rb +8 -8
- data/lib/shrine/attacher.rb +19 -14
- data/lib/shrine/attachment.rb +5 -5
- data/lib/shrine/plugins.rb +22 -0
- data/lib/shrine/plugins/add_metadata.rb +12 -3
- data/lib/shrine/plugins/default_storage.rb +6 -6
- data/lib/shrine/plugins/default_url.rb +1 -1
- data/lib/shrine/plugins/derivation_endpoint.rb +10 -6
- data/lib/shrine/plugins/derivatives.rb +19 -17
- data/lib/shrine/plugins/determine_mime_type.rb +3 -3
- data/lib/shrine/plugins/entity.rb +6 -6
- data/lib/shrine/plugins/metadata_attributes.rb +1 -1
- data/lib/shrine/plugins/model.rb +3 -3
- data/lib/shrine/plugins/presign_endpoint.rb +2 -2
- data/lib/shrine/plugins/pretty_location.rb +1 -1
- data/lib/shrine/plugins/processing.rb +1 -1
- data/lib/shrine/plugins/refresh_metadata.rb +2 -2
- data/lib/shrine/plugins/remote_url.rb +3 -3
- data/lib/shrine/plugins/remove_invalid.rb +10 -5
- data/lib/shrine/plugins/signature.rb +7 -6
- data/lib/shrine/plugins/store_dimensions.rb +18 -9
- data/lib/shrine/plugins/type_predicates.rb +113 -0
- data/lib/shrine/plugins/upload_endpoint.rb +3 -3
- data/lib/shrine/plugins/upload_options.rb +2 -2
- data/lib/shrine/plugins/url_options.rb +2 -2
- data/lib/shrine/plugins/validation.rb +9 -7
- data/lib/shrine/storage/linter.rb +4 -4
- data/lib/shrine/storage/s3.rb +62 -38
- data/lib/shrine/uploaded_file.rb +5 -1
- data/lib/shrine/version.rb +2 -2
- data/shrine.gemspec +6 -7
- metadata +23 -29
data/doc/plugins/derivatives.md
CHANGED
@@ -10,12 +10,11 @@ main attachment data in the same record attribute.
|
|
10
10
|
Shrine.plugin :derivatives
|
11
11
|
```
|
12
12
|
|
13
|
-
##
|
13
|
+
## Quick start
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
to create derivatives.
|
15
|
+
You'll usually want to create derivatives from an attached file. The simplest
|
16
|
+
way to do this is to define a processor which returns the processed files, and
|
17
|
+
then trigger it when you want to create derivatives.
|
19
18
|
|
20
19
|
Here is an example of generating image thumbnails:
|
21
20
|
|
@@ -27,7 +26,7 @@ gem "image_processing", "~> 1.8"
|
|
27
26
|
require "image_processing/mini_magick"
|
28
27
|
|
29
28
|
class ImageUploader < Shrine
|
30
|
-
Attacher.
|
29
|
+
Attacher.derivatives do |original|
|
31
30
|
magick = ImageProcessing::MiniMagick.source(original)
|
32
31
|
|
33
32
|
{
|
@@ -45,61 +44,160 @@ end
|
|
45
44
|
```
|
46
45
|
```rb
|
47
46
|
photo = Photo.new(image: file)
|
48
|
-
|
49
|
-
photo.
|
47
|
+
|
48
|
+
if photo.valid?
|
49
|
+
photo.image_derivatives! if photo.image_changed? # create derivatives
|
50
|
+
photo.save
|
51
|
+
end
|
50
52
|
```
|
51
53
|
|
52
|
-
|
53
|
-
route make sure to create derivatives for new attachments:
|
54
|
+
You can then retrieve the URL of a processed derivative:
|
54
55
|
|
55
56
|
```rb
|
56
|
-
photo.
|
57
|
+
photo.image_url(:large) #=> "https://s3.amazonaws.com/path/to/large.jpg"
|
57
58
|
```
|
58
59
|
|
59
|
-
|
60
|
-
|
60
|
+
The derivatives data is stored in the `<attachment>_data` column alongside the
|
61
|
+
main file:
|
61
62
|
|
62
63
|
```rb
|
63
64
|
photo.image_data #=>
|
64
65
|
# {
|
65
|
-
# "id": "original.jpg",
|
66
|
+
# "id": "path/to/original.jpg",
|
66
67
|
# "store": "store",
|
67
68
|
# "metadata": { ... },
|
68
69
|
# "derivatives": {
|
69
|
-
# "small": { "id": "small.jpg", "storage": "store", "metadata": { ... } },
|
70
|
-
# "medium": { "id": "medium.jpg", "storage": "store", "metadata": { ... } },
|
71
|
-
# "large": { "id": "large.jpg", "storage": "store", "metadata": { ... } },
|
70
|
+
# "small": { "id": "path/to/small.jpg", "storage": "store", "metadata": { ... } },
|
71
|
+
# "medium": { "id": "path/to/medium.jpg", "storage": "store", "metadata": { ... } },
|
72
|
+
# "large": { "id": "path/to/large.jpg", "storage": "store", "metadata": { ... } },
|
72
73
|
# }
|
73
74
|
# }
|
74
75
|
```
|
75
76
|
|
76
|
-
|
77
|
+
And they can be retrieved as `Shrine::UploadedFile` objects:
|
77
78
|
|
78
79
|
```rb
|
79
|
-
photo.image(:large) #=> #<Shrine::UploadedFile>
|
80
|
+
photo.image(:large) #=> #<Shrine::UploadedFile id="path/to/large.jpg" storage=:store metadata={...}>
|
80
81
|
photo.image(:large).url #=> "https://s3.amazonaws.com/path/to/large.jpg"
|
81
|
-
photo.image(:large).size #=>
|
82
|
+
photo.image(:large).size #=> 5825949
|
82
83
|
photo.image(:large).mime_type #=> "image/jpeg"
|
83
84
|
```
|
84
85
|
|
85
|
-
|
86
|
-
|
87
|
-
|
86
|
+
## Retrieving derivatives
|
87
|
+
|
88
|
+
The list of stored derivatives can be retrieved with `#<name>_derivatives`:
|
88
89
|
|
89
90
|
```rb
|
90
|
-
|
91
|
-
attacher.create_derivatives # calls registered processor and uploads results
|
92
|
-
attacher.derivatives #=>
|
91
|
+
photo.image_derivatives #=>
|
93
92
|
# {
|
94
|
-
# small: #<Shrine::UploadedFile
|
95
|
-
# medium: #<Shrine::UploadedFile
|
96
|
-
# large: #<Shrine::UploadedFile
|
93
|
+
# small: #<Shrine::UploadedFile ...>,
|
94
|
+
# medium: #<Shrine::UploadedFile ...>,
|
95
|
+
# large: #<Shrine::UploadedFile ...>,
|
97
96
|
# }
|
98
97
|
```
|
99
98
|
|
99
|
+
A specific derivative can be retrieved in any of the following ways:
|
100
|
+
|
101
|
+
```rb
|
102
|
+
photo.image_derivatives[:small] #=> #<Shrine::UploadedFile ...>
|
103
|
+
photo.image_derivatives(:small) #=> #<Shrine::UploadedFile ...>
|
104
|
+
photo.image(:small) #=> #<Shrine::UploadedFile ...>
|
105
|
+
```
|
106
|
+
|
107
|
+
Or with nested derivatives:
|
108
|
+
|
109
|
+
```rb
|
110
|
+
photo.image_derivatives #=> { thumbnail: { small: ..., medium: ..., large: ... } }
|
111
|
+
|
112
|
+
photo.image_derivatives.dig(:thumbnail, :small) #=> #<Shrine::UploadedFile ...>
|
113
|
+
photo.image_derivatives(:thumbnail, :small) #=> #<Shrine::UploadedFile ...>
|
114
|
+
photo.image(:thumbnails, :small) #=> #<Shrine::UploadedFile ...>
|
115
|
+
```
|
116
|
+
|
117
|
+
### Derivative URL
|
118
|
+
|
119
|
+
You can retrieve the URL of a derivative URL with `#<name>_url`:
|
120
|
+
|
121
|
+
```rb
|
122
|
+
photo.image_url(:small) #=> "https://example.com/small.jpg"
|
123
|
+
photo.image_url(:medium) #=> "https://example.com/medium.jpg"
|
124
|
+
photo.image_url(:large) #=> "https://example.com/large.jpg"
|
125
|
+
```
|
126
|
+
|
127
|
+
For nested derivatives you can pass multiple keys:
|
128
|
+
|
129
|
+
```rb
|
130
|
+
photo.image_derivatives #=> { thumbnail: { small: ..., medium: ..., large: ... } }
|
131
|
+
|
132
|
+
photo.image_url(:thumbnail, :medium) #=> "https://example.com/medium.jpg"
|
133
|
+
```
|
134
|
+
|
135
|
+
By default, `#<name>_url` method will return `nil` if derivative is not found.
|
136
|
+
You can use the [`default_url`][default_url] plugin to set up URL fallbacks:
|
137
|
+
|
138
|
+
```rb
|
139
|
+
Attacher.default_url do |derivative: nil, **|
|
140
|
+
"/fallbacks/#{derivative}.jpg" if derivative
|
141
|
+
end
|
142
|
+
```
|
143
|
+
```rb
|
144
|
+
photo.image_url(:medium) #=> "https://example.com/fallbacks.com/medium.jpg"
|
145
|
+
```
|
146
|
+
|
147
|
+
Any additional URL options passed to `#<name>_url` will be forwarded to the
|
148
|
+
storage:
|
149
|
+
|
150
|
+
```rb
|
151
|
+
photo.image_url(:small, response_content_disposition: "attachment")
|
152
|
+
```
|
153
|
+
|
154
|
+
You can also retrieve the derivative URL via `UploadedFile#url`:
|
155
|
+
|
156
|
+
```rb
|
157
|
+
photo.image_derivatives[:large].url
|
158
|
+
```
|
159
|
+
|
160
|
+
## Attacher API
|
161
|
+
|
162
|
+
The derivatives API is primarily defined on the `Shrine::Attacher` class, with
|
163
|
+
some important methods also being exposed through the `Shrine::Attachment`
|
164
|
+
module.
|
165
|
+
|
166
|
+
Here is a model example with equivalent attacher code:
|
167
|
+
|
168
|
+
```rb
|
169
|
+
photo.image_derivatives!(:thumbnails)
|
170
|
+
photo.image_derivatives #=> { ... }
|
171
|
+
|
172
|
+
photo.image_url(:large) #=> "https://..."
|
173
|
+
photo.image(:large) #=> #<Shrine::UploadedFile ...>
|
174
|
+
```
|
175
|
+
```rb
|
176
|
+
attacher.create_derivatives(:thumbnails)
|
177
|
+
attacher.get_derivatives #=> { ... }
|
178
|
+
|
179
|
+
attacher.url(:large) #=> "https://..."
|
180
|
+
attacher.get(:large) #=> "#<Shrine::UploadedFile>"
|
181
|
+
```
|
182
|
+
|
183
|
+
## Creating derivatives
|
184
|
+
|
100
185
|
By default, the `Attacher#create_derivatives` method downloads the attached
|
101
186
|
file, calls the processor, uploads results to attacher's permanent storage, and
|
102
|
-
saves uploaded files on the attacher.
|
187
|
+
saves uploaded files on the attacher.
|
188
|
+
|
189
|
+
```rb
|
190
|
+
attacher.file #=> #<Shrine::UploadedFile id="original.jpg" storage=:store ...>
|
191
|
+
attacher.create_derivatives # calls default processor and uploads results
|
192
|
+
attacher.derivatives #=>
|
193
|
+
# {
|
194
|
+
# small: #<Shrine::UploadedFile id="small.jpg" storage=:store ...>,
|
195
|
+
# medium: #<Shrine::UploadedFile id="medium.jpg" storage=:store ...>,
|
196
|
+
# large: #<Shrine::UploadedFile id="large.jpg" storage=:store ...>,
|
197
|
+
# }
|
198
|
+
```
|
199
|
+
|
200
|
+
Any additional arguments are forwarded to
|
103
201
|
[`Attacher#process_derivatives`](#processing-derivatives):
|
104
202
|
|
105
203
|
```rb
|
@@ -110,32 +208,43 @@ attacher.create_derivatives(foo: "bar") # pass custom options to the proce
|
|
110
208
|
### Naming processors
|
111
209
|
|
112
210
|
If you want to have multiple processors for an uploader, you can assign each
|
113
|
-
processor a name
|
114
|
-
the desired processor.
|
211
|
+
processor a name:
|
115
212
|
|
116
213
|
```rb
|
117
214
|
class ImageUploader < Shrine
|
118
|
-
Attacher.
|
119
|
-
|
215
|
+
Attacher.derivatives :thumbnails do |original|
|
216
|
+
{ large: ..., medium: ..., small: ... }
|
120
217
|
end
|
121
218
|
|
122
|
-
Attacher.
|
123
|
-
|
219
|
+
Attacher.derivatives :crop do |original|
|
220
|
+
{ cropped: ... }
|
124
221
|
end
|
125
|
-
|
126
|
-
# ...
|
127
222
|
end
|
128
223
|
```
|
224
|
+
|
225
|
+
Then when creating derivatives you can specify the name of the desired
|
226
|
+
processor. New derivatives will be merged with any existing ones.
|
227
|
+
|
129
228
|
```rb
|
130
|
-
photo.image_derivatives!(:thumbnails)
|
131
|
-
# or
|
132
229
|
attacher.create_derivatives(:thumbnails)
|
230
|
+
attacher.derivatives #=> { large: ..., medium: ..., small: ... }
|
231
|
+
|
232
|
+
attacher.create_derivatives(:crop)
|
233
|
+
attacher.derivatives #=> { large: ..., medium: ..., small: ..., cropped: ... }
|
133
234
|
```
|
134
235
|
|
135
236
|
### Derivatives storage
|
136
237
|
|
137
238
|
By default, derivatives are uploaded to the permanent storage of the attacher.
|
138
|
-
You can change the
|
239
|
+
You can change the destination storage by passing `:storage` to the creation
|
240
|
+
call:
|
241
|
+
|
242
|
+
```rb
|
243
|
+
attacher.create_derivatives(storage: :cache) # will be promoted together with main file
|
244
|
+
attacher.create_derivatives(storage: :other_store)
|
245
|
+
```
|
246
|
+
|
247
|
+
You can also change the default destination storage with the `:storage` plugin
|
139
248
|
option:
|
140
249
|
|
141
250
|
```rb
|
@@ -172,11 +281,14 @@ The storage block is evaluated in the context of a `Shrine::Attacher` instance:
|
|
172
281
|
|
173
282
|
```rb
|
174
283
|
Attacher.derivatives_storage do |derivative|
|
175
|
-
self
|
284
|
+
self #=> #<Shrine::Attacher>
|
176
285
|
|
286
|
+
file #=> #<Shrine::UploadedFile>
|
177
287
|
record #=> #<Photo>
|
178
288
|
name #=> :image
|
179
289
|
context #=> { ... }
|
290
|
+
|
291
|
+
# ...
|
180
292
|
end
|
181
293
|
```
|
182
294
|
|
@@ -186,7 +298,7 @@ Derivatives can be nested to any level, using both hashes and arrays, but the
|
|
186
298
|
top-level object must be a hash.
|
187
299
|
|
188
300
|
```rb
|
189
|
-
Attacher.
|
301
|
+
Attacher.derivatives :tiff do |original|
|
190
302
|
{
|
191
303
|
thumbnail: {
|
192
304
|
small: small,
|
@@ -201,114 +313,29 @@ Attacher.derivatives_processor :tiff do |original|
|
|
201
313
|
}
|
202
314
|
end
|
203
315
|
```
|
204
|
-
|
205
|
-
## Retrieving derivatives
|
206
|
-
|
207
|
-
If you're using the `Shrine::Attachment` module, you can retrieve stored
|
208
|
-
derivatives by calling `#<name>_derivatives` on your model/entity.
|
209
|
-
|
210
|
-
```rb
|
211
|
-
class Photo < Model(:image_data)
|
212
|
-
include ImageUploader::Attachment(:image)
|
213
|
-
end
|
214
|
-
```
|
215
|
-
```rb
|
216
|
-
photo.image_derivatives #=>
|
217
|
-
# {
|
218
|
-
# small: #<Shrine::UploadedFile>,
|
219
|
-
# medium: #<Shrine::UploadedFile>,
|
220
|
-
# large: #<Shrine::UploadedFile>,
|
221
|
-
# }
|
222
|
-
```
|
223
|
-
|
224
|
-
A specific derivative can be retrieved in any of the following ways:
|
225
|
-
|
226
|
-
```rb
|
227
|
-
photo.image_derivatives[:small] #=> #<Shrine::UploadedFile>
|
228
|
-
photo.image_derivatives(:small) #=> #<Shrine::UploadedFile>
|
229
|
-
photo.image(:small) #=> #<Shrine::UploadedFile>
|
230
|
-
```
|
231
|
-
|
232
|
-
And with nested derivatives:
|
233
|
-
|
234
|
-
```rb
|
235
|
-
photo.image_derivatives #=> { thumbnail: { small: ..., medium: ..., large: ... } }
|
236
|
-
|
237
|
-
photo.image_derivatives.dig(:thumbnail, :small) #=> #<Shrine::UploadedFile>
|
238
|
-
photo.image_derivatives(:thumbnail, :small) #=> #<Shrine::UploadedFile>
|
239
|
-
photo.image(:thumbnails, :small) #=> #<Shrine::UploadedFile>
|
240
|
-
```
|
241
|
-
|
242
|
-
When using `Shrine::Attacher` directly, you can retrieve derivatives using
|
243
|
-
`Attacher#derivatives`:
|
244
|
-
|
245
316
|
```rb
|
246
317
|
attacher.derivatives #=>
|
247
318
|
# {
|
248
|
-
#
|
249
|
-
#
|
250
|
-
#
|
319
|
+
# thumbnail: {
|
320
|
+
# small: #<Shrine::UploadedFile ...>,
|
321
|
+
# medium: #<Shrine::UploadedFile ...>,
|
322
|
+
# large: #<Shrine::UploadedFile ...>,
|
323
|
+
# },
|
324
|
+
# layers: [
|
325
|
+
# #<Shrine::UploadedFile ...>,
|
326
|
+
# #<Shrine::UploadedFile ...>,
|
327
|
+
# # ...
|
328
|
+
# ]
|
251
329
|
# }
|
252
330
|
```
|
253
331
|
|
254
|
-
## Derivative URL
|
255
|
-
|
256
|
-
If you're using the `Shrine::Attachment` module, you can use the `#<name>_url`
|
257
|
-
method to retrieve the URL of a derivative.
|
258
|
-
|
259
|
-
```rb
|
260
|
-
class Photo < Model(:image_data)
|
261
|
-
include ImageUploader::Attachment(:image)
|
262
|
-
end
|
263
|
-
```
|
264
|
-
```rb
|
265
|
-
photo.image_url(:small) #=> "https://example.com/small.jpg"
|
266
|
-
photo.image_url(:medium) #=> "https://example.com/medium.jpg"
|
267
|
-
photo.image_url(:large) #=> "https://example.com/large.jpg"
|
268
|
-
```
|
269
|
-
|
270
|
-
For nested derivatives you can pass multiple keys:
|
271
|
-
|
272
|
-
```rb
|
273
|
-
photo.image_derivatives #=> { thumbnail: { small: ..., medium: ..., large: ... } }
|
274
|
-
|
275
|
-
photo.image_url(:thumbnail, :medium) #=> "https://example.com/medium.jpg"
|
276
|
-
```
|
277
|
-
|
278
|
-
By default, `#<name>_url` method will return `nil` if derivative is not found.
|
279
|
-
You can use the [`default_url`][default_url] plugin to set up URL fallbacks:
|
280
|
-
|
281
|
-
```rb
|
282
|
-
Attacher.default_url do |derivative: nil, **|
|
283
|
-
"https://my-app.com/fallbacks/#{derivative}.jpg" if derivative
|
284
|
-
end
|
285
|
-
```
|
286
|
-
```rb
|
287
|
-
photo.image_url(:medium) #=> "https://example.com/fallbacks.com/medium.jpg"
|
288
|
-
```
|
289
|
-
|
290
|
-
Any additional URL options passed to `#<name>_url` will be forwarded to the
|
291
|
-
storage:
|
292
|
-
|
293
|
-
```rb
|
294
|
-
photo.image_url(:small, response_content_disposition: "attachment")
|
295
|
-
```
|
296
|
-
|
297
|
-
You can also retrieve the derivative URL via `UploadedFile#url`:
|
298
|
-
|
299
|
-
```rb
|
300
|
-
photo.image_derivatives[:large].url
|
301
|
-
# or
|
302
|
-
attacher.derivatives[:large].url
|
303
|
-
```
|
304
|
-
|
305
332
|
## Processing derivatives
|
306
333
|
|
307
334
|
A derivatives processor block takes the original file, and is expected to
|
308
335
|
return a hash of processed files (it can be [nested](#nesting-derivatives)).
|
309
336
|
|
310
337
|
```rb
|
311
|
-
Attacher.
|
338
|
+
Attacher.derivatives :my_processor do |original|
|
312
339
|
# return a hash of processed files
|
313
340
|
end
|
314
341
|
```
|
@@ -327,9 +354,10 @@ The processor block is evaluated in context of the `Shrine::Attacher` instance,
|
|
327
354
|
which allows you to change your processing logic based on the record data.
|
328
355
|
|
329
356
|
```rb
|
330
|
-
Attacher.
|
357
|
+
Attacher.derivatives :my_processor do |original|
|
331
358
|
self #=> #<Shrine::Attacher>
|
332
359
|
|
360
|
+
file #=> #<Shrine::UploadedFile>
|
333
361
|
record #=> #<Photo>
|
334
362
|
name #=> :image
|
335
363
|
context #=> { ... }
|
@@ -345,7 +373,7 @@ forwarded to the processor:
|
|
345
373
|
attacher.process_derivatives(:my_processor, foo: "bar")
|
346
374
|
```
|
347
375
|
```rb
|
348
|
-
Attacher.
|
376
|
+
Attacher.derivatives :my_processor do |original, **options|
|
349
377
|
options #=> { :foo => "bar" }
|
350
378
|
# ...
|
351
379
|
end
|
@@ -357,7 +385,7 @@ By default, the `Attacher#process_derivatives` method will download the
|
|
357
385
|
attached file and pass it to the processor:
|
358
386
|
|
359
387
|
```rb
|
360
|
-
Attacher.
|
388
|
+
Attacher.derivatives :my_processor do |original|
|
361
389
|
original #=> #<File:...>
|
362
390
|
# ...
|
363
391
|
end
|
@@ -366,12 +394,22 @@ end
|
|
366
394
|
attacher.process_derivatives(:my_processor) # downloads attached file and passes it to the processor
|
367
395
|
```
|
368
396
|
|
369
|
-
If you want to use a different source file,
|
370
|
-
|
371
|
-
|
397
|
+
If you want to use a different source file, you can pass it in to the process
|
398
|
+
call. Typically you'd pass a local file on disk. If you pass a
|
399
|
+
`Shrine::UploadedFile` object, it will be automatically downloaded to disk.
|
400
|
+
|
401
|
+
```rb
|
402
|
+
# named processor:
|
403
|
+
attacher.process_derivatives(:my_processor, source_file)
|
404
|
+
|
405
|
+
# default processor:
|
406
|
+
attacher.process_derivatives(source_file)
|
407
|
+
```
|
408
|
+
|
409
|
+
If you want to call multiple processors in a row with the same source file, you
|
410
|
+
can use this to avoid re-downloading the same source file each time:
|
372
411
|
|
373
412
|
```rb
|
374
|
-
# this way the source file is downloaded only once
|
375
413
|
attacher.file.download do |original|
|
376
414
|
attacher.process_derivatives(:thumbnails, original)
|
377
415
|
attacher.process_derivatives(:colors, original)
|
@@ -384,11 +422,11 @@ If you already have processed files that you want to save, you can do that with
|
|
384
422
|
`Attacher#add_derivatives`:
|
385
423
|
|
386
424
|
```rb
|
387
|
-
attacher.add_derivatives(
|
425
|
+
attacher.add_derivatives({
|
388
426
|
one: file_1,
|
389
427
|
two: file_2,
|
390
428
|
# ...
|
391
|
-
)
|
429
|
+
})
|
392
430
|
|
393
431
|
attacher.derivatives #=>
|
394
432
|
# {
|
@@ -402,7 +440,7 @@ New derivatives will be merged with existing ones:
|
|
402
440
|
|
403
441
|
```rb
|
404
442
|
attacher.derivatives #=> { one: #<Shrine::UploadedFile> }
|
405
|
-
attacher.add_derivatives(two: two_file)
|
443
|
+
attacher.add_derivatives({ two: two_file })
|
406
444
|
attacher.derivatives #=> { one: #<Shrine::UploadedFile>, two: #<Shrine::UploadedFile> }
|
407
445
|
```
|
408
446
|
|
@@ -410,7 +448,7 @@ The merging is deep, so the following will work as well:
|
|
410
448
|
|
411
449
|
```rb
|
412
450
|
attacher.derivatives #=> { nested: { one: #<Shrine::UploadedFile> } }
|
413
|
-
attacher.add_derivatives(nested: { two: two_file })
|
451
|
+
attacher.add_derivatives({ nested: { two: two_file } })
|
414
452
|
attacher.derivatives #=> { nested: { one: #<Shrine::UploadedFile>, two: #<Shrine::UploadedFile> } }
|
415
453
|
```
|
416
454
|
|
@@ -437,11 +475,11 @@ If you want to upload processed files without setting them, you can use
|
|
437
475
|
`Attacher#upload_derivatives`:
|
438
476
|
|
439
477
|
```rb
|
440
|
-
derivatives = attacher.upload_derivatives(
|
478
|
+
derivatives = attacher.upload_derivatives({
|
441
479
|
one: file_1,
|
442
480
|
two: file_2,
|
443
481
|
# ...
|
444
|
-
)
|
482
|
+
})
|
445
483
|
|
446
484
|
derivatives #=>
|
447
485
|
# {
|
@@ -479,9 +517,7 @@ attacher.upload_derivative :thumb, thumbnail_file,
|
|
479
517
|
location: "path/to/derivative"
|
480
518
|
```
|
481
519
|
|
482
|
-
|
483
|
-
name of the derivative, which you can use when extracting metadata, generating
|
484
|
-
location or generating upload options:
|
520
|
+
The `:derivative` name is automatically passed to the uploader:
|
485
521
|
|
486
522
|
```rb
|
487
523
|
class MyUploader < Shrine
|
@@ -510,8 +546,6 @@ If you want to disable this behaviour, pass `delete: false`:
|
|
510
546
|
|
511
547
|
```rb
|
512
548
|
attacher.upload_derivative(:thumb, thumbnail_file, delete: false)
|
513
|
-
|
514
|
-
File.exist?(thumbnail_file.path) #=> true
|
515
549
|
```
|
516
550
|
|
517
551
|
## Merging derivatives
|
@@ -521,7 +555,7 @@ If you want to save already uploaded derivatives, you can use
|
|
521
555
|
|
522
556
|
```rb
|
523
557
|
attacher.derivatives #=> { one: #<Shrine::UploadedFile> }
|
524
|
-
attacher.merge_derivatives attacher.upload_derivatives(two: two_file)
|
558
|
+
attacher.merge_derivatives attacher.upload_derivatives({ two: two_file })
|
525
559
|
attacher.derivatives #=> { one: #<Shrine::UploadedFile>, two: #<Shrine::UploadedFile> }
|
526
560
|
```
|
527
561
|
|
@@ -529,7 +563,7 @@ This does a deep merge, so the following will work as well:
|
|
529
563
|
|
530
564
|
```rb
|
531
565
|
attacher.derivatives #=> { nested: { one: #<Shrine::UploadedFile> } }
|
532
|
-
attacher.merge_derivatives attacher.upload_derivatives(nested: { two: two_file })
|
566
|
+
attacher.merge_derivatives attacher.upload_derivatives({ nested: { two: two_file } })
|
533
567
|
attacher.derivatives #=> { nested: { one: #<Shrine::UploadedFile>, two: #<Shrine::UploadedFile> } }
|
534
568
|
```
|
535
569
|
|
@@ -542,7 +576,7 @@ If instead of adding you want to *override* existing derivatives, you can use
|
|
542
576
|
|
543
577
|
```rb
|
544
578
|
attacher.derivatives #=> { one: #<Shrine::UploadedFile> }
|
545
|
-
attacher.set_derivatives attacher.upload_derivatives(two: two_file)
|
579
|
+
attacher.set_derivatives attacher.upload_derivatives({ two: two_file })
|
546
580
|
attacher.derivatives #=> { two: #<Shrine::UploadedFile> }
|
547
581
|
```
|
548
582
|
|
@@ -650,7 +684,7 @@ You can store derivatives even if there is no main attached file:
|
|
650
684
|
|
651
685
|
```rb
|
652
686
|
attacher.file #=> nil
|
653
|
-
attacher.add_derivatives(one: one_file, two: two_file)
|
687
|
+
attacher.add_derivatives({ one: one_file, two: two_file })
|
654
688
|
attacher.data #=>
|
655
689
|
# {
|
656
690
|
# "derivatives" => {
|
@@ -728,15 +762,17 @@ plugin :derivatives
|
|
728
762
|
Processing derivatives will trigger a `derivatives.shrine` event with the
|
729
763
|
following payload:
|
730
764
|
|
731
|
-
| Key | Description
|
732
|
-
| :-- | :----
|
733
|
-
| `:processor` | Name of the derivatives processor
|
734
|
-
| `:processor_options` | Any options passed to the processor
|
735
|
-
| `:
|
765
|
+
| Key | Description |
|
766
|
+
| :-- | :---- |
|
767
|
+
| `:processor` | Name of the derivatives processor |
|
768
|
+
| `:processor_options` | Any options passed to the processor |
|
769
|
+
| `:io` | The source file passed to the processor |
|
770
|
+
| `:attacher` | The attacher instance doing the processing |
|
771
|
+
| `:uploader` | The uploader class that sent the event |
|
736
772
|
|
737
773
|
A default log subscriber is added as well which logs these events:
|
738
774
|
|
739
|
-
```
|
775
|
+
```
|
740
776
|
Derivatives (2133ms) – {:processor=>:thumbnails, :processor_options=>{}, :uploader=>ImageUploader}
|
741
777
|
```
|
742
778
|
|
@@ -747,8 +783,8 @@ plugin :derivatives, log_subscriber: -> (event) {
|
|
747
783
|
Shrine.logger.info JSON.generate(name: event.name, duration: event.duration, **event.payload)
|
748
784
|
}
|
749
785
|
```
|
750
|
-
```
|
751
|
-
{"name":"derivatives","duration":2133,"processor":"thumbnails","processor_options":{},"uploader":"ImageUploader"}
|
786
|
+
```
|
787
|
+
{"name":"derivatives","duration":2133,"processor":"thumbnails","processor_options":{},"io":"#<File:...>","uploader":"ImageUploader"}
|
752
788
|
```
|
753
789
|
|
754
790
|
Or disable logging altogether:
|