FIX: `Upload#short_url` generates incorrect URL when extension is `nil`.

FIX: Upload#short_url generates incorrect URL when extension is nil.

diff --git a/app/models/post.rb b/app/models/post.rb
index fc2aaf3..84f2af6 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -890,7 +890,7 @@ class Post < ActiveRecord::Base
       /\/uploads\/#{RailsMultisite::ConnectionManagement.current_db}\//,
       /\/original\//,
       /\/optimized\//,
-      /\/uploads\/short-url\/[a-zA-Z0-9]+\..*/
+      /\/uploads\/short-url\/[a-zA-Z0-9]+(\.[a-z0-9]+)?/
     ]
 
     fragments ||= Nokogiri::HTML::fragment(self.cooked)
diff --git a/app/models/upload.rb b/app/models/upload.rb
index c25011a..bd1f079 100644
--- a/app/models/upload.rb
+++ b/app/models/upload.rb
@@ -361,7 +361,7 @@ class Upload < ActiveRecord::Base
   private
 
   def short_url_basename
-    "#{Upload.base62_sha1(sha1)}.#{extension}"
+    "#{Upload.base62_sha1(sha1)}#{extension.present? ? ".#{extension}" : ""}"
   end
 
 end
diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb
index acc1ea3..5293060 100644
--- a/spec/models/post_spec.rb
+++ b/spec/models/post_spec.rb
@@ -1234,6 +1234,7 @@ describe Post do
     fab!(:audio_upload) { Fabricate(:upload, extension: "ogg") }
     fab!(:attachment_upload) { Fabricate(:upload, extension: "csv") }
     fab!(:attachment_upload_2) { Fabricate(:upload) }
+    fab!(:attachment_upload_3) { Fabricate(:upload, extension: nil) }
 
     let(:base_url) { "#{Discourse.base_url_no_prefix}#{Discourse.base_uri}" }
     let(:video_url) { "#{base_url}#{video_upload.url}" }
@@ -1243,6 +1244,7 @@ describe Post do
       <<~RAW
       <a href="#{attachment_upload.url}">Link</a>
       [test|attachment](#{attachment_upload_2.short_url})
+      [test3|attachment](#{attachment_upload_3.short_url})
       <img src="#{image_upload.url}">
 
       <video width="100%" height="100%" controls>
@@ -1272,7 +1274,8 @@ describe Post do
         image_upload.id,
         audio_upload.id,
         attachment_upload.id,
-        attachment_upload_2.id
+        attachment_upload_2.id,
+        attachment_upload_3.id
       )
     end
 
diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb
index 44ff1f1..89bbe33 100644
--- a/spec/models/upload_spec.rb
+++ b/spec/models/upload_spec.rb
@@ -235,6 +235,9 @@ describe Upload do
     it "should generate a correct short url" do
       upload = Upload.new(sha1: 'bda2c513e1da04f7b4e99230851ea2aafeb8cc4e', extension: 'png')
       expect(upload.short_url).to eq('upload://r3AYqESanERjladb4vBB7VsMBm6.png')
+
+      upload.extension = nil
+      expect(upload.short_url).to eq('upload://r3AYqESanERjladb4vBB7VsMBm6')
     end
   end

GitHub sha: 73a45048

2 Likes