FIX: `Upload.get_from_url` not respective subfolder in s3 bucket names.

This commit is contained in:
Guo Xiang Tan 2018-07-06 11:36:29 +08:00
parent f662d1135e
commit 7163bf9323
2 changed files with 33 additions and 13 deletions

View File

@ -81,8 +81,16 @@ class Upload < ActiveRecord::Base
url = url.sub(Discourse.asset_host, "") if Discourse.asset_host.present? && Discourse.asset_host != SiteSetting.Upload.s3_cdn_url url = url.sub(Discourse.asset_host, "") if Discourse.asset_host.present? && Discourse.asset_host != SiteSetting.Upload.s3_cdn_url
# when using s3 without CDN # when using s3 without CDN
url = url.sub(/^https?\:/, "") if url.include?(Discourse.store.absolute_base_url) && Discourse.store.external? url = url.sub(/^https?\:/, "") if url.include?(Discourse.store.absolute_base_url) && Discourse.store.external?
# when using s3, we need to replace with the absolute base url # when using s3, we need to replace with the absolute base url
url = url.sub(SiteSetting.Upload.s3_cdn_url, Discourse.store.absolute_base_url) if SiteSetting.Upload.s3_cdn_url.present? if SiteSetting.Upload.s3_cdn_url.present?
path = Discourse.store.s3_bucket.split("/", 2)[1]
url = url.sub(
SiteSetting.Upload.s3_cdn_url,
"#{Discourse.store.absolute_base_url}#{path ? '/' + path : ''}"
)
end
# always try to get the path # always try to get the path
uri = begin uri = begin

View File

@ -93,7 +93,7 @@ describe Upload do
describe "s3 store" do describe "s3 store" do
let(:path) { "/original/3X/1/0/10f73034616a796dfd70177dc54b6def44c4ba6f.png" } let(:path) { "/original/3X/1/0/10f73034616a796dfd70177dc54b6def44c4ba6f.png" }
let(:url) { "//#{SiteSetting.s3_upload_bucket}.s3.amazonaws.com#{path}" } let(:url) { "#{SiteSetting.Upload.absolute_base_url}#{path}" }
before do before do
SiteSetting.enable_s3_uploads = true SiteSetting.enable_s3_uploads = true
@ -102,25 +102,37 @@ describe Upload do
SiteSetting.s3_secret_access_key = "some secret key" SiteSetting.s3_secret_access_key = "some secret key"
end end
after do
SiteSetting.enable_s3_uploads = false
end
it "should return the right upload when using base url (not CDN) for s3" do it "should return the right upload when using base url (not CDN) for s3" do
upload upload
url = "https://#{SiteSetting.s3_upload_bucket}.s3.amazonaws.com#{path}"
expect(Upload.get_from_url(url)).to eq(upload) expect(Upload.get_from_url(url)).to eq(upload)
end end
it "should return the right upload when using a CDN for s3" do describe 'when using a cdn' do
upload let(:s3_cdn_url) { 'https://mycdn.slowly.net' }
s3_cdn_url = 'https://mycdn.slowly.net'
SiteSetting.s3_cdn_url = s3_cdn_url
before do
SiteSetting.s3_cdn_url = s3_cdn_url
end
it "should return the right upload" do
upload
expect(Upload.get_from_url(URI.join(s3_cdn_url, path).to_s)).to eq(upload) expect(Upload.get_from_url(URI.join(s3_cdn_url, path).to_s)).to eq(upload)
end end
describe 'when upload bucket contains subfolder' do
let(:url) { "#{SiteSetting.Upload.absolute_base_url}/path/path2#{path}" }
before do
SiteSetting.s3_upload_bucket = "s3-upload-bucket/path/path2"
end
it "should return the right upload" do
upload
expect(Upload.get_from_url(URI.join(s3_cdn_url, path).to_s)).to eq(upload)
end
end
end
it "should return the right upload when using one CDN for both s3 and assets" do it "should return the right upload when using one CDN for both s3 and assets" do
begin begin
original_asset_host = Rails.configuration.action_controller.asset_host original_asset_host = Rails.configuration.action_controller.asset_host