From bb148d822cd48688c836fb37ef47c51a150f5e42 Mon Sep 17 00:00:00 2001 From: Tahsin Hasan Date: Sat, 28 Nov 2020 15:33:44 +0600 Subject: [PATCH] Permit hash on direct upload in active storage (#40662) --- .../direct_uploads_controller.rb | 2 +- .../direct_uploads_controller_test.rb | 49 +++++++++++++++++-- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/activestorage/app/controllers/active_storage/direct_uploads_controller.rb b/activestorage/app/controllers/active_storage/direct_uploads_controller.rb index 7e08974f56..99634597f3 100644 --- a/activestorage/app/controllers/active_storage/direct_uploads_controller.rb +++ b/activestorage/app/controllers/active_storage/direct_uploads_controller.rb @@ -11,7 +11,7 @@ class ActiveStorage::DirectUploadsController < ActiveStorage::BaseController private def blob_args - params.require(:blob).permit(:filename, :byte_size, :checksum, :content_type, :metadata).to_h.symbolize_keys + params.require(:blob).permit(:filename, :byte_size, :checksum, :content_type, metadata: {}).to_h.symbolize_keys end def direct_upload_json(blob) diff --git a/activestorage/test/controllers/direct_uploads_controller_test.rb b/activestorage/test/controllers/direct_uploads_controller_test.rb index 734798ff3c..27e89a7f1c 100644 --- a/activestorage/test/controllers/direct_uploads_controller_test.rb +++ b/activestorage/test/controllers/direct_uploads_controller_test.rb @@ -16,15 +16,23 @@ if SERVICE_CONFIGURATIONS[:s3] && SERVICE_CONFIGURATIONS[:s3][:access_key_id].pr test "creating new direct upload" do checksum = Digest::MD5.base64digest("Hello") + metadata = { + "foo": "bar", + "my_key_1": "my_value_1", + "my_key_2": "my_value_2", + "platform": "my_platform", + "library_ID": "12345" + } post rails_direct_uploads_url, params: { blob: { - filename: "hello.txt", byte_size: 6, checksum: checksum, content_type: "text/plain" } } + filename: "hello.txt", byte_size: 6, checksum: checksum, content_type: "text/plain", metadata: metadata } } response.parsed_body.tap do |details| assert_equal ActiveStorage::Blob.find(details["id"]), ActiveStorage::Blob.find_signed!(details["signed_id"]) assert_equal "hello.txt", details["filename"] assert_equal 6, details["byte_size"] assert_equal checksum, details["checksum"] + assert_equal metadata, details["metadata"].transform_keys(&:to_sym) assert_equal "text/plain", details["content_type"] assert_match SERVICE_CONFIGURATIONS[:s3][:bucket], details["direct_upload"]["url"] assert_match(/s3(-[-a-z0-9]+)?\.(\S+)?amazonaws\.com/, details["direct_upload"]["url"]) @@ -51,15 +59,23 @@ if SERVICE_CONFIGURATIONS[:gcs] test "creating new direct upload" do checksum = Digest::MD5.base64digest("Hello") + metadata = { + "foo": "bar", + "my_key_1": "my_value_1", + "my_key_2": "my_value_2", + "platform": "my_platform", + "library_ID": "12345" + } post rails_direct_uploads_url, params: { blob: { - filename: "hello.txt", byte_size: 6, checksum: checksum, content_type: "text/plain" } } + filename: "hello.txt", byte_size: 6, checksum: checksum, content_type: "text/plain", metadata: metadata } } @response.parsed_body.tap do |details| assert_equal ActiveStorage::Blob.find(details["id"]), ActiveStorage::Blob.find_signed!(details["signed_id"]) assert_equal "hello.txt", details["filename"] assert_equal 6, details["byte_size"] assert_equal checksum, details["checksum"] + assert_equal metadata, details["metadata"].transform_keys(&:to_sym) assert_equal "text/plain", details["content_type"] assert_match %r{storage\.googleapis\.com/#{@config[:bucket]}}, details["direct_upload"]["url"] assert_equal({ "Content-MD5" => checksum, "Content-Disposition" => "inline; filename=\"hello.txt\"; filename*=UTF-8''hello.txt" }, details["direct_upload"]["headers"]) @@ -85,15 +101,23 @@ if SERVICE_CONFIGURATIONS[:azure] test "creating new direct upload" do checksum = Digest::MD5.base64digest("Hello") + metadata = { + "foo": "bar", + "my_key_1": "my_value_1", + "my_key_2": "my_value_2", + "platform": "my_platform", + "library_ID": "12345" + } post rails_direct_uploads_url, params: { blob: { - filename: "hello.txt", byte_size: 6, checksum: checksum, content_type: "text/plain" } } + filename: "hello.txt", byte_size: 6, checksum: checksum, content_type: "text/plain", metadata: metadata } } @response.parsed_body.tap do |details| assert_equal ActiveStorage::Blob.find(details["id"]), ActiveStorage::Blob.find_signed!(details["signed_id"]) assert_equal "hello.txt", details["filename"] assert_equal 6, details["byte_size"] assert_equal checksum, details["checksum"] + assert_equal metadata, details["metadata"].transform_keys(&:to_sym) assert_equal "text/plain", details["content_type"] assert_match %r{#{@config[:storage_account_name]}\.blob\.core\.windows\.net/#{@config[:container]}}, details["direct_upload"]["url"] assert_equal({ "Content-Type" => "text/plain", "Content-MD5" => checksum, "x-ms-blob-content-disposition" => "inline; filename=\"hello.txt\"; filename*=UTF-8''hello.txt", "x-ms-blob-type" => "BlockBlob" }, details["direct_upload"]["headers"]) @@ -107,15 +131,23 @@ end class ActiveStorage::DiskDirectUploadsControllerTest < ActionDispatch::IntegrationTest test "creating new direct upload" do checksum = Digest::MD5.base64digest("Hello") + metadata = { + "foo": "bar", + "my_key_1": "my_value_1", + "my_key_2": "my_value_2", + "platform": "my_platform", + "library_ID": "12345" + } post rails_direct_uploads_url, params: { blob: { - filename: "hello.txt", byte_size: 6, checksum: checksum, content_type: "text/plain" } } + filename: "hello.txt", byte_size: 6, checksum: checksum, content_type: "text/plain", metadata: metadata } } @response.parsed_body.tap do |details| assert_equal ActiveStorage::Blob.find(details["id"]), ActiveStorage::Blob.find_signed!(details["signed_id"]) assert_equal "hello.txt", details["filename"] assert_equal 6, details["byte_size"] assert_equal checksum, details["checksum"] + assert_equal metadata, details["metadata"].transform_keys(&:to_sym) assert_equal "text/plain", details["content_type"] assert_match(/rails\/active_storage\/disk/, details["direct_upload"]["url"]) assert_equal({ "Content-Type" => "text/plain" }, details["direct_upload"]["headers"]) @@ -124,10 +156,17 @@ class ActiveStorage::DiskDirectUploadsControllerTest < ActionDispatch::Integrati test "creating new direct upload does not include root in json" do checksum = Digest::MD5.base64digest("Hello") + metadata = { + "foo": "bar", + "my_key_1": "my_value_1", + "my_key_2": "my_value_2", + "platform": "my_platform", + "library_ID": "12345" + } set_include_root_in_json(true) do post rails_direct_uploads_url, params: { blob: { - filename: "hello.txt", byte_size: 6, checksum: checksum, content_type: "text/plain" } } + filename: "hello.txt", byte_size: 6, checksum: checksum, content_type: "text/plain", metadata: metadata } } end @response.parsed_body.tap do |details|