diff options
Diffstat (limited to 'activestorage')
7 files changed, 50 insertions, 9 deletions
diff --git a/activestorage/app/jobs/active_storage/mirror_job.rb b/activestorage/app/jobs/active_storage/mirror_job.rb index e34faedb56..e70629d6ec 100644 --- a/activestorage/app/jobs/active_storage/mirror_job.rb +++ b/activestorage/app/jobs/active_storage/mirror_job.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "active_support/core_ext/object/try" + # Provides asynchronous mirroring of directly-uploaded blobs. class ActiveStorage::MirrorJob < ActiveStorage::BaseJob queue_as { ActiveStorage.queues[:mirror] } diff --git a/activestorage/lib/active_storage/attached/model.rb b/activestorage/lib/active_storage/attached/model.rb index 06864a846f..4bd6b56b6c 100644 --- a/activestorage/lib/active_storage/attached/model.rb +++ b/activestorage/lib/active_storage/attached/model.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "active_support/core_ext/object/try" + module ActiveStorage # Provides the class-level DSL for declaring an Active Record model's attachments. module Attached::Model diff --git a/activestorage/lib/active_storage/service/azure_storage_service.rb b/activestorage/lib/active_storage/service/azure_storage_service.rb index 8d77e9b20f..0648da70b5 100644 --- a/activestorage/lib/active_storage/service/azure_storage_service.rb +++ b/activestorage/lib/active_storage/service/azure_storage_service.rb @@ -17,10 +17,12 @@ module ActiveStorage @container = container 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 handle_errors do - blobs.create_block_blob(container, key, IO.try_convert(io) || io, content_md5: checksum, content_type: content_type) + content_disposition = content_disposition_with(filename: filename, type: disposition) if disposition && filename + + blobs.create_block_blob(container, key, IO.try_convert(io) || io, content_md5: checksum, content_type: content_type, content_disposition: content_disposition) end end end diff --git a/activestorage/lib/active_storage/service/s3_service.rb b/activestorage/lib/active_storage/service/s3_service.rb index e4bd57048a..a73f6ab526 100644 --- a/activestorage/lib/active_storage/service/s3_service.rb +++ b/activestorage/lib/active_storage/service/s3_service.rb @@ -20,12 +20,14 @@ module ActiveStorage @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 @@ -103,16 +105,16 @@ module ActiveStorage 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 diff --git a/activestorage/test/service/azure_storage_service_test.rb b/activestorage/test/service/azure_storage_service_test.rb index fc7b86ccb0..9eea1b94c8 100644 --- a/activestorage/test/service/azure_storage_service_test.rb +++ b/activestorage/test/service/azure_storage_service_test.rb @@ -23,6 +23,21 @@ if SERVICE_CONFIGURATIONS[:azure] @service.delete key end + test "upload with content disposition" do + key = SecureRandom.base58(24) + data = "Foobar" + + @service.upload(key, StringIO.new(data), checksum: Digest::MD5.base64digest(data), filename: ActiveStorage::Filename.new("test.txt"), disposition: :inline) + + assert_equal("inline; filename=\"test.txt\"; filename*=UTF-8''test.txt", @service.blobs.get_blob_properties(@service.container, key).properties[:content_disposition]) + + url = @service.url(key, expires_in: 2.minutes, disposition: :attachment, content_type: nil, filename: ActiveStorage::Filename.new("test.html")) + response = Net::HTTP.get_response(URI(url)) + assert_match(/attachment;.*test\.html/, response["Content-Disposition"]) + ensure + @service.delete key + end + test "signed URL generation" do url = @service.url(@key, expires_in: 5.minutes, disposition: :inline, filename: ActiveStorage::Filename.new("avatar.png"), content_type: "image/png") diff --git a/activestorage/test/service/s3_service_test.rb b/activestorage/test/service/s3_service_test.rb index b9120770e6..3dd1b59681 100644 --- a/activestorage/test/service/s3_service_test.rb +++ b/activestorage/test/service/s3_service_test.rb @@ -77,6 +77,23 @@ if SERVICE_CONFIGURATIONS[:s3] @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 }) diff --git a/activestorage/test/test_helper.rb b/activestorage/test/test_helper.rb index ac38b9362c..164b0acd96 100644 --- a/activestorage/test/test_helper.rb +++ b/activestorage/test/test_helper.rb @@ -6,6 +6,7 @@ require_relative "dummy/config/environment.rb" require "bundler/setup" require "active_support" require "active_support/test_case" +require "active_support/core_ext/object/try" require "active_support/testing/autorun" require "active_storage/service/mirror_service" require "image_processing/mini_magick" |