shrine 3.1.0 → 3.4.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +82 -0
- data/README.md +11 -4
- data/doc/advantages.md +4 -4
- data/doc/attacher.md +2 -2
- data/doc/carrierwave.md +24 -12
- data/doc/changing_derivatives.md +1 -1
- data/doc/changing_location.md +6 -5
- data/doc/design.md +134 -85
- data/doc/direct_s3.md +26 -0
- data/doc/external/articles.md +57 -45
- data/doc/external/extensions.md +41 -35
- data/doc/external/misc.md +23 -8
- data/doc/getting_started.md +156 -85
- data/doc/metadata.md +80 -44
- data/doc/multiple_files.md +1 -1
- data/doc/paperclip.md +28 -9
- data/doc/plugins/add_metadata.md +112 -35
- data/doc/plugins/atomic_helpers.md +41 -3
- data/doc/plugins/backgrounding.md +12 -2
- data/doc/plugins/column.md +36 -7
- data/doc/plugins/default_url.md +6 -3
- data/doc/plugins/derivatives.md +83 -44
- data/doc/plugins/download_endpoint.md +5 -5
- data/doc/plugins/dynamic_storage.md +1 -1
- data/doc/plugins/entity.md +12 -4
- data/doc/plugins/form_assign.md +5 -5
- data/doc/plugins/included.md +25 -5
- data/doc/plugins/infer_extension.md +9 -0
- data/doc/plugins/instrumentation.md +1 -1
- data/doc/plugins/metadata_attributes.md +1 -0
- data/doc/plugins/mirroring.md +1 -1
- data/doc/plugins/model.md +8 -3
- data/doc/plugins/persistence.md +10 -1
- data/doc/plugins/remote_url.md +6 -1
- data/doc/plugins/remove_invalid.md +9 -1
- data/doc/plugins/sequel.md +1 -1
- data/doc/plugins/store_dimensions.md +10 -0
- data/doc/plugins/type_predicates.md +96 -0
- data/doc/plugins/upload_endpoint.md +1 -1
- data/doc/plugins/upload_options.md +1 -1
- data/doc/plugins/url_options.md +4 -4
- data/doc/plugins/validation.md +14 -4
- data/doc/plugins/versions.md +7 -7
- data/doc/processing.md +287 -123
- data/doc/refile.md +9 -9
- data/doc/release_notes/2.8.0.md +1 -1
- data/doc/release_notes/3.0.0.md +1 -1
- 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/securing_uploads.md +2 -2
- data/doc/storage/memory.md +19 -0
- data/doc/storage/s3.md +104 -77
- data/doc/testing.md +12 -2
- data/doc/upgrading_to_3.md +99 -53
- data/lib/shrine.rb +9 -8
- data/lib/shrine/attacher.rb +20 -10
- data/lib/shrine/attachment.rb +2 -2
- data/lib/shrine/plugins.rb +22 -0
- data/lib/shrine/plugins/activerecord.rb +3 -3
- data/lib/shrine/plugins/add_metadata.rb +20 -5
- data/lib/shrine/plugins/backgrounding.rb +2 -2
- data/lib/shrine/plugins/default_url.rb +1 -1
- data/lib/shrine/plugins/derivation_endpoint.rb +13 -8
- data/lib/shrine/plugins/derivatives.rb +59 -30
- data/lib/shrine/plugins/determine_mime_type.rb +5 -3
- data/lib/shrine/plugins/entity.rb +12 -11
- data/lib/shrine/plugins/instrumentation.rb +12 -18
- data/lib/shrine/plugins/mirroring.rb +8 -8
- data/lib/shrine/plugins/model.rb +3 -3
- data/lib/shrine/plugins/presign_endpoint.rb +16 -4
- 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_attachment.rb +5 -0
- data/lib/shrine/plugins/remove_invalid.rb +10 -5
- data/lib/shrine/plugins/sequel.rb +1 -1
- data/lib/shrine/plugins/store_dimensions.rb +4 -2
- data/lib/shrine/plugins/type_predicates.rb +113 -0
- data/lib/shrine/plugins/upload_endpoint.rb +10 -5
- 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/memory.rb +5 -3
- data/lib/shrine/storage/s3.rb +117 -38
- data/lib/shrine/version.rb +1 -1
- data/shrine.gemspec +8 -8
- metadata +42 -34
@@ -13,7 +13,7 @@ Keys are names of the registered storages, and values are either hashes or
|
|
13
13
|
blocks.
|
14
14
|
|
15
15
|
```rb
|
16
|
-
plugin :upload_options, store: -> (io,
|
16
|
+
plugin :upload_options, store: -> (io, options) do
|
17
17
|
if options[:derivative]
|
18
18
|
{ acl: "public-read" }
|
19
19
|
else
|
data/doc/plugins/url_options.md
CHANGED
@@ -4,10 +4,10 @@ title: URL Options
|
|
4
4
|
|
5
5
|
The [`url_options`][url_options] plugin allows you to specify
|
6
6
|
URL options that will be applied by default for uploaded files of specified
|
7
|
-
storages.
|
7
|
+
storages. `url_options` are parameters specific to the storage service.
|
8
8
|
|
9
9
|
```rb
|
10
|
-
plugin :url_options, store: { expires_in: 24*60*60 }
|
10
|
+
plugin :url_options, store: { expires_in: 24*60*60 } # `expires_in` is a URL option for AWS S3
|
11
11
|
```
|
12
12
|
|
13
13
|
You can also generate the default URL options dynamically by using a block,
|
@@ -15,8 +15,8 @@ which will receive the UploadedFile object along with any options that were
|
|
15
15
|
passed to `UploadedFile#url`.
|
16
16
|
|
17
17
|
```rb
|
18
|
-
plugin :url_options, store: -> (
|
19
|
-
{ response_content_disposition: ContentDisposition.attachment(
|
18
|
+
plugin :url_options, store: -> (file, options) do
|
19
|
+
{ response_content_disposition: ContentDisposition.attachment(file.original_filename) }
|
20
20
|
end
|
21
21
|
```
|
22
22
|
|
data/doc/plugins/validation.md
CHANGED
@@ -34,11 +34,12 @@ The validation block is executed in context of a `Shrine::Attacher` instance:
|
|
34
34
|
```rb
|
35
35
|
class VideoUploader < Shrine
|
36
36
|
Attacher.validate do
|
37
|
-
self
|
37
|
+
self #=> #<Shrine::Attacher>
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
|
39
|
+
file #=> #<Shrine::UploadedFile>
|
40
|
+
record #=> #<Movie>
|
41
|
+
name #=> :video
|
42
|
+
context #=> { ... }
|
42
43
|
end
|
43
44
|
end
|
44
45
|
```
|
@@ -83,5 +84,14 @@ You can also skip validation by passing `validate: false`:
|
|
83
84
|
attacher.assign(file, validate: false) # skips validation
|
84
85
|
```
|
85
86
|
|
87
|
+
## Manual validation
|
88
|
+
|
89
|
+
You can also run validation manually via `Attacher#validate`:
|
90
|
+
|
91
|
+
```rb
|
92
|
+
attacher.set(uploaded_file) # doesn't trigger validation
|
93
|
+
attacher.validate # runs validation
|
94
|
+
```
|
95
|
+
|
86
96
|
[validation]: https://github.com/shrinerb/shrine/blob/master/lib/shrine/plugins/validation.rb
|
87
97
|
[validation_helpers]: https://shrinerb.com/docs/plugins/validation_helpers
|
data/doc/plugins/versions.md
CHANGED
@@ -17,7 +17,7 @@ require "image_processing/mini_magick"
|
|
17
17
|
|
18
18
|
plugin :processing
|
19
19
|
|
20
|
-
process(:store) do |io,
|
20
|
+
process(:store) do |io, **options|
|
21
21
|
versions = { original: io } # retain original
|
22
22
|
|
23
23
|
io.download do |original|
|
@@ -120,7 +120,7 @@ In addition to Hashes, the plugin also supports Arrays of files. For example,
|
|
120
120
|
you might want to split a PDf into pages:
|
121
121
|
|
122
122
|
```rb
|
123
|
-
process(:store) do |io,
|
123
|
+
process(:store) do |io, **options|
|
124
124
|
versions = { pages: [] }
|
125
125
|
|
126
126
|
io.download do |pdf|
|
@@ -146,7 +146,7 @@ which you can do by adding the yielded `Shrine::UploadedFile` object as one of
|
|
146
146
|
the versions, by convention named `:original`:
|
147
147
|
|
148
148
|
```rb
|
149
|
-
process(:store) do |io,
|
149
|
+
process(:store) do |io, **options|
|
150
150
|
# processing thumbnail
|
151
151
|
{ original: io, thumbnail: thumbnail }
|
152
152
|
end
|
@@ -163,12 +163,12 @@ The version name will be available via `:version` when generating location or a
|
|
163
163
|
default URL.
|
164
164
|
|
165
165
|
```rb
|
166
|
-
def generate_location(io,
|
167
|
-
"uploads/#{
|
166
|
+
def generate_location(io, version: nil, **)
|
167
|
+
"uploads/#{version}-#{super}"
|
168
168
|
end
|
169
169
|
|
170
|
-
Attacher.default_url do |
|
171
|
-
"/images/defaults/#{
|
170
|
+
Attacher.default_url do |version: nil, **|
|
171
|
+
"/images/defaults/#{version}.jpg"
|
172
172
|
end
|
173
173
|
```
|
174
174
|
|
data/doc/processing.md
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
title: File Processing
|
3
3
|
---
|
4
4
|
|
5
|
-
Shrine allows you to process attached files
|
5
|
+
Shrine allows you to process attached files eagerly or on-the-fly. For
|
6
6
|
example, if your app is accepting image uploads, you can generate a predefined
|
7
7
|
set of of thumbnails when the image is attached to a record, or you can have
|
8
8
|
thumbnails generated dynamically as they're needed.
|
9
9
|
|
10
10
|
How you're going to implement processing is entirely up to you. For images it's
|
11
11
|
recommended to use the **[ImageProcessing]** gem, which provides wrappers for
|
12
|
-
processing with [
|
13
|
-
[libvips]
|
14
|
-
|
12
|
+
processing with [MiniMagick][ImageProcessing::MiniMagick] and
|
13
|
+
[libvips][ImageProcessing::Vips]. Here is an example of generating a thumbnail
|
14
|
+
with ImageProcessing:
|
15
15
|
|
16
16
|
```
|
17
17
|
$ brew install imagemagick
|
@@ -24,17 +24,21 @@ gem "image_processing", "~> 1.8"
|
|
24
24
|
require "image_processing/mini_magick"
|
25
25
|
|
26
26
|
thumbnail = ImageProcessing::MiniMagick
|
27
|
-
.source(image)
|
28
|
-
.resize_to_limit
|
27
|
+
.source(image) # input file
|
28
|
+
.resize_to_limit(600, 400) # resize macro
|
29
|
+
.colorspace("grayscale") # custom operation
|
30
|
+
.convert("jpeg") # output type
|
31
|
+
.saver(quality: 90) # output options
|
32
|
+
.call # run the pipeline
|
29
33
|
|
30
34
|
thumbnail #=> #<Tempfile:...> (a 600x400 thumbnail of the source image)
|
31
35
|
```
|
32
36
|
|
33
|
-
##
|
37
|
+
## Eager processing
|
34
38
|
|
35
39
|
Let's say we're handling images, and want to generate a predefined set of
|
36
|
-
thumbnails with various dimensions. We can use the
|
37
|
-
upload and save the processed files:
|
40
|
+
thumbnails with various dimensions. We can use the
|
41
|
+
**[`derivatives`][derivatives]** plugin to upload and save the processed files:
|
38
42
|
|
39
43
|
```rb
|
40
44
|
Shrine.plugin :derivatives
|
@@ -56,8 +60,11 @@ end
|
|
56
60
|
```
|
57
61
|
```rb
|
58
62
|
photo = Photo.new(image: file)
|
59
|
-
|
60
|
-
photo.
|
63
|
+
|
64
|
+
if photo.valid?
|
65
|
+
photo.image_derivatives! if photo.image_changed? # creates derivatives
|
66
|
+
photo.save
|
67
|
+
end
|
61
68
|
```
|
62
69
|
|
63
70
|
After the processed files are uploaded, their data is saved into the
|
@@ -71,27 +78,54 @@ photo.image(:large).size #=> 5825949
|
|
71
78
|
photo.image(:large).mime_type #=> "image/jpeg"
|
72
79
|
```
|
73
80
|
|
74
|
-
###
|
81
|
+
### Conditional derivatives
|
75
82
|
|
76
|
-
|
77
|
-
|
78
|
-
promotion:
|
83
|
+
The `Attacher.derivatives` block is evaluated in context of a
|
84
|
+
`Shrine::Attacher` instance:
|
79
85
|
|
80
86
|
```rb
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
87
|
+
Attacher.derivatives do |original|
|
88
|
+
self #=> #<Shrine::Attacher>
|
89
|
+
|
90
|
+
file #=> #<Shrine::UploadedFile>
|
91
|
+
record #=> #<Photo>
|
92
|
+
name #=> :image
|
93
|
+
context #=> { ... }
|
94
|
+
|
95
|
+
# ...
|
96
|
+
end
|
97
|
+
```
|
98
|
+
|
99
|
+
This gives you the ability to branch the processing logic based on the
|
100
|
+
attachment information:
|
101
|
+
|
102
|
+
```rb
|
103
|
+
Attacher.derivatives do |original|
|
104
|
+
magick = ImageProcessing::MiniMagick.source(original)
|
105
|
+
result = {}
|
106
|
+
|
107
|
+
if record.is_a?(Photo)
|
108
|
+
result[:jpg] = magick.convert!("jpeg")
|
109
|
+
result[:gray] = magick.colorspace!("grayscale")
|
110
|
+
end
|
111
|
+
|
112
|
+
if file.mime_type == "image/svg+xml"
|
113
|
+
result[:png] = magick.loader(transparent: "white").convert!("png")
|
85
114
|
end
|
115
|
+
|
116
|
+
result
|
86
117
|
end
|
87
118
|
```
|
88
119
|
|
120
|
+
The [`type_predicates`][type_predicates] plugin provides convenient predicate
|
121
|
+
methods for branching based on the file type.
|
122
|
+
|
89
123
|
### Backgrounding
|
90
124
|
|
91
125
|
Since file processing can be time consuming, it's recommended to move it into a
|
92
126
|
background job.
|
93
127
|
|
94
|
-
####
|
128
|
+
#### A) Creating derivatives with promotion
|
95
129
|
|
96
130
|
The simplest way is to use the [`backgrounding`][backgrounding] plugin to move
|
97
131
|
promotion into a background job, and then create derivatives as part of
|
@@ -120,7 +154,7 @@ class PromoteJob
|
|
120
154
|
end
|
121
155
|
```
|
122
156
|
|
123
|
-
####
|
157
|
+
#### B) Creating derivatives separately from promotion
|
124
158
|
|
125
159
|
Derivatives don't need to be created as part of the attachment flow, you can
|
126
160
|
create them at any point after promotion:
|
@@ -151,7 +185,7 @@ class DerivativesJob
|
|
151
185
|
end
|
152
186
|
```
|
153
187
|
|
154
|
-
####
|
188
|
+
#### C) Creating derivatives concurrently
|
155
189
|
|
156
190
|
You can also generate derivatives concurrently:
|
157
191
|
|
@@ -204,67 +238,98 @@ class DerivativeJob
|
|
204
238
|
end
|
205
239
|
```
|
206
240
|
|
207
|
-
###
|
241
|
+
### URL fallbacks
|
208
242
|
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
Shrine integration, the ImageProcessing gem that we saw earlier is a completely
|
213
|
-
generic gem.
|
243
|
+
If you're creating derivatives in a background job, you'll likely want to use
|
244
|
+
some fallbacks for derivative URLs while the background job is still
|
245
|
+
processing. You can do that with the [`default_url`][default_url] plugin.
|
214
246
|
|
215
|
-
|
216
|
-
|
247
|
+
```rb
|
248
|
+
Shrine.plugin :default_url
|
249
|
+
```
|
250
|
+
|
251
|
+
#### A) Fallback to original
|
252
|
+
|
253
|
+
You can fall back to the original file URL when the derivative is missing:
|
217
254
|
|
218
255
|
```rb
|
219
|
-
|
220
|
-
|
256
|
+
Attacher.default_url do |derivative: nil, **|
|
257
|
+
file&.url if derivative
|
258
|
+
end
|
221
259
|
```
|
222
260
|
```rb
|
223
|
-
|
261
|
+
photo.image_url(:large) #=> "https://example.com/path/to/original.jpg"
|
262
|
+
# ... background job finishes ...
|
263
|
+
photo.image_url(:large) #=> "https://example.com/path/to/large.jpg"
|
264
|
+
```
|
224
265
|
|
225
|
-
|
226
|
-
Attacher.derivatives do |original|
|
227
|
-
transcoded = Tempfile.new ["transcoded", ".mp4"]
|
228
|
-
screenshot = Tempfile.new ["screenshot", ".jpg"]
|
266
|
+
#### B) Fallback to derivative
|
229
267
|
|
230
|
-
|
231
|
-
movie.transcode(transcoded.path)
|
232
|
-
movie.screenshot(screenshot.path)
|
268
|
+
You can fall back to another derivative URL when the derivative is missing:
|
233
269
|
|
234
|
-
|
235
|
-
|
270
|
+
```rb
|
271
|
+
Attacher.default_url do |derivative: nil, **|
|
272
|
+
derivatives[:optimized]&.url if derivative
|
236
273
|
end
|
237
274
|
```
|
275
|
+
```rb
|
276
|
+
photo.image_url(:large) #=> "https://example.com/path/to/optimized.jpg"
|
277
|
+
# ... background job finishes ...
|
278
|
+
photo.image_url(:large) #=> "https://example.com/path/to/large.jpg"
|
279
|
+
```
|
280
|
+
|
281
|
+
#### C) Fallback to on-the-fly
|
282
|
+
|
283
|
+
You can also fall back to [on-the-fly processing](#on-the-fly-processing),
|
284
|
+
which should generally provide the best user experience.
|
285
|
+
|
286
|
+
```rb
|
287
|
+
THUMBNAILS = {
|
288
|
+
small: [300, 300],
|
289
|
+
medium: [500, 500],
|
290
|
+
large: [800, 800],
|
291
|
+
}
|
292
|
+
|
293
|
+
Attacher.default_url do |derivative: nil, **|
|
294
|
+
file&.derivation_url(:thumbnail, *THUMBNAILS.fetch(derivative)) if derivative
|
295
|
+
end
|
296
|
+
```
|
297
|
+
```rb
|
298
|
+
photo.image_url(:large) #=> "../derivations/thumbnail/800/800/..."
|
299
|
+
# ... background job finishes ...
|
300
|
+
photo.image_url(:large) #=> "https://example.com/path/to/large.jpg"
|
301
|
+
```
|
238
302
|
|
239
303
|
## On-the-fly processing
|
240
304
|
|
241
|
-
|
305
|
+
Having eagerly created image thumbnails can be a pain to maintain, because
|
242
306
|
whenever you need to add a new version or change an existing one, you need to
|
243
|
-
retroactively apply it to all existing
|
244
|
-
guide for more details).
|
307
|
+
retroactively apply it to all existing attachments (see the [Managing
|
308
|
+
Derivatives] guide for more details).
|
245
309
|
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
processing such as creating image thumbnails or document
|
310
|
+
Sometimes it makes more sense to generate thumbnails dynamically as they're
|
311
|
+
requested, and then cache them for future requests. This strategy is known as
|
312
|
+
processing "**on-the-fly**" or "**on-demand**", and it's suitable for
|
313
|
+
short-running processing such as creating image thumbnails or document
|
314
|
+
previews.
|
250
315
|
|
251
316
|
Shrine provides on-the-fly processing functionality via the
|
252
|
-
[`derivation_endpoint`][derivation_endpoint] plugin.
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
3. define a processing block for the type files you want to generate
|
258
|
-
|
259
|
-
Together it might look something like this:
|
317
|
+
**[`derivation_endpoint`][derivation_endpoint]** plugin. You set it up by
|
318
|
+
loading the plugin with a secret key (you generate this yourself, maybe via
|
319
|
+
something like `SecureRandom.hex`) and a path prefix, mount its Rack app in
|
320
|
+
your routes on the configured path prefix, and define processing you want to
|
321
|
+
perform:
|
260
322
|
|
323
|
+
```rb
|
324
|
+
# config/initializers/shrine.rb (Rails)
|
325
|
+
# ...
|
326
|
+
Shrine.plugin :derivation_endpoint, secret_key: "<SHRINE_SECRET_KEY>"
|
327
|
+
```
|
261
328
|
```rb
|
262
329
|
require "image_processing/mini_magick"
|
263
330
|
|
264
331
|
class ImageUploader < Shrine
|
265
|
-
plugin :derivation_endpoint,
|
266
|
-
secret_key: "<YOUR SECRET KEY>",
|
267
|
-
prefix: "derivations/image"
|
332
|
+
plugin :derivation_endpoint, prefix: "derivations/image" # matches mount point
|
268
333
|
|
269
334
|
derivation :thumbnail do |file, width, height|
|
270
335
|
ImageProcessing::MiniMagick
|
@@ -273,11 +338,11 @@ class ImageUploader < Shrine
|
|
273
338
|
end
|
274
339
|
end
|
275
340
|
```
|
276
|
-
|
277
341
|
```rb
|
278
342
|
# config/routes.rb (Rails)
|
279
343
|
Rails.application.routes.draw do
|
280
|
-
|
344
|
+
# ...
|
345
|
+
mount ImageUploader.derivation_endpoint => "/derivations/image"
|
281
346
|
end
|
282
347
|
```
|
283
348
|
|
@@ -293,8 +358,133 @@ The plugin is highly customizable, be sure to check out the
|
|
293
358
|
[documentation][derivation_endpoint], especially the [performance
|
294
359
|
section][derivation_endpoint performance].
|
295
360
|
|
361
|
+
### Dynamic derivation
|
362
|
+
|
363
|
+
If you have multiple types of transformations and don't want to have a
|
364
|
+
derivation for each one, you can set up a single derivation that applies any
|
365
|
+
series of transformations:
|
366
|
+
|
367
|
+
```rb
|
368
|
+
class ImageUploader < Shrine
|
369
|
+
derivation :transform do |original, transformations|
|
370
|
+
transformations = Shrine.urlsafe_deserialize(transformations)
|
371
|
+
|
372
|
+
vips = ImageProcessing::Vips.source(original)
|
373
|
+
vips.apply!(transformations)
|
374
|
+
end
|
375
|
+
end
|
376
|
+
```
|
377
|
+
```rb
|
378
|
+
photo.image.derivation_url :transform, Shrine.urlsafe_serialize(
|
379
|
+
crop: [10, 10, 500, 500],
|
380
|
+
resize_to_fit: [300, 300],
|
381
|
+
gaussblur: 1,
|
382
|
+
)
|
383
|
+
```
|
384
|
+
|
385
|
+
You can create a helper method for convenience:
|
386
|
+
|
387
|
+
```rb
|
388
|
+
def derivation_url(file, transformations)
|
389
|
+
file.derivation_url(:transform, Shrine.urlsafe_serialize(transformations))
|
390
|
+
end
|
391
|
+
```
|
392
|
+
```rb
|
393
|
+
derivation_url photo.image,
|
394
|
+
crop: [10, 10, 500, 500],
|
395
|
+
resize_to_fit: [300, 300],
|
396
|
+
gaussblur: 1
|
397
|
+
```
|
398
|
+
|
399
|
+
## Processing other filetypes
|
400
|
+
|
401
|
+
So far we've only been talking about processing images. However, there is
|
402
|
+
nothing image-specific in Shrine's processing API, you can just as well process
|
403
|
+
any other types of files. The processing tool doesn't need to have any special
|
404
|
+
Shrine integration, the ImageProcessing gem that we saw earlier is a completely
|
405
|
+
generic gem.
|
406
|
+
|
407
|
+
To demonstrate, here is an example of transcoding videos using
|
408
|
+
[streamio-ffmpeg]:
|
409
|
+
|
410
|
+
```rb
|
411
|
+
# Gemfile
|
412
|
+
gem "streamio-ffmpeg"
|
413
|
+
```
|
414
|
+
```rb
|
415
|
+
require "streamio-ffmpeg"
|
416
|
+
|
417
|
+
class VideoUploader < Shrine
|
418
|
+
Attacher.derivatives do |original|
|
419
|
+
transcoded = Tempfile.new ["transcoded", ".mp4"]
|
420
|
+
screenshot = Tempfile.new ["screenshot", ".jpg"]
|
421
|
+
|
422
|
+
movie = FFMPEG::Movie.new(original.path)
|
423
|
+
movie.transcode(transcoded.path)
|
424
|
+
movie.screenshot(screenshot.path)
|
425
|
+
|
426
|
+
{ transcoded: transcoded, screenshot: screenshot }
|
427
|
+
end
|
428
|
+
end
|
429
|
+
```
|
430
|
+
|
431
|
+
### Polymorphic uploader
|
432
|
+
|
433
|
+
Sometimes you might want an attachment attribute to accept multiple types of
|
434
|
+
files, and apply different processing depending on the type. Since Shrine's
|
435
|
+
processing blocks are evaluated dynamically, you can use conditional logic:
|
436
|
+
|
437
|
+
```rb
|
438
|
+
class PolymorphicUploader < Shrine
|
439
|
+
IMAGE_TYPES = %w[image/jpeg image/png image/webp]
|
440
|
+
VIDEO_TYPES = %w[video/mp4 video/quicktime]
|
441
|
+
PDF_TYPES = %w[application/pdf]
|
442
|
+
|
443
|
+
Attacher.validate do
|
444
|
+
validate_mime_type IMAGE_TYPES + VIDEO_TYPES + PDF_TYPES
|
445
|
+
# ...
|
446
|
+
end
|
447
|
+
|
448
|
+
Attacher.derivatives do |original|
|
449
|
+
case file.mime_type
|
450
|
+
when *IMAGE_TYPES then process_derivatives(:image, original)
|
451
|
+
when *VIDEO_TYPES then process_derivatives(:video, original)
|
452
|
+
when *PDF_TYPES then process_derivatives(:pdf, original)
|
453
|
+
end
|
454
|
+
end
|
455
|
+
|
456
|
+
Attacher.derivatives :image do |original|
|
457
|
+
# ...
|
458
|
+
end
|
459
|
+
|
460
|
+
Attacher.derivatives :video do |original|
|
461
|
+
# ...
|
462
|
+
end
|
463
|
+
|
464
|
+
Attacher.derivatives :pdf do |original|
|
465
|
+
# ...
|
466
|
+
end
|
467
|
+
end
|
468
|
+
```
|
469
|
+
|
296
470
|
## Extras
|
297
471
|
|
472
|
+
### Automatic derivatives
|
473
|
+
|
474
|
+
If you would like derivatives to be automatically created with promotion, you
|
475
|
+
can use the `create_on_promote` option built-in to the derivatives plugin.
|
476
|
+
|
477
|
+
```rb
|
478
|
+
class Shrine::Attacher
|
479
|
+
plugin :derivatives, create_on_promote: true
|
480
|
+
end
|
481
|
+
```
|
482
|
+
|
483
|
+
This shouldn't be needed if you're processing in the
|
484
|
+
[background](#backgrounding), as in that case you have a background worker that
|
485
|
+
will be called for each attachment, so you can call
|
486
|
+
`Attacher#create_derivatives` there.
|
487
|
+
|
298
488
|
### libvips
|
299
489
|
|
300
490
|
As mentioned, ImageProcessing gem also has an alternative backend for
|
@@ -304,7 +494,7 @@ characteristics – it's often **multiple times faster** than ImageMagick and ha
|
|
304
494
|
low memory usage (see [Why is libvips quick]).
|
305
495
|
|
306
496
|
Using libvips is as easy as installing it and switching to the
|
307
|
-
`ImageProcessing::Vips` backend:
|
497
|
+
[`ImageProcessing::Vips`][ImageProcessing::Vips] backend:
|
308
498
|
|
309
499
|
```
|
310
500
|
$ brew install vips
|
@@ -326,7 +516,7 @@ thumbnail = ImageProcessing::Vips
|
|
326
516
|
thumbnail #=> #<Tempfile:...> (a 600x400 thumbnail of the source image)
|
327
517
|
```
|
328
518
|
|
329
|
-
### Parallelize
|
519
|
+
### Parallelize uploads
|
330
520
|
|
331
521
|
If you're generating derivatives, you can parallelize the uploads using the
|
332
522
|
[concurrent-ruby] gem:
|
@@ -351,86 +541,60 @@ Concurrent::Promises.zip(*tasks).wait!
|
|
351
541
|
|
352
542
|
### External processing
|
353
543
|
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
544
|
+
You can also integrate Shrine with 3rd-party processing services such as
|
545
|
+
[Cloudinary] and [Imgix]. In the most common case, you'd serve images directly
|
546
|
+
from these services, see the corresponding plugin docs for more details
|
547
|
+
([shrine-cloudinary], [shrine-imgix] and [others][external storages])
|
548
|
+
|
549
|
+
You can also choose to use these services as an implementation detail of your
|
550
|
+
application, by downloading the processed images and saving them to your
|
551
|
+
storage. Here is how you might store files processed by Imgix as derivatives:
|
358
552
|
|
359
553
|
```rb
|
360
554
|
# Gemfile
|
361
555
|
gem "down", "~> 5.0"
|
362
556
|
gem "http", "~> 4.0"
|
557
|
+
gem "shrine-imgix", "~> 0.5"
|
558
|
+
```
|
559
|
+
```rb
|
560
|
+
Shrine.plugin :derivatives
|
561
|
+
Shrine.plugin :imgix, client: { host: "my-app.imgix.net", secure_url_token: "secret" }
|
363
562
|
```
|
364
|
-
|
365
563
|
```rb
|
366
564
|
require "down/http"
|
367
565
|
|
368
566
|
class ImageUploader < Shrine
|
369
|
-
|
370
|
-
|
371
|
-
prefix: "derivations/image",
|
372
|
-
download: false
|
373
|
-
|
374
|
-
derivation :thumbnail do |width, height|
|
375
|
-
# generate thumbnails using ImageOptim.com
|
376
|
-
down = Down::Http.new(method: :post)
|
377
|
-
down.download("https://im2.io/<USERNAME>/#{width}x#{height}/#{source.url}")
|
567
|
+
IMGIX_THUMBNAIL = -> (file, width, height) do
|
568
|
+
Down::Http.download(file.imgix_url(w: width, h: height))
|
378
569
|
end
|
379
|
-
end
|
380
|
-
```
|
381
|
-
|
382
|
-
### Cloudinary
|
383
570
|
|
384
|
-
|
385
|
-
|
386
|
-
[
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
```
|
393
|
-
|
394
|
-
```rb
|
395
|
-
require "cloudinary"
|
396
|
-
require "shrine/storage/cloudinary"
|
397
|
-
|
398
|
-
Cloudinary.config(
|
399
|
-
cloud_name: "<YOUR_CLOUD_NAME>",
|
400
|
-
api_key: "<YOUR_API_KEY>",
|
401
|
-
api_secret: "<YOUR_API_SECRET>",
|
402
|
-
)
|
403
|
-
|
404
|
-
Shrine.storages = {
|
405
|
-
cache: Shrine::Storage::Cloudinary.new(prefix: "cache"),
|
406
|
-
store: Shrine::Storage::Cloudinary.new,
|
407
|
-
}
|
408
|
-
```
|
409
|
-
|
410
|
-
Now when we upload our images to Cloudinary, we can generate URLs with various
|
411
|
-
processing parameters:
|
412
|
-
|
413
|
-
```rb
|
414
|
-
photo.image_url(width: 100, height: 100, crop: :fit)
|
415
|
-
#=> "http://res.cloudinary.com/myapp/image/upload/w_100,h_100,c_fit/nature.jpg"
|
571
|
+
Attacher.derivatives do
|
572
|
+
{
|
573
|
+
large: IMGIX_THUMBNAIL[file, 800, 800],
|
574
|
+
medium: IMGIX_THUMBNAIL[file, 500, 500],
|
575
|
+
small: IMGIX_THUMBNAIL[file, 300, 300],
|
576
|
+
}
|
577
|
+
end
|
578
|
+
end
|
416
579
|
```
|
417
580
|
|
418
581
|
[`Shrine::UploadedFile`]: http://shrinerb.com/rdoc/classes/Shrine/UploadedFile/InstanceMethods.html
|
419
582
|
[ImageProcessing]: https://github.com/janko/image_processing
|
420
|
-
[
|
421
|
-
[
|
422
|
-
[libvips]: http://libvips.github.io/libvips/
|
423
|
-
[Why is libvips quick]: https://github.com/libvips/libvips/wiki/Why-is-libvips-quick
|
424
|
-
[ImageOptim.com]: https://imageoptim.com/api
|
583
|
+
[ImageProcessing::MiniMagick]: https://github.com/janko/image_processing/blob/master/doc/minimagick.md#readme
|
584
|
+
[ImageProcessing::Vips]: https://github.com/janko/image_processing/blob/master/doc/vips.md#readme
|
425
585
|
[streamio-ffmpeg]: https://github.com/streamio/streamio-ffmpeg
|
426
586
|
[Managing Derivatives]: https://shrinerb.com/docs/changing-derivatives
|
427
|
-
[Cloudinary]: https://cloudinary.com
|
587
|
+
[Cloudinary]: https://cloudinary.com/
|
588
|
+
[Imgix]: https://www.imgix.com/
|
428
589
|
[shrine-cloudinary]: https://github.com/shrinerb/shrine-cloudinary
|
590
|
+
[shrine-imgix]: https://github.com/shrinerb/shrine-imgix
|
429
591
|
[backgrounding]: https://shrinerb.com/docs/plugins/backgrounding
|
430
|
-
[ruby-vips]: https://github.com/libvips/ruby-vips
|
431
|
-
[MiniMagick]: https://github.com/minimagick/minimagick
|
432
592
|
[derivation_endpoint]: https://shrinerb.com/docs/plugins/derivation_endpoint
|
433
593
|
[derivation_endpoint performance]: https://shrinerb.com/docs/plugins/derivation_endpoint#performance
|
434
594
|
[derivatives]: https://shrinerb.com/docs/plugins/derivatives
|
435
595
|
[concurrent-ruby]: https://github.com/ruby-concurrency/concurrent-ruby
|
436
|
-
[
|
596
|
+
[default_url]: https://shrinerb.com/docs/plugins/default_url
|
597
|
+
[external storages]: https://shrinerb.com/docs/external/extensions#storages
|
598
|
+
[libvips]: https://libvips.github.io/libvips/
|
599
|
+
[Why is libvips quick]: https://github.com/libvips/libvips/wiki/Why-is-libvips-quick
|
600
|
+
[type_predicates]: https://shrinerb.com/docs/plugins/type_predicates
|