Skip to content

Commit

Permalink
Merge pull request #36791 from peterzhu2118/s3-upload-disposition
Browse files Browse the repository at this point in the history
Upload with filename and disposition for S3
  • Loading branch information
gmcgibbon committed Jul 31, 2019
2 parents 33671d3 + e6d2e8b commit 4b65173
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
16 changes: 9 additions & 7 deletions activestorage/lib/active_storage/service/s3_service.rb
Expand Up @@ -20,12 +20,14 @@ def initialize(bucket:, upload: {}, **options)
@upload_options = upload
end

def upload(key, io, checksum: nil, content_type: nil, **)
def upload(key, io, checksum: nil, filename: nil, content_type: nil, disposition: nil, **)
instrument :upload, key: key, checksum: checksum do
content_disposition = content_disposition_with(filename: filename, type: disposition) if disposition && filename

if io.size < multipart_upload_threshold
upload_with_single_part key, io, checksum: checksum, content_type: content_type
upload_with_single_part key, io, checksum: checksum, content_type: content_type, content_disposition: content_disposition
else
upload_with_multipart key, io, content_type: content_type
upload_with_multipart key, io, content_type: content_type, content_disposition: content_disposition
end
end
end
Expand Down Expand Up @@ -103,16 +105,16 @@ def headers_for_direct_upload(key, content_type:, checksum:, **)
MAXIMUM_UPLOAD_PARTS_COUNT = 10000
MINIMUM_UPLOAD_PART_SIZE = 5.megabytes

def upload_with_single_part(key, io, checksum: nil, content_type: nil)
object_for(key).put(body: io, content_md5: checksum, content_type: content_type, **upload_options)
def upload_with_single_part(key, io, checksum: nil, content_type: nil, content_disposition: nil)
object_for(key).put(body: io, content_md5: checksum, content_type: content_type, content_disposition: content_disposition, **upload_options)
rescue Aws::S3::Errors::BadDigest
raise ActiveStorage::IntegrityError
end

def upload_with_multipart(key, io, content_type: nil)
def upload_with_multipart(key, io, content_type: nil, content_disposition: nil)
part_size = [ io.size.fdiv(MAXIMUM_UPLOAD_PARTS_COUNT).ceil, MINIMUM_UPLOAD_PART_SIZE ].max

object_for(key).upload_stream(content_type: content_type, part_size: part_size, **upload_options) do |out|
object_for(key).upload_stream(content_type: content_type, content_disposition: content_disposition, part_size: part_size, **upload_options) do |out|
IO.copy_stream(io, out)
end
end
Expand Down
17 changes: 17 additions & 0 deletions activestorage/test/service/s3_service_test.rb
Expand Up @@ -77,6 +77,23 @@ class ActiveStorage::Service::S3ServiceTest < ActiveSupport::TestCase
@service.delete key
end

test "upload with content disposition" do
key = SecureRandom.base58(24)
data = "Something else entirely!"

@service.upload(
key,
StringIO.new(data),
checksum: Digest::MD5.base64digest(data),
filename: ActiveStorage::Filename.new("cool_data.txt"),
disposition: :attachment
)

assert_equal("attachment; filename=\"cool_data.txt\"; filename*=UTF-8''cool_data.txt", @service.bucket.object(key).content_disposition)
ensure
@service.delete key
end

test "uploading a large object in multiple parts" do
service = build_service(upload: { multipart_threshold: 5.megabytes })

Expand Down

0 comments on commit 4b65173

Please sign in to comment.