DEV: `InlineUploads` should process CDN upload URLs as well.

approved
#1

DEV: InlineUploads should process CDN upload URLs as well.

diff --git a/app/services/inline_uploads.rb b/app/services/inline_uploads.rb
index 772fe9c..d7f36a0 100644
--- a/app/services/inline_uploads.rb
+++ b/app/services/inline_uploads.rb
@@ -128,7 +128,11 @@ class InlineUploads
         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)
@@ -242,6 +246,7 @@ class InlineUploads
     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\.]*)/,
@@ -260,10 +265,12 @@ class InlineUploads
         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
diff --git a/spec/services/inline_uploads_spec.rb b/spec/services/inline_uploads_spec.rb
index a89d8a2..fc1ca63 100644
--- a/spec/services/inline_uploads_spec.rb
+++ b/spec/services/inline_uploads_spec.rb
@@ -3,12 +3,7 @@ require 'rails_helper'
 
 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
@@ -214,6 +209,8 @@ RSpec.describe InlineUploads do
         #{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)
@@ -222,6 +219,8 @@ RSpec.describe InlineUploads do
         #{Discourse.base_url}#{upload.short_path} #{Discourse.base_url}#{upload2.short_path}
 
         ![](#{upload3.short_url})
+
+        ![](#{upload3.short_url})
         MD
       end

GitHub sha: befb074c

1 Like
Approved #2