shrine 3.0.1 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of shrine might be problematic. Click here for more details.

Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +16 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +7 -2
  5. data/doc/advantages.md +29 -12
  6. data/doc/carrierwave.md +54 -22
  7. data/doc/changing_derivatives.md +39 -39
  8. data/doc/getting_started.md +63 -58
  9. data/doc/multiple_files.md +5 -3
  10. data/doc/paperclip.md +92 -33
  11. data/doc/plugins/activerecord.md +1 -1
  12. data/doc/plugins/data_uri.md +2 -2
  13. data/doc/plugins/derivation_endpoint.md +26 -28
  14. data/doc/plugins/derivatives.md +170 -142
  15. data/doc/plugins/determine_mime_type.md +2 -2
  16. data/doc/plugins/infer_extension.md +2 -2
  17. data/doc/plugins/instrumentation.md +1 -1
  18. data/doc/plugins/metadata_attributes.md +21 -10
  19. data/doc/plugins/persistence.md +1 -0
  20. data/doc/plugins/refresh_metadata.md +5 -4
  21. data/doc/plugins/remote_url.md +2 -2
  22. data/doc/plugins/signature.md +11 -2
  23. data/doc/plugins/store_dimensions.md +2 -2
  24. data/doc/plugins/upload_endpoint.md +7 -11
  25. data/doc/plugins/validation_helpers.md +3 -3
  26. data/doc/processing.md +5 -5
  27. data/doc/refile.md +30 -9
  28. data/doc/release_notes/2.19.0.md +1 -1
  29. data/doc/release_notes/3.0.1.md +4 -0
  30. data/doc/release_notes/3.1.0.md +73 -0
  31. data/doc/securing_uploads.md +1 -1
  32. data/doc/storage/file_system.md +1 -1
  33. data/doc/storage/s3.md +1 -5
  34. data/doc/upgrading_to_3.md +4 -2
  35. data/doc/validation.md +3 -2
  36. data/lib/shrine.rb +1 -2
  37. data/lib/shrine/attacher.rb +4 -4
  38. data/lib/shrine/attachment.rb +3 -3
  39. data/lib/shrine/plugins/add_metadata.rb +1 -5
  40. data/lib/shrine/plugins/default_storage.rb +6 -6
  41. data/lib/shrine/plugins/derivatives.rb +4 -3
  42. data/lib/shrine/plugins/signature.rb +7 -6
  43. data/lib/shrine/plugins/store_dimensions.rb +18 -9
  44. data/lib/shrine/uploaded_file.rb +0 -1
  45. data/lib/shrine/version.rb +2 -2
  46. metadata +3 -2
@@ -151,7 +151,7 @@ processing:
151
151
  ```rb
152
152
  class ImageUploader < Shrine
153
153
  # ...
154
- Attacher.derivatives_processor do |original|
154
+ Attacher.derivatives do |original|
155
155
  width, height = Shrine.dimensions(original)
156
156
 
157
157
  fail ImageBombError if width > 5000 || height > 5000
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  id: file-system
3
- title: Shrine::Storage::FileSystem
3
+ title: File System
4
4
  ---
5
5
 
6
6
  The FileSystem storage handles uploads to the filesystem, and it is most
@@ -1,5 +1,5 @@
1
1
  ---
2
- title: Shrine::Storage::S3
2
+ title: AWS S3
3
3
  ---
4
4
 
5
5
  The S3 storage handles uploads to Amazon S3 service, using the [aws-sdk-s3]
@@ -232,10 +232,6 @@ When downloading encrypted S3 objects, the same server-side encryption
232
232
  parameters need to be passed in.
233
233
 
234
234
  ```rb
235
- s3.download("key", sse_customer_algorithm: "AES256",
236
- sse_customer_key: "secret_key",
237
- sse_customer_key_md5: "secret_key_md5")
238
-
239
235
  s3.open("key", sse_customer_algorithm: "AES256",
240
236
  sse_customer_key: "secret_key",
241
237
  sse_customer_key_md5: "secret_key_md5")
@@ -283,7 +283,7 @@ attacher.destroy_background # calls destroy block
283
283
  ## Versions
284
284
 
285
285
  The `versions`, `processing`, `recache`, and `delete_raw` plugins have been
286
- deprecated in favour of the new [`derivatives`][derivatives] plugin. Let's
286
+ deprecated in favour of the new **[`derivatives`][derivatives]** plugin. Let's
287
287
  assume you have the following `versions` code:
288
288
 
289
289
  ```rb
@@ -318,7 +318,8 @@ else
318
318
  end
319
319
  ```
320
320
 
