shrine 2.16.0 → 2.17.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.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6a54ce86ad9f430b7c0896fa83db280316dd3e48a27bff320445b561b966bdcd
4
- data.tar.gz: 2924f5048e3ff3991d49da834000564612eba1749576c7ea42cf446cb025c7ed
3
+ metadata.gz: e01f77764b72ba6acce9663b1dc0652afb5c645682aaec7972ebeeda8e326655
4
+ data.tar.gz: 5f9e8accd7448390a6f3cd25158eb01295ec6e1ac9fb8e70b10c2adf56f34e4a
5
5
  SHA512:
6
- metadata.gz: 7a0e9566b2b6b7a78c2a85019108147bcd071b46cef63fb1cdc848ee1fb1a8cf086afce8bf62d966e88cbeefe35d4437d471c7a7180a8abd34682d47fe62b60b
7
- data.tar.gz: 013cb42c24c2af94c2aa84344c5ce9e07e6001dfb56a06e2b6f4c4805969c4fa2d45421a455e34d1ec9290607c5ffebddd0114d91871cd99e84b6623cb65527e
6
+ metadata.gz: 1bb7469736eeb705faa8bc68e7afa1373d0d3e0af221e5a1372bf829f4c0c553726b4a5cc3aa82081e5c54781636bf8d4a4e97a703f08dfc0ed0162d8470aa9b
7
+ data.tar.gz: d0b0bd017bb9e79c2f7761640ed664ea8f673975ee86871d18df550c04838bdf7df0a3e50a0c96b0b59d9192b459ace9052a909624da7dc8ed6a5dc313a84c70
@@ -1,3 +1,53 @@
1
+ ## 2.17.0 (2019-05-06)
2
+
3
+ * `data_uri` – Add `Attacher#assign_data_uri` which accepts additional `Shrine#upload` options (@janko)
4
+
5
+ * `remote_url` – Accept additional `Shrine#upload` options in `Attacher#assign_remote_url` (@janko)
6
+
7
+ * `download_endpoint` – Allow passing options to `Shrine.download_endpoint` (@janko)
8
+
9
+ * `download_endpoint` – Fix `Shrine.download_endpoint` not being accepted by Rails' `#mount` (@janko)
10
+
11
+ * `download_endpoint` – Remove Roda dependency (@janko)
12
+
13
+ * `presign_endpoint` – Soft-rename `Shrine::Plugins::PresignEndpoint::App` class to `Shrine::PresignEndpoint` (@janko)
14
+
15
+ * `upload_endpoint` – Soft-rename `Shrine::Plugins::UploadEndpoint::App` class to `Shrine::UploadEndpoint` (@janko)
16
+
17
+ * `processing` – Fix defining process blocks being applied to `Shrine` superclasses (@ksol)
18
+
19
+ * `derivation_endpoint` – Add `ETag` header to prevent `Rack::ETag` from buffering file content (@janko)
20
+
21
+ * `rack_response` – Add `ETag` header to prevent `Rack::ETag` from buffering file content (@janko)
22
+
23
+ * `download_endpoint` – Add `ETag` header to prevent `Rack::ETag` from buffering file content (@janko)
24
+
25
+ * `default_url` – Add `:host` for specifying the URL host (@janko)
26
+
27
+ * `versions` – Fix uploaded versions being deleted when string version names are used (@janko)
28
+
29
+ * `versions` – Allow `Attacher#url` to accept version name indifferently (@FunkyloverOne)
30
+
31
+ * Improve performance of cleaning empty directories on deletion in `FileSystem` storage (@adamniedzielski)
32
+
33
+ * Drop MRI 2.3 support (@janko)
34
+
35
+ * `metadata_attributes` – Fix `Attacher#assign` not accepting additional options anymore (@janko)
36
+
37
+ * `derivation_endpoint` – Add support for Rack < 2 (@Antsiscool)
38
+
39
+ * `derivation_endpoint` – Fix `:upload` option being incompatible with `moving` plugin (@speedo-spin)
40
+
41
+ * `determine_mime_type` – Allow passing options to analzyers (Marcel accepts `:filename_fallback` option) (@hmistry)
42
+
43
+ * `determine_mime_type` – Revert "Extended determine MIME type with Marcel" (@hmistry)
44
+
45
+ * `rack_response` – improve performance for upper bounded `Range` header values (@zarqman)
46
+
47
+ * `rack_response` – prevent response body from yielding `nil`-chunks (@zarqman)
48
+
49
+ * `parsed_json` – Accepts hashes with symbols keys (@aglushkov)
50
+
1
51
  ## 2.16.0 (2019-02-18)
2
52
 
3
53
  * `derivation_endpoint` – Add `:upload_open_options` for download option for derivation result (@janko)
@@ -34,6 +34,15 @@ plugin :data_uri, error_message: "data URI was invalid"
34
34
  plugin :data_uri, error_message: ->(uri) { I18n.t("errors.data_uri_invalid") }
35
35
  ```
36
36
 
37
+ ## Upload options
38
+
39
+ If you want to pass additional options for `Shrine#upload`, you can use
40
+ `#assign_data_uri`:
41
+
42
+ ```rb
43
+ attacher.assign_data_uri(uri, metadata: { "mime_type" => "text/plain" })
44
+ ```
45
+
37
46
  ## File extension
38
47
 
39
48
  A data URI doesn't convey any information about the file extension, so when
@@ -32,4 +32,16 @@ Attacher.default_url do |options|
32
32
  end
33
33
  ```
34
34
 
35
+ ## Host
36
+
37
+ If the default URL is relative, the URL host can be specified via the `:host`
38
+ option:
39
+
40
+ ```rb
41
+ plugin :default_url, host: "https://example.com"
42
+ ```
43
+ ```rb
44
+ user.avatar_url #=> "https://example.com/avatar/missing.jpg"
45
+ ```
46
+
35
47
  [default_url]: /lib/shrine/plugins/default_url.rb
@@ -445,9 +445,10 @@ plugin :derivation_endpoint, upload: true,
445
445
 
446
446
  ### Redirecting
447
447
 
448
- You can configure the endpoint to redirect to the uploaded derivative on the
449
- storage instead of serving it through the endpoint (which is the default
450
- behaviour) by setting both `:upload` and `:upload_redirect` to `true`:
448
+ If you are using remote cloud storages, you can configure the endpoint to
449
+ redirect the client to the uploaded derivative on the remote storage instead of
450
+ serving it through the endpoint (which is the default behaviour) by setting both
451
+ `:upload` and `:upload_redirect` to `true`:
451
452
 
452
453
  ```rb
453
454
  plugin :derivation_endpoint, upload: true,
@@ -463,6 +464,22 @@ plugin :derivation_endpoint, upload: true,
463
464
  upload_redirect_url_options: { public: true }
464
465
  ```
465
466
 
467
+ If you are using the local filesystem storage, then redirecting does not make
468
+ sense.
469
+
470
+ ### Deleting derivatives
471
+
472
+ When you use the `:upload` options and upload the derivatives to storage, there
473
+ will come a time when you need to delete the derivatives because the original
474
+ file is being replaced, deleted, or some other reason. In this case, you can
475
+ delete the derivatives before deleting the record or updating the record with
476
+ the new original file.
477
+
478
+ ```rb
479
+ # photo is the model and image is the file attachment
480
+ photo.image.derivation(:thumbnail).delete
481
+ ```
482
+
466
483
  ## Cache busting
467
484
 
468
485
  The derivation endpoint response instructs browsers, CDNs and other clients to
@@ -567,8 +584,8 @@ these errors converted to 404 responses by adding them to `:download_errors`:
567
584
 
568
585
  ```rb
569
586
  plugin :derivation_endpoint, download_errors: [
570
- Errno::ENOENT, # raised by Shrine::Storage::FileSystem
571
- Aws::S3::Errors::NoSuchKey, # raised by Shrine::Storage::S3
587
+ Errno::ENOENT, # raised by Shrine::Storage::FileSystem
588
+ Aws::S3::Errors::NotFound, # raised by Shrine::Storage::S3
572
589
  ]
