A platform for community discussion. Free, open, simple.
FIX: Use filename as alt for hotlinked image uploads (#31651)
dsims89a297fde3e2
diff --git a/app/services/inline_uploads.rb b/app/services/inline_uploads.rbindex 8d5ef6aa857..72f402b8b7f 100644--- a/app/services/inline_uploads.rb+++ b/app/services/inline_uploads.rb@@ -249,7 +249,9 @@ class InlineUploadsraw =raw.gsub(%r{^(https?://\S+)(\s?)$}) do |match|if upload = blk.call(match)- ""+ filename_modified = upload.original_filename&.gsub(/[\[\]\|]/, "").to_s+ filename_modified = File.basename(filename_modified, File.extname(filename_modified))+ ""elsematchenddiff --git a/spec/jobs/pull_hotlinked_images_spec.rb b/spec/jobs/pull_hotlinked_images_spec.rbindex 9a9f8fba5a1..2f4e534f838 100644--- a/spec/jobs/pull_hotlinked_images_spec.rb+++ b/spec/jobs/pull_hotlinked_images_spec.rb@@ -1,11 +1,11 @@# frozen_string_literal: trueRSpec.describe Jobs::PullHotlinkedImages do- let(:image_url) { "http://wiki.mozilla.org/images/2/2e/Longcat1.png" }+ let(:image_url) { "http://wiki.mozilla.org/images/2/2e/Longcat1.gif" }let(:broken_image_url) { "http://wiki.mozilla.org/images/2/2e/Longcat2.png" }let(:large_image_url) { "http://wiki.mozilla.org/images/2/2e/Longcat3.png" }let(:encoded_image_url) { "https://example.com/אלחוט-.jpg" }- let(:png) do+ let(:gif) doBase64.decode64("R0lGODlhAQABALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//wBiZCH5BAEAAA8ALAAAAAABAAEAAAQC8EUAOw==",)@@ -21,11 +21,11 @@ RSpec.describe Jobs::PullHotlinkedImages dobefore doJobs.run_immediately!- stub_request(:get, image_url).to_return(body: png, headers: { "Content-Type" => "image/png" })+ stub_request(:get, image_url).to_return(body: gif, headers: { "Content-Type" => "image/gif" })stub_request(:get, encoded_image_url).to_return(- body: png,+ body: gif,headers: {- "Content-Type" => "image/png",+ "Content-Type" => "image/gif",},)stub_request(:get, broken_image_url).to_return(status: 404)@@ -173,7 +173,7 @@ RSpec.describe Jobs::PullHotlinkedImages doendit "replaces correct image URL" do- url = image_url.sub("/2e/Longcat1.png", "")+ url = image_url.sub("/2e/Longcat1.gif", "")post = Fabricate(:post, user: user, raw: "[Images](#{url})\n")stub_image_size@@ -215,7 +215,7 @@ RSpec.describe Jobs::PullHotlinkedImages doit "replaces images without extension" dourl = image_url.sub(/\.[a-zA-Z0-9]+$/, "")- stub_request(:get, url).to_return(body: png, headers: { "Content-Type" => "image/png" })+ stub_request(:get, url).to_return(body: gif, headers: { "Content-Type" => "image/gif" })post = Fabricate(:post, user: user, raw: "<img src='#{url}'>")stub_image_size@@ -459,7 +459,7 @@ RSpec.describe Jobs::PullHotlinkedImages dohttps://commons.wikimedia.org/wiki/File:Brisbane_May_2013201.jpg<img src='#{broken_image_url}'><a href='#{url}'><img src='#{large_image_url}'></a>- + MDexpect(post.cooked).to match(%r{<p><img src=.*/uploads})@@ -481,7 +481,7 @@ RSpec.describe Jobs::PullHotlinkedImages doexpect(post.raw).to eq(<<~MD.chomp)Onebox here:- + MDexpect(post.cooked).to match(%r{<img src=.*/uploads})diff --git a/spec/services/inline_uploads_spec.rb b/spec/services/inline_uploads_spec.rbindex dcc0d42fabd..651f32dddb0 100644--- a/spec/services/inline_uploads_spec.rb+++ b/spec/services/inline_uploads_spec.rb@@ -712,4 +712,33 @@ RSpec.describe InlineUploads doexpect(url).to eq("https://some-site.com/a_test?q=1&b=hello%20there")endend++ describe ".replace_hotlinked_image_urls" do+ context "when raw has an image URL" do+ fab!(:image_upload)+ it "replaces URL with image markdown and uses filename as alt" do+ origin = "http://foo.bar/#{image_upload.original_filename}"+ raw =+ InlineUploads.replace_hotlinked_image_urls(raw: "look at this:\n#{origin}") do |match_src|+ expect(match_src).to eq(origin)+ image_upload+ end++ expect(raw).to eq("look at this:\n")+ end+ end+ context "when raw has an image URL with a square bracket in filename" do+ let!(:image_upload) { Fabricate(:image_upload, original_filename: "image]1.jpg") }+ it "does not make broken markdown" do+ origin = "http://foo.bar/#{image_upload.original_filename}"+ raw =+ InlineUploads.replace_hotlinked_image_urls(raw: "look at this:\n#{origin}") do |match_src|+ expect(match_src).to eq(origin)+ image_upload+ end++ expect(raw).to eq("look at this:\n")+ end+ end+ endend