FIX: skip markdown conversion for hotlinked non image urls

FIX: skip markdown conversion for hotlinked non image urls

diff --git a/app/services/inline_uploads.rb b/app/services/inline_uploads.rb
index e5652db..13d5db7 100644
--- a/app/services/inline_uploads.rb
+++ b/app/services/inline_uploads.rb
@@ -73,6 +73,9 @@ class InlineUploads
 
       markdown.scan(/(\n{2,}|\A)#{regexp}$/) do |match|
         if match[1].present?
+          extension = match[1].split(".")[-1].downcase
+          next if FileHelper.supported_images.exclude?(extension)
+
           index = $~.offset(2)[0]
           indexes << index
           raw_matches << [match[1], match[1], +"![](#{PLACEHOLDER})", index]
@@ -120,6 +123,11 @@ class InlineUploads
       .sort { |a, b| a[3] <=> b[3] }
       .each do |match, link, replace_with, _index|
 
+      if match == link
+        extension = match.split(".")[-1].downcase
+        next if FileHelper.supported_images.exclude?(extension)
+      end
+
       node_info = link_occurences.shift
       next unless node_info&.dig(:is_valid)
 
diff --git a/spec/fabricators/upload_fabricator.rb b/spec/fabricators/upload_fabricator.rb
index 8c54009..ae211c5 100644
--- a/spec/fabricators/upload_fabricator.rb
+++ b/spec/fabricators/upload_fabricator.rb
@@ -21,6 +21,15 @@ Fabricator(:upload) do
   extension "png"
 end
 
+Fabricator(:video_upload, from: :upload) do
+  original_filename "video.mp4"
+  width nil
+  height nil
+  thumbnail_width nil
+  thumbnail_height nil
+  extension "mp4"
+end
+
 Fabricator(:upload_s3, from: :upload) do
   url do |attrs|
     sequence(:url) do |n|
diff --git a/spec/services/inline_uploads_spec.rb b/spec/services/inline_uploads_spec.rb
index 88e3f39..32e1d18 100644
--- a/spec/services/inline_uploads_spec.rb
+++ b/spec/services/inline_uploads_spec.rb
@@ -228,6 +228,19 @@ RSpec.describe InlineUploads do
         MD
       end
 
+      it "should not correct non image URLs to the short url and paths" do
+        SiteSetting.authorized_extensions = "mp4"
+        upload4 = Fabricate(:video_upload)
+
+        md = <<~MD
+        #{GlobalSetting.cdn_url}#{upload4.url}
+        MD
+
+        expect(InlineUploads.process(md)).to eq(<<~MD)
+        #{GlobalSetting.cdn_url}#{upload4.url}
+        MD
+      end
+
       it "should correct img tags with uppercase upload extension" do
         md = <<~MD
         test<img src="#{upload.url.sub(".png", ".PNG")}">

GitHub sha: 3840ace9

Since the markdown shouldn’t be touched, I think it makes more sense to re-use the md variable rather than copy-pasting the same string.

expect(InlineUploads.process(md)).to eq(md)
1 Like

upload4 doesn’t really make much sense since there are no other uploads in this test.

I think either upload or video_upload would be more clear.

1 Like

SPEC: improve the code readability

FIX: convert hotlinked non-image urls to short url.