573
590
  ```
574
591
 
@@ -9,17 +9,19 @@ plugin :determine_mime_type
9
9
 
10
10
  By default the UNIX [file] utility is used to determine the MIME type, and the
11
11
  result is automatically written to the `mime_type` metadata field. You can
12
- choose a different built-in MIME type analyzer:
12
+ choose a different built-in MIME type analyzer, for example:
13
13
 
14
14
  ```rb
15
15
  plugin :determine_mime_type, analyzer: :marcel
16
16
  ```
17
17
 
18
+ ## Analyzers
19
+
18
20
  The following analyzers are accepted:
19
21
 
20
22
  | Name | Description |
21
23
  | :------ | :----------- |
22
- | `:file` | (Default). Uses the [file] utility to determine the MIME type from file contents. It is installed by default on most operating systems, but the [Windows equivalent] needs to be installed separately. |
24
+ | `:file` | (**Default**). Uses the [file] utility to determine the MIME type from file contents. It is installed by default on most operating systems, but the [Windows equivalent] needs to be installed separately. |
23
25
  | `:fastimage` | Uses the [fastimage] gem to determine the MIME type from file contents. Fastimage is optimized for speed over accuracy. Best used for image content. |
24
26
  | `:filemagic` | Uses the [ruby-filemagic] gem to determine the MIME type from file contents, using a similar MIME database as the `file` utility. Unlike the `file` utility, ruby-filemagic works on Windows without any setup. |
25
27
  | `:mimemagic` | Uses the [mimemagic] gem to determine the MIME type from file contents. Unlike ruby-filemagic, mimemagic is a pure-ruby solution, so it will work across all Ruby implementations. |
@@ -28,8 +30,33 @@ The following analyzers are accepted:
28
30
  | `:mini_mime` | Uses the [mini_mime] gem to determine the MIME type from the file extension. Note that unlike other solutions, this analyzer is not guaranteed to return the actual MIME type of the file. |
29
31
  | `:content_type` | Retrieves the value of the `#content_type` attribute of the IO object. Note that this value normally comes from the "Content-Type" request header, so it's not guaranteed to hold the actual MIME type of the file. |
30
32
 
31
- A single analyzer is not going to properly recognize all types of files, so you
32
- can build your own custom analyzer for your requirements, where you can combine
33
+ You'll need to ensure the file utility or gem is in installed on your system
34
+ for Shrine to use the analyzer.
35
+
36
+ ## Analyzer Options
37
+
38
+ Currently, Marcel is the only analyzer that accepts options to be passed in.
39
+ Analyzer options can be set in one of two ways:
40
+
41
+ ```rb
42
+ plugin :determine_mime_type, analyzer: :marcel, analyzer_options: { filename_fallback: true }
43
+
44
+ # or
45
+
46
+ plugin :determine_mime_type, analyzer: -> (io, analyzers) do
47
+ mime_type = analyzers[:marcel].call(io, filename_fallback: true)
48
+ end
49
+ ```
50
+
51
+ The `:filename_fallback` option for Marcel analyzer lets it use the filename as
52
+ a fallback option when it fails to determine the MIME type from the file
53
+ content. Set `:filename_fallback` to `true` in order to fallback to using the
54
+ filename or set to `false` to not use the filename (this is the default).
55
+
56
+ ## Issues
57
+
58
+ If you find using a single analyzer is not able to recognize all of the file
59
+ types in your application, you can build your own custom analyzer and combine
33
60
  the built-in analyzers. For example, if you want to correctly determine MIME
34
61
  type of .css, .js, .json, .csv, .xml, or similar text-based files, you can
35
62
  combine `file` and `mime_types` analyzers:
@@ -42,7 +69,9 @@ plugin :determine_mime_type, analyzer: -> (io, analyzers) do
42
69
  end
43
70
  ```
44
71
 
45
- You can also use methods for determining the MIME type directly:
72
+ ## Other Usage
73
+
74
+ You can also use the methods for determining the MIME type directly:
46
75
 
47
76
  ```rb
48
77
  # or YourUploader.determine_mime_type(io)
@@ -3,12 +3,7 @@
3
3
  The [`download_endpoint`][download_endpoint] plugin provides a Rack app for
4
4
  downloading uploaded files from specified storages. This can be useful when
