From 2365efbdaaca8b8444c12ac8c61903066e15da15 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Thu, 14 Feb 2019 13:17:05 +0000 Subject: [PATCH] file upload: proxy direct upload through the APP_HOST Ref #3409 --- app/lib/active_storage/service/ds_proxy_service.rb | 2 +- spec/lib/active_storage/service/ds_proxy_service.rb | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/lib/active_storage/service/ds_proxy_service.rb b/app/lib/active_storage/service/ds_proxy_service.rb index 78c420693..e81fdfacb 100644 --- a/app/lib/active_storage/service/ds_proxy_service.rb +++ b/app/lib/active_storage/service/ds_proxy_service.rb @@ -27,7 +27,7 @@ module ActiveStorage def publicize(url) search = %r{^https://[^/]+/v1/AUTH_[a-f0-9]{32}} - replace = 'https://static.demarches-simplifiees.fr' + replace = "https://#{ENV['APP_HOST']}/direct-upload" url.gsub(search, replace) end end diff --git a/spec/lib/active_storage/service/ds_proxy_service.rb b/spec/lib/active_storage/service/ds_proxy_service.rb index 90814b21b..87889d76f 100644 --- a/spec/lib/active_storage/service/ds_proxy_service.rb +++ b/spec/lib/active_storage/service/ds_proxy_service.rb @@ -1,17 +1,20 @@ describe ActiveStorage::Service::DsProxyService do let(:private_host) { 'storage.sbg1.cloud.ovh.net:443' } - let(:public_host) { 'static.demarches-simplifiees.fr' } + let(:public_host) { 'www.demarches-simplifiees.fr' } let(:auth) { 'AUTH_a24c37ed11a84896914514384898c34b' } let(:bucket) { 'test_local' } let(:key) { '2R6rr89nFeSRkSgXHd3smvEf' } let(:temp_url_params) { 'temp_url_sig=5ab8cfc3ba5da2598a6c88cc6b1b461fe4e115bc&temp_url_expires=1547598179' } - let(:storage_service) { storage_service = double(ActiveStorage::Service) } - subject { ActiveStorage::Service::DsProxyService.new(wrapped: storage_service) } + + subject do + allow(ENV).to receive(:[]).with('APP_HOST').and_return(public_host) + ActiveStorage::Service::DsProxyService.new(wrapped: storage_service) + end describe '#url' do let(:private_url) { "https://#{private_host}/v1/#{auth}/#{bucket}/#{key}?#{temp_url_params}" } - let(:public_url) { "https://#{public_host}/#{bucket}/#{key}?#{temp_url_params}" } + let(:public_url) { "https://#{public_host}/direct-upload/#{bucket}/#{key}?#{temp_url_params}" } before do expect(storage_service).to receive(:url).and_return(private_url) @@ -25,7 +28,7 @@ describe ActiveStorage::Service::DsProxyService do describe '#url_for_direct_upload' do let(:download_params) { 'inline&filename=documents_top_confidentiels.bmp' } let(:private_url) { "https://#{private_host}/v1/#{auth}/#{bucket}/#{key}?#{temp_url_params}&#{download_params}" } - let(:public_url) { "https://#{public_host}/#{bucket}/#{key}?#{temp_url_params}&#{download_params}" } + let(:public_url) { "https://#{public_host}/direct-upload/#{bucket}/#{key}?#{temp_url_params}&#{download_params}" } before do expect(storage_service).to receive(:url_for_direct_upload).and_return(private_url)