fix s3 subfolders harder
specifically, include the folder in absolute_base_url
This commit is contained in:
parent
cbfa7f0112
commit
52e9f49ec1
|
@ -89,6 +89,10 @@ class GlobalSetting
|
|||
@s3_bucket_name ||= s3_bucket.downcase.split("/")[0]
|
||||
end
|
||||
|
||||
def self.s3_bucket_folder_path
|
||||
@s3_bucket_folder_path ||= s3_bucket.downcase.split("/")[1]
|
||||
end
|
||||
|
||||
# for testing
|
||||
def self.reset_s3_cache!
|
||||
@use_s3 = nil
|
||||
|
|
|
@ -139,15 +139,10 @@ class SiteSetting < ActiveRecord::Base
|
|||
|
||||
def self.absolute_base_url
|
||||
bucket = SiteSetting.enable_s3_uploads ? Discourse.store.s3_bucket_name : GlobalSetting.s3_bucket_name
|
||||
domain = SiteSetting.Upload.s3_region =~ /^cn-/ ? "amazonaws.com.cn" : "amazonaws.com"
|
||||
path = SiteSetting.enable_s3_uploads ? Discourse.store.s3_bucket_folder_path : GlobalSetting.s3_bucket_folder_path
|
||||
|
||||
# cf. http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
|
||||
if SiteSetting.Upload.s3_region == "us-east-1"
|
||||
"//#{bucket}.s3.amazonaws.com"
|
||||
elsif SiteSetting.Upload.s3_region == 'cn-north-1'
|
||||
"//#{bucket}.s3.cn-north-1.amazonaws.com.cn"
|
||||
else
|
||||
"//#{bucket}.s3-#{SiteSetting.Upload.s3_region}.amazonaws.com"
|
||||
end
|
||||
"//#{bucket}.s3.#{SiteSetting.Upload.s3_region}.#{domain}#{path ? '/' + path : ''}"
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ module FileStore
|
|||
# if this fails, it will throw an exception
|
||||
path = @s3_helper.upload(file, path, options)
|
||||
# return the upload url
|
||||
"#{absolute_base_url}/#{path}"
|
||||
"//#{URI.parse(absolute_base_url).hostname}/#{path}"
|
||||
end
|
||||
|
||||
def remove_file(url, path)
|
||||
|
@ -65,6 +65,10 @@ module FileStore
|
|||
@s3_helper.s3_bucket_name
|
||||
end
|
||||
|
||||
def s3_bucket_folder_path
|
||||
@s3_helper.s3_bucket_folder_path
|
||||
end
|
||||
|
||||
def absolute_base_url
|
||||
@absolute_base_url ||= SiteSetting.Upload.absolute_base_url
|
||||
end
|
||||
|
@ -85,8 +89,7 @@ module FileStore
|
|||
def cdn_url(url)
|
||||
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}/")
|
||||
url.sub("#{schema}#{absolute_base_url}", SiteSetting.Upload.s3_cdn_url)
|
||||
end
|
||||
|
||||
def cache_avatar(avatar, user_id)
|
||||
|
|
|
@ -48,7 +48,7 @@ describe FileStore::S3Store do
|
|||
s3_object.expects(:upload_file)
|
||||
|
||||
expect(store.store_upload(uploaded_file, upload)).to eq(
|
||||
"//s3-upload-bucket.s3-us-west-1.amazonaws.com/original/1X/#{upload.sha1}.png"
|
||||
"//s3-upload-bucket.s3.us-west-1.amazonaws.com/original/1X/#{upload.sha1}.png"
|
||||
)
|
||||
end
|
||||
|
||||
|
@ -66,7 +66,7 @@ describe FileStore::S3Store do
|
|||
s3_object.expects(:upload_file)
|
||||
|
||||
expect(store.store_upload(uploaded_file, upload)).to eq(
|
||||
"//s3-upload-bucket.s3-us-west-1.amazonaws.com/discourse-uploads/original/1X/#{upload.sha1}.png"
|
||||
"//s3-upload-bucket.s3.us-west-1.amazonaws.com/discourse-uploads/original/1X/#{upload.sha1}.png"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
@ -83,7 +83,7 @@ describe FileStore::S3Store do
|
|||
s3_object.expects(:upload_file)
|
||||
|
||||
expect(store.store_optimized_image(optimized_image_file, optimized_image)).to eq(
|
||||
"//s3-upload-bucket.s3-us-west-1.amazonaws.com/#{path}"
|
||||
"//s3-upload-bucket.s3.us-west-1.amazonaws.com/#{path}"
|
||||
)
|
||||
end
|
||||
|
||||
|
@ -102,7 +102,7 @@ describe FileStore::S3Store do
|
|||
s3_object.expects(:upload_file)
|
||||
|
||||
expect(store.store_optimized_image(optimized_image_file, optimized_image)).to eq(
|
||||
"//s3-upload-bucket.s3-us-west-1.amazonaws.com/#{path}"
|
||||
"//s3-upload-bucket.s3.us-west-1.amazonaws.com/#{path}"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
@ -116,7 +116,7 @@ describe FileStore::S3Store do
|
|||
it "removes the file from s3 with the right paths" do
|
||||
store.expects(:get_depth_for).with(upload.id).returns(0)
|
||||
s3_helper.expects(:s3_bucket).returns(s3_bucket)
|
||||
upload.update_attributes!(url: "//s3-upload-bucket.s3-us-west-1.amazonaws.com/original/1X/#{upload.sha1}.png")
|
||||
upload.update_attributes!(url: "//s3-upload-bucket.s3.us-west-1.amazonaws.com/original/1X/#{upload.sha1}.png")
|
||||
s3_object = stub
|
||||
|
||||
s3_bucket.expects(:object).with("tombstone/original/1X/#{upload.sha1}.png").returns(s3_object)
|
||||
|
@ -135,7 +135,7 @@ describe FileStore::S3Store do
|
|||
it "removes the file from s3 with the right paths" do
|
||||
store.expects(:get_depth_for).with(upload.id).returns(0)
|
||||
s3_helper.expects(:s3_bucket).returns(s3_bucket)
|
||||
upload.update_attributes!(url: "//s3-upload-bucket.s3-us-west-1.amazonaws.com/discourse-uploads/original/1X/#{upload.sha1}.png")
|
||||
upload.update_attributes!(url: "//s3-upload-bucket.s3.us-west-1.amazonaws.com/discourse-uploads/original/1X/#{upload.sha1}.png")
|
||||
s3_object = stub
|
||||
|
||||
s3_bucket.expects(:object).with("discourse-uploads/tombstone/original/1X/#{upload.sha1}.png").returns(s3_object)
|
||||
|
@ -151,7 +151,7 @@ describe FileStore::S3Store do
|
|||
describe "#remove_optimized_image" do
|
||||
let(:optimized_image) do
|
||||
Fabricate(:optimized_image,
|
||||
url: "//s3-upload-bucket.s3-us-west-1.amazonaws.com/optimized/1X/#{upload.sha1}_1_100x200.png",
|
||||
url: "//s3-upload-bucket.s3.us-west-1.amazonaws.com/optimized/1X/#{upload.sha1}_1_100x200.png",
|
||||
upload: upload
|
||||
)
|
||||
end
|
||||
|
@ -193,28 +193,25 @@ describe FileStore::S3Store do
|
|||
describe ".has_been_uploaded?" do
|
||||
|
||||
it "identifies S3 uploads" do
|
||||
expect(store.has_been_uploaded?("//s3-upload-bucket.s3.amazonaws.com/1337.png")).to eq(true)
|
||||
expect(store.has_been_uploaded?("//s3-upload-bucket.s3.us-east-1.amazonaws.com/1337.png")).to eq(true)
|
||||
end
|
||||
|
||||
it "does not match other s3 urls" do
|
||||
expect(store.has_been_uploaded?("//s3-upload-bucket.s3-us-east-1.amazonaws.com/1337.png")).to eq(false)
|
||||
expect(store.has_been_uploaded?("//s3.amazonaws.com/s3-upload-bucket/1337.png")).to eq(false)
|
||||
expect(store.has_been_uploaded?("//s4_upload_bucket.s3.amazonaws.com/1337.png")).to eq(false)
|
||||
expect(store.has_been_uploaded?("//s4_upload_bucket.s3.us-east-1.amazonaws.com/1337.png")).to eq(false)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe ".absolute_base_url" do
|
||||
it "returns a lowercase schemaless absolute url" do
|
||||
expect(store.absolute_base_url).to eq("//s3-upload-bucket.s3.amazonaws.com")
|
||||
expect(store.absolute_base_url).to eq("//s3-upload-bucket.s3.us-east-1.amazonaws.com")
|
||||
end
|
||||
|
||||
it "uses the proper endpoint" do
|
||||
SiteSetting.s3_region = "us-east-1"
|
||||
expect(FileStore::S3Store.new(s3_helper).absolute_base_url).to eq("//s3-upload-bucket.s3.amazonaws.com")
|
||||
|
||||
SiteSetting.s3_region = "us-west-2"
|
||||
expect(FileStore::S3Store.new(s3_helper).absolute_base_url).to eq("//s3-upload-bucket.s3-us-west-2.amazonaws.com")
|
||||
expect(FileStore::S3Store.new(s3_helper).absolute_base_url).to eq("//s3-upload-bucket.s3.us-west-2.amazonaws.com")
|
||||
|
||||
SiteSetting.s3_region = "cn-north-1"
|
||||
expect(FileStore::S3Store.new(s3_helper).absolute_base_url).to eq("//s3-upload-bucket.s3.cn-north-1.amazonaws.com.cn")
|
||||
|
|
Loading…
Reference in New Issue