mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Merge branch 's3_refactor'
This commit is contained in:
@@ -46,7 +46,7 @@ class Backup
|
||||
|
||||
def s3
|
||||
require "s3_helper" unless defined? S3Helper
|
||||
@s3_helper ||= S3Helper.new(s3_bucket)
|
||||
@s3_helper ||= S3Helper.new(s3_bucket, '', S3Helper.s3_options(SiteSetting))
|
||||
end
|
||||
|
||||
def upload_to_s3
|
||||
|
||||
@@ -75,6 +75,21 @@ class GlobalSetting
|
||||
end
|
||||
end
|
||||
|
||||
def self.use_s3?
|
||||
(@use_s3 ||=
|
||||
begin
|
||||
s3_bucket &&
|
||||
s3_region && (
|
||||
s3_use_iam_profile || (s3_access_key_id && s3_secret_access_key)
|
||||
) ? :true : :false
|
||||
end) == :true
|
||||
end
|
||||
|
||||
# for testing
|
||||
def self.reset_s3_cache!
|
||||
@use_s3 = nil
|
||||
end
|
||||
|
||||
def self.database_config
|
||||
hash = { "adapter" => "postgresql" }
|
||||
%w{pool timeout socket host port username password replica_host replica_port}.each do |s|
|
||||
|
||||
@@ -118,6 +118,43 @@ class SiteSetting < ActiveRecord::Base
|
||||
def self.attachment_filename_blacklist_regex
|
||||
@attachment_filename_blacklist_regex ||= Regexp.union(SiteSetting.attachment_filename_blacklist.split("|"))
|
||||
end
|
||||
|
||||
# helpers for getting s3 settings that fallback to global
|
||||
class Upload
|
||||
def self.s3_cdn_url
|
||||
SiteSetting.enable_s3_uploads ? SiteSetting.s3_cdn_url : GlobalSetting.s3_cdn_url
|
||||
end
|
||||
|
||||
def self.s3_region
|
||||
SiteSetting.enable_s3_uploads ? SiteSetting.s3_region : GlobalSetting.s3_region
|
||||
end
|
||||
|
||||
def self.s3_upload_bucket
|
||||
SiteSetting.enable_s3_uploads ? SiteSetting.s3_upload_bucket : GlobalSetting.s3_bucket
|
||||
end
|
||||
|
||||
def self.enable_s3_uploads
|
||||
SiteSetting.enable_s3_uploads || GlobalSetting.use_s3?
|
||||
end
|
||||
|
||||
def self.absolute_base_url
|
||||
bucket = SiteSetting.enable_s3_uploads ? Discourse.store.s3_bucket_name : GlobalSetting.s3_bucket
|
||||
|
||||
# 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
|
||||
end
|
||||
end
|
||||
|
||||
def self.Upload
|
||||
SiteSetting::Upload
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
|
||||
@@ -44,8 +44,8 @@ class TopicLinkClick < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
if SiteSetting.s3_cdn_url.present?
|
||||
cdn_uri = URI.parse(SiteSetting.s3_cdn_url) rescue nil
|
||||
if SiteSetting.Upload.s3_cdn_url.present?
|
||||
cdn_uri = URI.parse(SiteSetting.Upload.s3_cdn_url) rescue nil
|
||||
if cdn_uri && cdn_uri.hostname == uri.hostname && uri.path.starts_with?(cdn_uri.path)
|
||||
is_cdn_link = true
|
||||
path = uri.path[cdn_uri.path.length..-1]
|
||||
|
||||
@@ -79,7 +79,7 @@ class Upload < ActiveRecord::Base
|
||||
# we store relative urls, so we need to remove any host/cdn
|
||||
url = url.sub(Discourse.asset_host, "") if Discourse.asset_host.present?
|
||||
# when using s3, we need to replace with the absolute base url
|
||||
url = url.sub(SiteSetting.s3_cdn_url, Discourse.store.absolute_base_url) if SiteSetting.s3_cdn_url.present?
|
||||
url = url.sub(SiteSetting.Upload.s3_cdn_url, Discourse.store.absolute_base_url) if SiteSetting.Upload.s3_cdn_url.present?
|
||||
|
||||
# always try to get the path
|
||||
uri = URI(url) rescue nil
|
||||
|
||||
Reference in New Issue
Block a user