A platform for community discussion. Free, open, simple.
FIX: improve support for subfolder S3 CDN
Sam5d96809abd79
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rbindex b26b81c939f..6ad17dfbce5 100644--- a/app/helpers/application_helper.rb+++ b/app/helpers/application_helper.rb@@ -65,6 +65,12 @@ module ApplicationHelperif GlobalSetting.cdn_urlpath = path.gsub(GlobalSetting.cdn_url, GlobalSetting.s3_cdn_url)else+ # we must remove the subfolder path here, assets are uploaded to s3+ # without it getting involved+ if ActionController::Base.config.relative_url_root+ path = path.sub(ActionController::Base.config.relative_url_root, "")+ end+path = "#{GlobalSetting.s3_cdn_url}#{path}"enddiff --git a/lib/file_store/s3_store.rb b/lib/file_store/s3_store.rbindex b2f9c20988b..076ea2171ab 100644--- a/lib/file_store/s3_store.rb+++ b/lib/file_store/s3_store.rb@@ -91,7 +91,7 @@ module FileStorereturn url if SiteSetting.Upload.s3_cdn_url.blank?schema = url[/^(https?:)?\/\//, 1]folder = @s3_helper.s3_bucket_folder_path.nil? ? "" : "#{@s3_helper.s3_bucket_folder_path}/"- url.sub("#{schema}#{absolute_base_url}/#{folder}", "#{SiteSetting.Upload.s3_cdn_url}#{Discourse.base_uri}/")+ url.sub("#{schema}#{absolute_base_url}/#{folder}", "#{SiteSetting.Upload.s3_cdn_url}/")enddef cache_avatar(avatar, user_id)diff --git a/spec/components/file_store/s3_store_spec.rb b/spec/components/file_store/s3_store_spec.rbindex 99e46fe4ccc..49e72cf954b 100644--- a/spec/components/file_store/s3_store_spec.rb+++ b/spec/components/file_store/s3_store_spec.rb@@ -285,17 +285,18 @@ describe FileStore::S3Store dodescribe '.cdn_url' do- it 'uses the correct path' do- url = "//s3-upload-bucket.s3-us-west-2.amazonaws.com/livechat/original/1X/2252ae522257fc537351e47bbdd34698936b6c38.jpeg"- expect(store.cdn_url(url)).to end_with('/livechat/original/1X/2252ae522257fc537351e47bbdd34698936b6c38.jpeg')- end-it 'supports subfolder' do- SiteSetting.s3_upload_bucket = 's3-upload-bucket/livechat/community'- GlobalSetting.stubs(:relative_url_root).returns('/community')+ SiteSetting.s3_upload_bucket = 's3-upload-bucket/livechat'+ SiteSetting.s3_cdn_url = 'https://rainbow.com'++ # none of this should matter at all+ # subfolder should not leak into uploads+ global_setting :relative_url_root, '/community'Discourse.stubs(:base_uri).returns("/community")- url = "//s3-upload-bucket.s3-us-west-2.amazonaws.com/livechat/community/original/1X/2252ae522257fc537351e47bbdd34698936b6c38.jpeg"- expect(store.cdn_url(url)).to end_with('/livechat/community/original/1X/2252ae522257fc537351e47bbdd34698936b6c38.jpeg')++ url = "//s3-upload-bucket.s3.amazonaws.com/livechat/original/gif.png"++ expect(store.cdn_url(url)).to eq("https://rainbow.com/original/gif.png")endenddiff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rbindex 69a50d2ca52..06553a4fde4 100644--- a/spec/helpers/application_helper_spec.rb+++ b/spec/helpers/application_helper_spec.rb@@ -23,6 +23,15 @@ describe ApplicationHelper doset_env "COMPRESS_BROTLI", "1"end+ after do+ ActionController::Base.config.relative_url_root = nil+ end++ it "deals correctly with subfolder" do+ ActionController::Base.config.relative_url_root = "/community"+ expect(helper.preload_script("application")).to include('https://s3cdn.com/assets/application.js')+ end+it "returns magic brotli mangling for brotli requests" dohelper.request.env["HTTP_ACCEPT_ENCODING"] = 'br'diff --git a/test/javascripts/lib/discourse-test.js.es6 b/test/javascripts/lib/discourse-test.js.es6index 9627eafdbb9..94498a16462 100644--- a/test/javascripts/lib/discourse-test.js.es6+++ b/test/javascripts/lib/discourse-test.js.es6@@ -9,3 +9,18 @@ QUnit.test("getURL on subfolder install", assert => {"relative url has subfolder");});++QUnit.test("getURLWithCDN on subfolder install with S3", assert => {+ Discourse.BaseUri = "/forum";++ Discourse.S3CDN = "https://awesome.cdn/site";+ Discourse.S3BaseUrl = "//test.s3-us-west-1.amazonaws.com/site";++ let url = "//test.s3-us-west-1.amazonaws.com/site/forum/awesome.png";+ let expected = "https://awesome.cdn/site/forum/awesome.png";++ assert.equal(Discourse.getURLWithCDN(url), expected, "at correct path");++ Discourse.S3CDN = null;+ Discourse.S3BaseUrl = null;+});