mirror of
https://github.com/discourse/discourse.git
synced 2025-06-04 11:11:13 +08:00
FIX: improve support for subfolder S3 CDN
This commit is contained in:
@ -65,6 +65,12 @@ module ApplicationHelper
|
|||||||
if GlobalSetting.cdn_url
|
if GlobalSetting.cdn_url
|
||||||
path = path.gsub(GlobalSetting.cdn_url, GlobalSetting.s3_cdn_url)
|
path = path.gsub(GlobalSetting.cdn_url, GlobalSetting.s3_cdn_url)
|
||||||
else
|
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}"
|
path = "#{GlobalSetting.s3_cdn_url}#{path}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -91,7 +91,7 @@ module FileStore
|
|||||||
return url if SiteSetting.Upload.s3_cdn_url.blank?
|
return url if SiteSetting.Upload.s3_cdn_url.blank?
|
||||||
schema = url[/^(https?:)?\/\//, 1]
|
schema = url[/^(https?:)?\/\//, 1]
|
||||||
folder = @s3_helper.s3_bucket_folder_path.nil? ? "" : "#{@s3_helper.s3_bucket_folder_path}/"
|
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
|
end
|
||||||
|
|
||||||
def cache_avatar(avatar, user_id)
|
def cache_avatar(avatar, user_id)
|
||||||
|
@ -285,17 +285,18 @@ describe FileStore::S3Store do
|
|||||||
|
|
||||||
describe '.cdn_url' 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
|
it 'supports subfolder' do
|
||||||
SiteSetting.s3_upload_bucket = 's3-upload-bucket/livechat/community'
|
SiteSetting.s3_upload_bucket = 's3-upload-bucket/livechat'
|
||||||
GlobalSetting.stubs(:relative_url_root).returns('/community')
|
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")
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -23,6 +23,15 @@ describe ApplicationHelper do
|
|||||||
set_env "COMPRESS_BROTLI", "1"
|
set_env "COMPRESS_BROTLI", "1"
|
||||||
end
|
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
|
it "returns magic brotli mangling for brotli requests" do
|
||||||
|
|
||||||
helper.request.env["HTTP_ACCEPT_ENCODING"] = 'br'
|
helper.request.env["HTTP_ACCEPT_ENCODING"] = 'br'
|
||||||
|
@ -9,3 +9,18 @@ QUnit.test("getURL on subfolder install", assert => {
|
|||||||
"relative url has subfolder"
|
"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;
|
||||||
|
});
|
||||||
|
Reference in New Issue
Block a user