5
5
  files from your storage isn't accessible over URL (e.g. database storages) or
6
- if you want to authenticate your downloads. It requires the [Roda] gem.
7
-
8
- ```rb
9
- # Gemfile
10
- gem "roda" # dependency of the download_endpoint plugin
11
- ```
6
+ if you want to authenticate your downloads.
12
7
 
13
8
  You can configure the plugin with the path prefix which the endpoint will be
14
9
  mounted on.
@@ -104,6 +99,14 @@ plugin :download_endpoint, redirect: -> (uploaded_file, request) do
104
99
  end
105
100
  ```
106
101
 
102
+ ## Ad-hoc options
103
+
104
+ You can override any of the options above when creating the endpoint:
105
+
106
+ ```rb
107
+ Shrine.download_endpoint(disposition: "attachment")
108
+ ```
109
+
107
110
  ## Custom endpoint
108
111
 
109
112
  If you want to have more control on download requests, you can use the
@@ -120,4 +123,3 @@ If you want to have more control on download requests, you can use the
120
123
  | `:redirect` | Whether to redirect to uploaded files on the storage | `false` |
121
124
 
122
125
  [download_endpoint]: /lib/shrine/plugins/download_endpoint.rb
123
- [Roda]: https://github.com/jeremyevans/roda
@@ -37,7 +37,13 @@ You can dynamically pass options to the downloader by using
37
37
  `Attacher#assign_remote_url`:
38
38
 
39
39
  ```rb
40
- attacher.assign_remote_url("http://example.com/cool-image.png", downloader: { 'Authorization' => 'Basic ...' })
40
+ attacher.assign_remote_url(url, downloader: { 'Authorization' => 'Basic ...' })
41
+ ```
42
+
43
+ You can also pass any other `Shrine#upload` options:
44
+
45
+ ```rb
46
+ attacher.assign_remote_url(url, metadata: { "mime_type" => "text/plain" })
41
47
  ```
42
48
 
43
49
  ## Maximum size
