Use content_disposition gem for :content_disposition create_multipart_upload option

parent 3bbe43e4
......@@ -280,14 +280,15 @@ options: {
In that case a [`Rack::Request`] object is also passed to the block. The
initial request to `POST /s3/multipart` will contain `type` and `filename`
query parameters, so for example you could use that to make requesting the URL
later force a download with the original filename:
later force a download with the original filename (using the
[content_disposition] gem):
```rb
options: {
create_multipart_upload: -> (request) do
filename = request.params["filename"]
{ content_disposition: "attachment; filename=\"#{CGI.escape(filename)}\"" }
{ content_disposition: ContentDisposition.attachment(filename) }
end
}
```
......@@ -477,6 +478,7 @@ License](https://opensource.org/licenses/MIT).
[Adding Direct S3 Uploads]: https://github.com/shrinerb/shrine/wiki/Adding-Direct-S3-Uploads
[Minio]: https://minio.io/
[Client]: #client
[content_disposition]: https://github.com/shrinerb/content_disposition
[`Rack::Request`]: https://www.rubydoc.info/github/rack/rack/master/Rack/Request
[`Aws::S3::Client#initialize`]: https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Client.html#initialize-instance_method
[`Aws::S3::Client#create_multipart_upload`]: https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Client.html#create_multipart_upload-instance_method
......
require "uppy/s3_multipart/client"
require "roda"
require "content_disposition"
require "securerandom"
require "cgi"
module Uppy
module S3Multipart
......@@ -40,8 +40,7 @@ module Uppy
key = SecureRandom.hex + extension
key = "#{opts[:prefix]}/#{key}" if opts[:prefix]
# CGI-escape the filename because aws-sdk's signature calculator trips on special characters
content_disposition = "inline; filename=\"#{CGI.escape(filename)}\"" if filename
content_disposition = ContentDisposition.inline(filename) if filename
options = { content_type: content_type, content_disposition: content_disposition }
options[:acl] = "public-read" if opts[:public]
......
......@@ -49,8 +49,10 @@ describe Uppy::S3Multipart::App do
it "handles 'filename' JSON body parameter" do
response = app.post "/s3/multipart", json: { filename: "nature.jpg" }
expected_content_disposition = %(inline; filename="nature.jpg"; filename*=UTF-8''nature.jpg)
assert_equal :create_multipart_upload, @s3.api_requests[0][:operation_name]
assert_equal "inline; filename=\"nature.jpg\"", @s3.api_requests[0][:params][:content_disposition]
assert_equal expected_content_disposition, @s3.api_requests[0][:params][:content_disposition]
assert_match /^\w{32}\.jpg$/, response.body_json["key"]
end
......
......@@ -15,6 +15,7 @@ Gem::Specification.new do |gem|
gem.add_dependency "roda", ">= 2.27", "< 4"
gem.add_dependency "aws-sdk-s3", "~> 1.0"
gem.add_dependency "content_disposition", "~> 1.0"
gem.add_development_dependency "rake"
gem.add_development_dependency "minitest"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment