diff --git a/app/controllers/asset_manager_redirect_controller.rb b/app/controllers/asset_manager_redirect_controller.rb new file mode 100644 index 000000000..7bba68370 --- /dev/null +++ b/app/controllers/asset_manager_redirect_controller.rb @@ -0,0 +1,10 @@ +class AssetManagerRedirectController < ApplicationController + def show + asset_url = Plek.new.public_asset_host + if request.host.start_with?("draft-") + asset_url = Plek.new.external_url_for("draft-assets") + end + + redirect_to host: URI.parse(asset_url).host, status: :moved_permanently + end +end diff --git a/config/routes.rb b/config/routes.rb index 0eca6af76..221a1999f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,6 +11,8 @@ get "healthcheck", to: proc { [200, {}, [""]] } + get "/government/uploads/*path" => "asset_manager_redirect#show", format: true + get "*path/:variant" => "content_items#show", constraints: { variant: /print/, diff --git a/test/controllers/asset_manager_redirect_controller_test.rb b/test/controllers/asset_manager_redirect_controller_test.rb new file mode 100644 index 000000000..61a4c15f3 --- /dev/null +++ b/test/controllers/asset_manager_redirect_controller_test.rb @@ -0,0 +1,22 @@ +require "test_helper" + +class AssetManagerRedirectControllerTest < ActionController::TestCase + setup do + Plek.any_instance.stubs(:public_asset_host).returns("http://asset-host.com") + Plek.any_instance.stubs(:external_url_for).returns("http://draft-asset-host.com") + end + + test "redirects asset requests made via public host to the public asset host" do + request.host = "some-host.com" + get :show, params: { path: "asset", format: "txt" } + + assert_redirected_to "http://asset-host.com/government/uploads/asset.txt" + end + + test "redirects asset requests made via draft host to the draft asset host" do + request.host = "draft-some-host.com" + get :show, params: { path: "asset", format: "txt" } + + assert_redirected_to "http://draft-asset-host.com/government/uploads/asset.txt" + end +end