@@ -0,0 +1,129 @@
1
+ ## New features
2
+
3
+ * The `download_endpoint` plugin now accepts ad-hoc options when creating the
4
+ endpoint instance, which override any plugin options.
5
+
6
+ ```rb
7
+ Shrine.download_enpdoint(disposition: "attachment")
8
+ ```
9
+
10
+ * The `Shrine::Attacher#assign_remote_url` method in the `remote_url` plugin
11
+ now accepts additional options forwarded to `Shrine#upload`.
12
+
13
+ ```rb
14
+ attacher.assign_remote_url(url, metadata: { "mime_type" => "text/plain" })
15
+ ```
16
+
17
+ * The `Shrine::Attacher#assign_data_uri` method has been added to the
18
+ `data_uri` plugin, which accepts additional `Shrine#upload` options.
19
+
20
+ ```rb
21
+ attacher.assign_data_uri(uri, metadata: { "filename" => "custom.txt" })
22
+ ```
23
+
24
+ * The `default_url` plugin now accepts a `:host` option for specifying the URL
25
+ host.
26
+
27
+ ```rb
28
+ plugin :default_url, host: "https://example.com"
29
+
30
+ Attacher.default_url { "/#{name}/missing.png" }
31
+ ```
32
+ ```rb
33
+ user.avatar_url #=> "https://example.com/avatar/missing.png"
34
+ ```
35
+
36
+ ## Other improvements
37
+
38
+ * The `derivation_endpoint` plugin now works correctly when `moving` plugin
39
+ is loaded and `upload: true` is set.
40
+
41
+ * The `Shrine.download_endpoint` object is now compatible with the Rails router
42
+ again (this was broken in Shrine 2.15.0).
43
+
44
+ * The `rack_response` plugin was in certain cases producing `nil`-chunks for
45
+ the response body, which caused exceptions with some web servers. This has
46
+ now been fixed.
47
+
48
+ * The `rack_response` plugin now stops retrieving file content that goes past
49
+ the range specified in the `Range` header. This improves performance for
50
+ storage classes that internally use `Down::ChunkedIO`.
51
+
52
+ * The `derivation_endpoint`, `rack_response`, and `download_endpoint` plugins
53
+ now include `ETag` in the headers of responses, preventing the `Rack::ETag`
54
+ middleware from buffering the response body.
55
+
56
+ * Fixed inheritance of `process` blocks in the `processing` plugin. Now adding
57
+ `process` blocks in the `Shrine` subclass won't affect the superclass.
58
+
59
+ * Improved performance of `Shrine::Storage::FileSystem#delete` by switching
60
+ from `Dir.foreach` to `Dir.empty?`.
61
+
62
+ * The `parsed_json` plugin now accepts hashes with symbol keys.
63
+
64
+ * The `versions` plugin now accepts string version names in
65
+ `Shrine::Attacher#url`.
66
+
67
+ * The `versions` plugin now supports returning a hash of versions with string
68
+ names inside the `process` block.
69
+
70
+ * The `download_endpoint` plugin has been rewritten to use plain Rack, so Roda
71
+ isn't a dependency anymore.
72
+
73
+ * The endpoint's `#inspect` and `#to_s` outputs in `upload_endpoint`,
74
+ `presign_endpoint`, `download_endpoint`, and `derivation_endpoint` plugins
75
+ have been simplified. Now the `rake routes` command output in Rails apps
76
+ should be much more compact.
77
+
78
+ ```rb
79
+ # BEFORE:
80
+ ImageUploader.upload_endpoint(:cache) #=> #<Shrine::Plugins::UploadEndpoint::App:0x00007f91bec17dc8 @max_size=nil, @rack_response=nil, @shrine_class=ImageUploader, @storage_key=:cache, @upload=nil, @upload_context=nil>
81
+ ImageUploader.presign_endpoint(:cache) #=> #<Shrine::Plugins::PresignEndpoint::App:0x00007f91beca4ea8 @presign=nil, @presign_location=nil, @presign_options=nil, @rack_response=nil, @shrine_class=ImageUploader, @storage_key=:cache>
82
+ ImageUploader.download_endpoint #=> #<Class:0x00007f91bed1fa90>
83
+ ImageUploader.derivation_endpoint #=> #<Shrine::DerivationEndpoint:0x00007f91bed4c900 @options={}, @shrine_class=ImageUploader>
84
+
85
+ # AFTER:
86
+ ImageUploader.upload_endpoint(:cache) #=> #<ImageUploader::UploadEndpoint(:cache)>
87
+ ImageUploader.presign_endpoint(:cache) #=> #<ImageUploader::PresignEndpoint(:cache)>
88
+ ImageUploader.download_endpoint #=> #<ImageUploader::DownloadEndpoint>
89
+ ImageUploader.derivation_endpoint #=> #<ImageUploader::DerivationEndpoint>
90
+ ```
91
+
92
+ * The `derivation_endpoint` plugin is now compatible with Rack 1.6.
93
+
94
+ * The `metadata_attributes` plugin now preserves `Attacher#assign` method
95
+ signature, by retaining the second `options` argument.
96
+
97
+ ## Backwards compatibility
98
+
99
+ * The support for MRI 2.3 has been dropped.
100
+
101
+ * The automatic filename fallback was removed from the `:marcel` analyzer in
102
+ `determine_mime_type` plugin, as in some cases it caused MIME type to be
103
+ incorrectly determined and could cause potential security issues. You can
104
+ still opt in for it:
105
+
106
+ ```rb
107
+ plugin :determine_mime_type, analyzer: :marcel, analyzer_options: { filename_fallback: true }
108
+ ```
109
+
110
+ * The `Shrine::Plugins::UploadEndpoint::App` class from the `upload_endpoint`
111
+ plugin has been renamed to `Shrine::UploadEndpoint`. The old constant will be
112
+ removed in Shrine 3.
113
+
114
+ * The `Shrine::Plugins::PresignEndpoint::App` class from the `presign_endpoint`
115
+ plugin has been renamed to `Shrine::PresignEndpoint`. The old constant will
116
+ be removed in Shrine 3.
117
+
118
+ * The `Shrine::Plugins::DownloadEndpoint::App` class is not a `Roda` subclass
119
+ anymore, it's now a PORO whose instance responds to `#call`. This shouldn't
120
+ affect your code unless you were calling Roda methods on that class.
121
+
122
+ * The plugin options of `upload_enpdoint`, `presign_endpoint`, and
123
+ `download_endpoint` are now internally stored in a different place in
124
+ `Shrine.opts`. This shouldn't affect your code unless you were accessing
125
+ these options directly.
126
+
127
+ * The `Shrine::UrlSigner` internal class in `derivation_endpoint` plugin has
128
+ been updated. This shouldn't affect your code unless you were
129
+ using/overriding that class.
@@ -254,12 +254,11 @@ Shrine::Storage::S3(client: client, bucket: "my-bucket")
254
254
 
