FIX: Cover more edge cases in `InlineUploads`.

approved
#1

FIX: Cover more edge cases in InlineUploads.

diff --git a/app/services/inline_uploads.rb b/app/services/inline_uploads.rb
index 45afd7b..cb153e5 100644
--- a/app/services/inline_uploads.rb
+++ b/app/services/inline_uploads.rb
@@ -22,12 +22,11 @@ class InlineUploads
       end
 
       if seen_link = matched_uploads(node).first
-        link_occurences <<
-          if (actual_link = (node.attributes["href"]&.value || node.attributes["src"]&.value))
-            { link: actual_link, is_valid: true }
-          else
-            { link: seen_link, is_valid: false }
-          end
+        if (actual_link = (node.attributes["href"]&.value || node.attributes["src"]&.value))
+          link_occurences << { link: actual_link, is_valid: true }
+        elsif node.name != "p"
+          link_occurences << { link: actual_link, is_valid: false }
+        end
       end
     end
 
@@ -60,7 +59,7 @@ class InlineUploads
     ]
 
     if Discourse.store.external?
-      regexps << /(https?:#{SiteSetting.Upload.s3_base_url}#{UPLOAD_REGEXP_PATTERN})/
+      regexps << /((https?:)?#{SiteSetting.Upload.s3_base_url}#{UPLOAD_REGEXP_PATTERN})/
       regexps << /(#{SiteSetting.Upload.s3_cdn_url}#{UPLOAD_REGEXP_PATTERN})/
     end
 
@@ -235,28 +234,31 @@ class InlineUploads
     regexps = [
       /(upload:\/\/([a-zA-Z0-9]+)[a-z0-9\.]*)/,
       /(\/uploads\/short-url\/([a-zA-Z0-9]+)[a-z0-9\.]*)/,
+      /(#{Discourse.base_url}\/uploads\/short-url\/([a-zA-Z0-9]+)[a-z0-9\.]*)/,
     ]
 
     db = RailsMultisite::ConnectionManagement.current_db
 
     if Discourse.store.external?
       if Rails.configuration.multisite
-        regexps << /(#{SiteSetting.Upload.s3_base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
+        regexps << /((https?:)?#{SiteSetting.Upload.s3_base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
         regexps << /(#{SiteSetting.Upload.s3_cdn_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
       else
-        regexps << /(#{SiteSetting.Upload.s3_base_url}#{UPLOAD_REGEXP_PATTERN})/
+        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 << /(#{Discourse.base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
       end
     else
       regexps << /(\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
+      regexps << /(#{Discourse.base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
     end
 
     node = node.to_s
 
     regexps.each do |regexp|
-      node.scan(regexp) do |matched|
-        matches << matched[0]
+      node.scan(/(^|[\n\s"'\(>])#{regexp}($|[\n\s"'\)<])/) do |matched|
+        matches << matched[1]
       end
     end
 
diff --git a/spec/services/inline_uploads_spec.rb b/spec/services/inline_uploads_spec.rb
index e340289..5bc4caf 100644
--- a/spec/services/inline_uploads_spec.rb
+++ b/spec/services/inline_uploads_spec.rb
@@ -60,26 +60,55 @@ RSpec.describe InlineUploads do
       end
 
       it "should not correct code blocks" do
+        md = "`<a class=\"attachment\" href=\"#{upload2.url}\">In Code Block</a>`"
+
+        expect(InlineUploads.process(md)).to eq(md)
+
+        md = "    <a class=\"attachment\" href=\"#{upload2.url}\">In Code Block</a>"
+
+        expect(InlineUploads.process(md)).to eq(md)
+      end
+
+      it "should not correct invalid links in quotes" do
+        post = Fabricate(:post)
+        user = Fabricate(:user)
 
         md = <<~MD
-          `<a class=\"attachment\" href=\"#{upload2.url}\">In Code Block</a>`
+        [quote="#{user.username}, post:#{post.post_number}, topic:#{post.topic.id}"]
+        <img src="#{upload.url}"
+        someothertext#{upload2.url}someothertext
+
+        <img src="#{upload.url}"
 
-                 <a class=\"attachment\" href=\"#{upload2.url}\">In Code Block</a>
+        sometext#{upload2.url}sometext
 
-          `‍``
-          <a class=\"attachment\" href=\"#{upload2.url}\">In Code Block</a>
-          `‍``
-          a [code]<a class=\"attachment\" href=\"#{upload2.url}\">In Code Block</a>[/code] b
+        #{upload3.url}
+
+        #{Discourse.base_url}#{upload3.url}
+        [/quote]
 
-          [code]
-          <a class=\"attachment\" href=\"#{upload2.url}\">In Code Block</a>
-          [/code]
+        <img src="#{upload2.url}">
         MD
 
-        expect(InlineUploads.process(md)).to eq(md)
+        expect(InlineUploads.process(md)).to eq(<<~MD)
+        [quote="#{user.username}, post:#{post.post_number}, topic:#{post.topic.id}"]
+        <img src="#{upload.url}"
+        someothertext#{upload2.url}someothertext
+
+        <img src="#{upload.url}"
+
+        sometext#{upload2.url}sometext
+
+        #{upload3.url}
+
+        ![](#{upload3.short_url})
+        [/quote]
+
+        ![](#{upload2.short_url})
+        MD
       end
 
-      it "should not correct links in quotes" do
+      it "should correct links in quotes" do
         post = Fabricate(:post)
         user = Fabricate(:user)
 
@@ -472,11 +501,13 @@ RSpec.describe InlineUploads do
 
       it "should correct image URLs to the short version" do
         md = <<~MD
+        #{upload.url}
         <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">
         MD
 
         expect(InlineUploads.process(md)).to eq(<<~MD)
+        ![](#{upload.short_url})
         ![some image](#{upload.short_url})
         ![some image](#{upload2.short_url})
         MD

GitHub sha: 64152189

1 Like
Approved #3