321
- With `derivatives` it becomes this:
321
+ With `derivatives`, the original file is automatically downloaded and retained,
322
+ so the code is now much simpler:
322
323
 
323
324
  ```rb
324
325
  Shrine.plugin :derivatives, versions_compatibility: true # handle versions column format
@@ -328,6 +329,7 @@ class ImageUploader < Shrine
328
329
  Attacher.derivatives_processor do |original|
329
330
  magick = ImageProcessing::MiniMagick.source(original)
330
331
 
332
+ # the :original file should NOT be included anymore
331
333
  {
332
334
  large: magick.resize_to_limit!(800, 800),
333
335
  medium: magick.resize_to_limit!(500, 500),
@@ -88,10 +88,11 @@ when defining more validations:
88
88
  class ApplicationUploader < Shrine
89
89
  Attacher.validate { validate_max_size 5*1024*1024 }
90
90
  end
91
-
91
+ ```
92
+ ```rb
92
93
  class ImageUploader < ApplicationUploader
93
94
  Attacher.validate do
94
- super() # empty braces are required
95
+ super() # empty parentheses are required
95
96
  validate_mime_type %w[image/jpeg image/png image/webp]
96
97
  end
97
98
  end
@@ -11,8 +11,7 @@ require "json"
11
11
  require "tempfile"
12
12
  require "logger"
13
13
 
14
- # Core class that represents uploader.
15
- # Base implementation is defined in InstanceMethods and ClassMethods.
14
+ # Core class that handles uploading files to specified storage.
16
15
  class Shrine
17
16
  # A generic exception used by Shrine.
18
17
  class Error < StandardError; end
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Shrine
4
- # Core class which handles attaching files to model instances.
5
- # Base implementation is defined in InstanceMethods and ClassMethods.
4
+ # Core class that handles attaching files. It uses Shrine and
5
+ # Shrine::UploadedFile objects internally.
6
6
  class Attacher
7
7
  @shrine_class = ::Shrine
8
8
 
@@ -46,9 +46,9 @@ class Shrine
46
46
  end
47
47
 
48
48
  # Returns the temporary storage identifier.
49
- def cache_key; @cache; end
49
+ def cache_key; @cache.to_sym; end
50
50
  # Returns the permanent storage identifier.
51
- def store_key; @store; end
51
+ def store_key; @store.to_sym; end
52
52
 
53
53
  # Returns the uploader that is used for the temporary storage.
54
54
  def cache; shrine_class.new(cache_key); end
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Shrine
4
- # Core class which creates attachment modules for specified attribute names
5
- # that are included into model classes.
6
- # Base implementation is defined in InstanceMethods and ClassMethods.
4
+ # Core class that provides an attachment interface for a specified attribute
5
+ # name, which can be added to model/entity classes. The model/entity plugins
6
+ # define the main interface, which delegates to a Shrine::Attacher object.
7
7
  class Attachment < Module
8
8
  @shrine_class = ::Shrine
9
9
 
@@ -22,7 +22,7 @@ class Shrine
22
22
  private
23
23
 
24
24
  def _metadata_method(name)
25
- FileMethods.send(:define_method, name) do
25
+ self::UploadedFile.send(:define_method, name) do
26
26
  metadata[name.to_s]
27
27
  end
28
28
  end
@@ -54,10 +54,6 @@ class Shrine
54
54
  end
55
55
  end
56
56
  end
57
-
58
- module FileMethods
59
- # methods will be dynamically defined here through `Shrine.add_metadata`
60
- end
61
57
  end
62
58
 
63
59
  register_plugin(:add_metadata, AddMetadata)
@@ -34,12 +34,12 @@ class Shrine
34
34
  if @cache.respond_to?(:call)
35
35
  if @cache.arity == 2
36
36
  Shrine.deprecation("Passing record & name argument to default storage block is deprecated and will be removed in Shrine 4. Use a block without arguments instead.")
37
- @cache.call(record, name)
37
+ @cache.call(record, name).to_sym
38
38
  else
39
- instance_exec(&@cache)
39
+ instance_exec(&@cache).to_sym
40
40
  end
41
41
  else
42
- @cache
42
+ super
43
43
  end
44
44
  end
45
45
 
@@ -47,12 +47,12 @@ class Shrine
47
47
  if @store.respond_to?(:call)
48
48
  if @store.arity == 2
49
49
  Shrine.deprecation("Passing record & name argument to default storage block is deprecated and will be removed in Shrine 4. Use a block without arguments instead.")
50
- @store.call(record, name)
50
+ @store.call(record, name).to_sym
51
51
  else
52
- instance_exec(&@store)
52
+ instance_exec(&@store).to_sym
53
53
  end
54
54
  else
55
- @store
55
+ super
56
56
  end
57
57
  end
58
58
  end
@@ -64,6 +64,7 @@ class Shrine
64
64
  processor
65
65
  end
66
66
  end
67
+ alias derivatives derivatives_processor
67
68
 
68
69
  # Specifies default storage to which derivatives will be uploaded.
69
70
  #
@@ -179,9 +180,9 @@ class Shrine
179
180
  # end
180
181
  #
181
182
  # attacher.create_derivatives(:my_processor)
182
- def create_derivatives(*args)
183
- files = process_derivatives(*args)
184
- add_derivatives(files)
183
+ def create_derivatives(*args, storage: nil, **options)
184
+ files = process_derivatives(*args, **options)
185
+ add_derivatives(files, storage: storage)
185
186
  end
186
187
 
187
188
  # Uploads given hash of files and adds uploaded files to the
@@ -21,10 +21,13 @@ class Shrine
21
21
  module ClassMethods
22
22
  # Calculates `algorithm` hash of the contents of the IO object, and
23
23
  # encodes it into `format`.
24
- def calculate_signature(io, algorithm, format: :hex)
25
- instrument_signature(io, algorithm, format) do
26
- SignatureCalculator.new(algorithm.downcase, format: format).call(io)
27
- end
24
+ def calculate_signature(io, algorithm, format: :hex, rewind: true)
25
+ calculator = SignatureCalculator.new(algorithm.downcase, format: format)
26
+
27
+ signature = instrument_signature(io, algorithm, format) { calculator.call(io) }
28
+ io.rewind if rewind
29
+
30
+ signature
28
31
  end
29
32
  alias signature calculate_signature
30
33
 
@@ -62,8 +65,6 @@ class Shrine
62
65
 
63
66
  def call(io)
64
67
  hash = send(:"calculate_#{algorithm}", io)
65
- io.rewind
66
-
67
68
  send(:"encode_#{format}", hash)
68
69
  end
69
70
 
@@ -113,23 +113,32 @@ class Shrine
113
113
 
114
114
  def extract_with_fastimage(io)
115
115
  require "fastimage"
116
- FastImage.size(io, raise_on_failure: true)
117
- rescue FastImage::FastImageException => error
118
- on_error(error)
116
+
117
+ begin
118
+ FastImage.size(io, raise_on_failure: true)
119
+ rescue FastImage::FastImageException => error
120
+ on_error(error)
121
+ end
119
122
  end
120
123
 
121
124
  def extract_with_mini_magick(io)
122
125
  require "mini_magick"
123
- Shrine.with_file(io) { |file| MiniMagick::Image.new(file.path).dimensions }
124
- rescue MiniMagick::Error => error
125
- on_error(error)
126
+
127
+ begin
128
+ Shrine.with_file(io) { |file| MiniMagick::Image.new(file.path).dimensions }
129
+ rescue MiniMagick::Error => error
130
+ on_error(error)
131
+ end
126
132
  end
127
133
 
128
134
  def extract_with_ruby_vips(io)
129
135
  require "vips"
130
- Shrine.with_file(io) { |file| Vips::Image.new_from_file(file.path).size }
131
- rescue Vips::Error => error
132
- on_error(error)
136
+
137
+ begin
138
+ Shrine.with_file(io) { |file| Vips::Image.new_from_file(file.path).size }
139
+ rescue Vips::Error => error
140
+ on_error(error)
141
+ end
133
142
  end
134
143
 
135
144
  def on_error(error)
@@ -6,7 +6,6 @@ require "uri"
6
6
 
7
7
  class Shrine
8
8
  # Core class that represents a file uploaded to a storage.
9
- # Base implementation is defined in InstanceMethods and ClassMethods.
10
9
  class UploadedFile
11
10
  @shrine_class = ::Shrine
12
11
 
@@ -7,8 +7,8 @@ class Shrine
7
7
 
8
8
  module VERSION
9
9
  MAJOR = 3
10
- MINOR = 0
11
- TINY = 1
10
+ MINOR = 1
11
+ TINY = 0
12
12
  PRE = nil
13
13
 
14
14
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shrine
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-17 00:00:00.000000000 Z
11
+ date: 2019-11-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: down
@@ -467,6 +467,7 @@ files:
467
467
  - doc/release_notes/2.9.0.md
468
468
  - doc/release_notes/3.0.0.md
469
469
  - doc/release_notes/3.0.1.md
470
+ - doc/release_notes/3.1.0.md
470
471
  - doc/retrieving_uploads.md
471
472
  - doc/securing_uploads.md
472
473
  - doc/storage/file_system.md