255
255
  ## Accelerate endpoint
256
256
 
257
- To use Amazon S3's [Transfer Acceleration] feature, you can change the
258
- `:endpoint` of the underlying client to the accelerate endpoint, and this will
259
- be applied both to regular and presigned uploads, as well as download URLs.
257
+ To use Amazon S3's [Transfer Acceleration] feature, set
258
+ `:use_accelerate_endpoint` to `true` when initializing the storage:
260
259
 
261
260
  ```rb
262
- Shrine::Storage::S3.new(endpoint: "https://s3-accelerate.amazonaws.com")
261
+ Shrine::Storage::S3.new(use_accelerate_enpdoint: true, **other_options)
263
262
  ```
264
263
 
265
264
  ## Clearing cache
@@ -83,11 +83,11 @@ class Shrine
83
83
  # the content type, decodes it, wrappes it in a StringIO and assigns it.
84
84
  # If it fails, it sets the error message and assigns the uri in an
85
85
  # instance variable so that it shows up on the UI.
86
- def data_uri=(uri)
86
+ def assign_data_uri(uri, **options)
87
87
  return if uri == "" || uri.nil?
88
88
 
89
89
  data_file = shrine_class.data_uri(uri)
90
- assign(data_file)
90
+ assign(data_file, **options)
91
91
  rescue ParseError => error
92
92
  message = shrine_class.opts[:data_uri_error_message] || error.message
93
93
  message = message.call(uri) if message.respond_to?(:call)
@@ -95,6 +95,11 @@ class Shrine
95
95
  @data_uri = uri
96
96
  end
97
97
 
98
+ # Alias for #assign_data_uri.
99
+ def data_uri=(uri)
100
+ assign_data_uri(uri)
101
+ end
102
+
98
103
  # Form builders require the reader as well.
99
104
  def data_uri
100
105
  @data_uri
@@ -6,7 +6,9 @@ class Shrine
6
6
  #
7
7
  # [doc/plugins/default_url.md]: https://github.com/shrinerb/shrine/blob/master/doc/plugins/default_url.md
8
8
  module DefaultUrl
9
- def self.configure(uploader, &block)
9
+ def self.configure(uploader, opts = {}, &block)
10
+ uploader.opts[:default_url_host] = opts.fetch(:host, uploader.opts[:default_url_host])
11
+
10
12
  if block
11
13
  uploader.opts[:default_url] = block
12
14
  Shrine.deprecation("Passing a block to default_url plugin is deprecated and will probably be removed in future versions of Shrine. Use `Attacher.default_url { ... }` instead.")
@@ -27,16 +29,26 @@ class Shrine
27
29
  private
28
30
 
29
31
  def default_url(**options)
30
- if default_url_block
31
- instance_exec(options, &default_url_block)
32
- elsif shrine_class.opts[:default_url]
33
- shrine_class.opts[:default_url].call(context.merge(options){|k, old, new| old})
32
+ url = if default_url_block
33
+ instance_exec(options, &default_url_block)
34
+ elsif shrine_class.opts[:default_url]
35
+ shrine_class.opts[:default_url].call(context.merge(options){|k, old, new| old})
36
+ end
37
+
38
+ if default_url_host
39
+ [default_url_host, url].join
40
+ else
41
+ url
34
42
  end
35
43
  end
36
44
 
37
45
  def default_url_block
38
46
  shrine_class.opts[:default_url_block]
39
47
  end
48
+
49
+ def default_url_host
50
+ shrine_class.opts[:default_url_host]
51
+ end
40
52
  end
41
53
  end
42
54