Skip to content

Commit

Permalink
DEV: InlineUploads should process CDN upload URLs as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
tgxworld committed Jun 14, 2019
1 parent a01488a commit befb074
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
9 changes: 8 additions & 1 deletion app/services/inline_uploads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ def self.process(markdown, on_missing: nil)
end

if !Discourse.store.external?
next if uri&.host && uri.host != Discourse.current_hostname
host = uri&.host

if host && ![Discourse.current_hostname, URI(GlobalSetting.cdn_url).hostname].include?(host)
next
end
end

upload = Upload.get_from_url(link)
Expand Down Expand Up @@ -242,6 +246,7 @@ def self.matched_uploads(node)
matches = []

base_url = Discourse.base_url.sub(/https?:\/\//, "(https?://)")
cdn_url = GlobalSetting.cdn_url.sub(/https?:\/\//, "(https?://)")

regexps = [
/(upload:\/\/([a-zA-Z0-9]+)[a-zA-Z0-9\.]*)/,
Expand All @@ -260,10 +265,12 @@ def self.matched_uploads(node)
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})/
end
else
regexps << /(\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
regexps << /(#{base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
regexps << /(#{cdn_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
end

node = node.to_s
Expand Down
11 changes: 5 additions & 6 deletions spec/services/inline_uploads_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@

RSpec.describe InlineUploads do
before do
@original_asset_host = Rails.configuration.action_controller.asset_host
Rails.configuration.action_controller.asset_host = "https://cdn.discourse.org/stuff"
end

after do
Rails.configuration.action_controller.asset_host = @original_asset_host
set_cdn_url "https://awesome.com"
end

describe '.process' do
Expand Down Expand Up @@ -214,13 +209,17 @@
#{Discourse.base_url}#{upload.url} #{Discourse.base_url}#{upload2.url}
#{Discourse.base_url}#{upload3.url}
#{GlobalSetting.cdn_url}#{upload3.url}
MD

expect(InlineUploads.process(md)).to eq(<<~MD)
![](#{upload.short_url})
#{Discourse.base_url}#{upload.short_path} #{Discourse.base_url}#{upload2.short_path}
![](#{upload3.short_url})
![](#{upload3.short_url})
MD
end
Expand Down

0 comments on commit befb074

Please sign in to comment.