FIX: improve support for subfolder S3 CDN

This commit is contained in:
Sam 2018-08-22 12:31:13 +10:00
parent f5142861e5
commit 5d96809abd
5 changed files with 41 additions and 10 deletions

View File

@ -65,6 +65,12 @@ module ApplicationHelper
if GlobalSetting.cdn_url
path = 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}"
end

View File

@ -91,7 +91,7 @@ module FileStore
return 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}/")
end
def cache_avatar(avatar, user_id)

View File

@ -285,17 +285,18 @@ describe FileStore::S3Store do
describe '.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")
end
end

View File

@ -23,6 +23,15 @@ describe ApplicationHelper do
set_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" do
helper.request.env["HTTP_ACCEPT_ENCODING"] = 'br'

View File

@ -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;
});