mirror of
https://github.com/codeninjasllc/discourse.git
synced 2025-04-28 15:03:58 -04:00
FIX: S3 upload when using dots in bucket name
This commit is contained in:
parent
646cca3128
commit
c6fb60e0a0
5 changed files with 40 additions and 3 deletions
app/models
lib
spec/models
|
@ -84,6 +84,10 @@ class SiteSetting < ActiveRecord::Base
|
||||||
authorized_images.count > 0 && file.original_filename =~ /\.(#{authorized_images.join("|")})$/i
|
authorized_images.count > 0 && file.original_filename =~ /\.(#{authorized_images.join("|")})$/i
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.scheme
|
||||||
|
use_ssl? ? "https" : "http"
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# == Schema Information
|
# == Schema Information
|
||||||
|
|
|
@ -98,7 +98,7 @@ class CookedPostProcessor
|
||||||
absolute_url = url
|
absolute_url = url
|
||||||
absolute_url = Discourse.base_url_no_prefix + absolute_url if absolute_url =~ /^\/[^\/]/
|
absolute_url = Discourse.base_url_no_prefix + absolute_url if absolute_url =~ /^\/[^\/]/
|
||||||
# FastImage fails when there's no scheme
|
# FastImage fails when there's no scheme
|
||||||
absolute_url = (SiteSetting.use_ssl? ? "https:" : "http:") + absolute_url if absolute_url.start_with?("//")
|
absolute_url = SiteSetting.scheme + ":" + absolute_url if absolute_url.start_with?("//")
|
||||||
return unless is_valid_image_url?(absolute_url)
|
return unless is_valid_image_url?(absolute_url)
|
||||||
# we can *always* crawl our own images
|
# we can *always* crawl our own images
|
||||||
return unless SiteSetting.crawl_images? || Discourse.store.has_been_uploaded?(url)
|
return unless SiteSetting.crawl_images? || Discourse.store.has_been_uploaded?(url)
|
||||||
|
|
|
@ -128,7 +128,7 @@ module FileStore
|
||||||
end
|
end
|
||||||
|
|
||||||
def is_local?(url)
|
def is_local?(url)
|
||||||
absolute_url = url.start_with?("//") ? (SiteSetting.use_ssl? ? "https:" : "http:") + url : url
|
absolute_url = url.start_with?("//") ? SiteSetting.scheme + ":" + url : url
|
||||||
absolute_url.start_with?(absolute_base_url) || absolute_url.start_with?(absolute_base_cdn_url)
|
absolute_url.start_with?(absolute_base_url) || absolute_url.start_with?(absolute_base_cdn_url)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ module FileStore
|
||||||
|
|
||||||
extension = File.extname(upload.original_filename)
|
extension = File.extname(upload.original_filename)
|
||||||
temp_file = Tempfile.new(["discourse-s3", extension])
|
temp_file = Tempfile.new(["discourse-s3", extension])
|
||||||
url = (SiteSetting.use_ssl? ? "https:" : "http:") + upload.url
|
url = SiteSetting.scheme + ":" + upload.url
|
||||||
|
|
||||||
File.open(temp_file.path, "wb") do |f|
|
File.open(temp_file.path, "wb") do |f|
|
||||||
f.write(open(url, "rb", read_timeout: 5).read)
|
f.write(open(url, "rb", read_timeout: 5).read)
|
||||||
|
@ -108,6 +108,9 @@ module FileStore
|
||||||
provider: 'AWS',
|
provider: 'AWS',
|
||||||
aws_access_key_id: SiteSetting.s3_access_key_id,
|
aws_access_key_id: SiteSetting.s3_access_key_id,
|
||||||
aws_secret_access_key: SiteSetting.s3_secret_access_key,
|
aws_secret_access_key: SiteSetting.s3_secret_access_key,
|
||||||
|
scheme: SiteSetting.scheme,
|
||||||
|
# cf. https://github.com/fog/fog/issues/2381
|
||||||
|
path_style: dns_compatible?(s3_bucket, SiteSetting.use_ssl?),
|
||||||
}
|
}
|
||||||
options[:region] = SiteSetting.s3_region unless SiteSetting.s3_region.empty?
|
options[:region] = SiteSetting.s3_region unless SiteSetting.s3_region.empty?
|
||||||
options
|
options
|
||||||
|
@ -164,6 +167,22 @@ module FileStore
|
||||||
"tombstone/"
|
"tombstone/"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# cf. https://github.com/aws/aws-sdk-core-ruby/blob/master/lib/aws/plugins/s3_bucket_dns.rb#L56-L78
|
||||||
|
def dns_compatible?(bucket_name, ssl)
|
||||||
|
if valid_subdomain?(bucket_name)
|
||||||
|
bucket_name.match(/\./) && ssl ? false : true
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def valid_subdomain?(bucket_name)
|
||||||
|
bucket_name.size < 64 &&
|
||||||
|
bucket_name =~ /^[a-z0-9][a-z0-9.-]+[a-z0-9]$/ &&
|
||||||
|
bucket_name !~ /(\d+\.){3}\d+/ &&
|
||||||
|
bucket_name !~ /[.-]{2}/
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -110,4 +110,18 @@ describe SiteSetting do
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "scheme" do
|
||||||
|
|
||||||
|
it "returns http when ssl is disabled" do
|
||||||
|
SiteSetting.expects(:use_ssl).returns(false)
|
||||||
|
SiteSetting.scheme.should == "http"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns https when using ssl" do
|
||||||
|
SiteSetting.expects(:use_ssl).returns(true)
|
||||||
|
SiteSetting.scheme.should == "https"
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue