DEV: Extract common regexps for multisite.

This commit is contained in:
Guo Xiang Tan 2019-07-29 18:57:56 +08:00
parent d4acd35466
commit faea594436
3 changed files with 12 additions and 10 deletions

View File

@ -896,16 +896,17 @@ class Post < ActiveRecord::Base
fragments ||= Nokogiri::HTML::fragment(self.cooked)
links = fragments.css("a/@href", "img/@src").map { |media| media.value }.uniq
local_store = FileStore::LocalStore.new
links.each do |src|
next if src.blank? || upload_patterns.none? { |pattern| src.split("?")[0] =~ pattern }
next if Rails.configuration.multisite && src.exclude?(current_db) && src.exclude?("short-url")
src = "#{SiteSetting.force_https ? "https" : "http"}:#{src}" if src.start_with?("//")
next unless Discourse.store.has_been_uploaded?(src) || (include_local_upload && src =~ /\A\/[^\/]/i)
next unless Discourse.store.has_been_uploaded?(src) || (include_local_upload && (src =~ /\A\/[^\/]/i || local_store.has_been_uploaded?(src)))
path = begin
URI(URI.unescape(src))&.path
URI(URI.unescape(src.gsub(GlobalSetting.cdn_url, "")))&.path
rescue URI::Error
end

View File

@ -272,13 +272,17 @@ class InlineUploads
cdn_url = GlobalSetting.cdn_url.sub(/https?:\/\//, "(https?://)")
end
db = RailsMultisite::ConnectionManagement.current_db
regexps = [
/(upload:\/\/([a-zA-Z0-9]+)[a-zA-Z0-9\.]*)/,
/(\/uploads\/short-url\/([a-zA-Z0-9]+)[a-zA-Z0-9\.]*)/,
/(#{base_url}\/uploads\/short-url\/([a-zA-Z0-9]+)[a-zA-Z0-9\.]*)/,
/(\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/,
/(#{base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/,
]
db = RailsMultisite::ConnectionManagement.current_db
regexps << /(#{cdn_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/ if cdn_url
if Discourse.store.external?
if Rails.configuration.multisite
@ -287,14 +291,7 @@ class InlineUploads
else
regexps << /((https?:)?#{SiteSetting.Upload.s3_base_url}#{UPLOAD_REGEXP_PATTERN})/
regexps << /(#{SiteSetting.Upload.s3_cdn_url}#{UPLOAD_REGEXP_PATTERN})/
regexps << /(\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
regexps << /(#{base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
regexps << /(#{cdn_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/ if cdn_url
end
else
regexps << /(\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
regexps << /(#{base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
regexps << /(#{cdn_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/ if cdn_url
end
node = node.to_s

View File

@ -587,8 +587,10 @@ RSpec.describe InlineUploads do
describe "s3 uploads" do
let(:upload) { Fabricate(:upload_s3) }
let(:upload2) { Fabricate(:upload_s3) }
let(:upload3) { Fabricate(:upload) }
before do
upload3
SiteSetting.enable_s3_uploads = true
SiteSetting.s3_upload_bucket = "s3-upload-bucket"
SiteSetting.s3_access_key_id = "some key"
@ -636,6 +638,7 @@ RSpec.describe InlineUploads do
<img src="#{upload.url}" alt="some image">
<img src="#{URI.join(SiteSetting.s3_cdn_url, URI.parse(upload2.url).path).to_s}" alt="some image">
<img src="#{upload3.url}">
MD
expect(InlineUploads.process(md)).to eq(<<~MD)
@ -644,6 +647,7 @@ RSpec.describe InlineUploads do
![some image](#{upload.short_url})
![some image](#{upload2.short_url})
![](#{upload3.short_url})
MD
ensure
Rails.configuration.multisite = false