From 8be05519cd3214260a835af81bebf686f19c76ff Mon Sep 17 00:00:00 2001 From: "alxjrvs@gmail.com" Date: Fri, 12 Nov 2021 13:08:04 -0500 Subject: [PATCH 1/2] Update ActiveStorage Representation Proxycontroller --- .../representations/proxy_controller.rb | 2 +- .../representations/proxy_controller_test.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/activestorage/app/controllers/active_storage/representations/proxy_controller.rb b/activestorage/app/controllers/active_storage/representations/proxy_controller.rb index c903b8216c..e0cb364d77 100644 --- a/activestorage/app/controllers/active_storage/representations/proxy_controller.rb +++ b/activestorage/app/controllers/active_storage/representations/proxy_controller.rb @@ -9,7 +9,7 @@ class ActiveStorage::Representations::ProxyController < ActiveStorage::Representations::BaseController def show http_cache_forever public: true do - send_blob_stream @representation.image + send_blob_stream @representation.image, disposition: params[:disposition] end end end diff --git a/activestorage/test/controllers/representations/proxy_controller_test.rb b/activestorage/test/controllers/representations/proxy_controller_test.rb index 8ae815ea1d..67665e99db 100644 --- a/activestorage/test/controllers/representations/proxy_controller_test.rb +++ b/activestorage/test/controllers/representations/proxy_controller_test.rb @@ -8,6 +8,22 @@ class ActiveStorage::Representations::ProxyControllerWithVariantsTest < ActionDi @blob = create_file_blob filename: "racecar.jpg" end + test "showing variant attachment" do + get rails_blob_representation_proxy_url( + disposition: :attachment, + filename: @blob.filename, + signed_blob_id: @blob.signed_id, + variation_key: ActiveStorage::Variation.encode(resize_to_limit: [100, 100])) + + assert_response :ok + assert_match(/^attachment/, response.headers["Content-Disposition"]) + + image = read_image(@blob.variant(resize_to_limit: [100, 100])) + assert_equal 100, image.width + assert_equal 67, image.height + end + + test "showing variant inline" do get rails_blob_representation_proxy_url( filename: @blob.filename, From acda391b3f0c9cc6deca9ceebe4b28b1b8642a8d Mon Sep 17 00:00:00 2001 From: "alxjrvs@gmail.com" Date: Fri, 12 Nov 2021 13:08:13 -0500 Subject: [PATCH 2/2] Update ActiveStorage Blobs Proxycontroller --- .../controllers/active_storage/blobs/proxy_controller.rb | 2 +- .../test/controllers/blobs/proxy_controller_test.rb | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/activestorage/app/controllers/active_storage/blobs/proxy_controller.rb b/activestorage/app/controllers/active_storage/blobs/proxy_controller.rb index 0073517db3..e2b3488ad8 100644 --- a/activestorage/app/controllers/active_storage/blobs/proxy_controller.rb +++ b/activestorage/app/controllers/active_storage/blobs/proxy_controller.rb @@ -17,7 +17,7 @@ class ActiveStorage::Blobs::ProxyController < ActiveStorage::BaseController response.headers["Accept-Ranges"] = "bytes" response.headers["Content-Length"] = @blob.byte_size.to_s - send_blob_stream @blob + send_blob_stream @blob, disposition: params[:disposition] end end end diff --git a/activestorage/test/controllers/blobs/proxy_controller_test.rb b/activestorage/test/controllers/blobs/proxy_controller_test.rb index 83bcf3f0f8..443539aded 100644 --- a/activestorage/test/controllers/blobs/proxy_controller_test.rb +++ b/activestorage/test/controllers/blobs/proxy_controller_test.rb @@ -21,11 +21,17 @@ class ActiveStorage::Blobs::ProxyControllerTest < ActionDispatch::IntegrationTes assert_equal "application/octet-stream", response.headers["Content-Type"] end - test "forcing Content-Disposition to attachment" do + test "forcing Content-Disposition to attachment based on type" do get rails_storage_proxy_url(create_blob(content_type: "application/zip")) assert_match(/^attachment; /, response.headers["Content-Disposition"]) end + test "caller can change disposition to attachment" do + url = rails_storage_proxy_url(create_blob(content_type: "image/jpeg"), disposition: :attachment) + get url + assert_match(/^attachment; /, response.headers["Content-Disposition"]) + end + test "signed ID within expiration date" do get rails_storage_proxy_url(create_file_blob(filename: "racecar.jpg"), expires_in: 1.minute) assert_response :success