From 78e03649abbba3961aa9848d2a10022958943cf3 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 5 Jul 2022 03:47:10 +0100 Subject: [PATCH] FIX: Replace onebox markdown when pulling hotlinked image (#17328) If an image is oneboxed directly, then we should replace the onebox URL with a markdown image tag. This ensures that the wrapper link points to the downloaded version rather than the original. This regressed in bf6f8299 --- app/services/inline_uploads.rb | 8 ++++++++ spec/jobs/pull_hotlinked_images_spec.rb | 3 +++ 2 files changed, 11 insertions(+) diff --git a/app/services/inline_uploads.rb b/app/services/inline_uploads.rb index b2519f6331f..3cc45a6b5a9 100644 --- a/app/services/inline_uploads.rb +++ b/app/services/inline_uploads.rb @@ -245,6 +245,14 @@ class InlineUploads # Markdown inline - ![alt](http://... "image title") InlineUploads.match_md_inline_img(raw, external_src: true, &replace) + raw.gsub!(/^(https?:\/\/\S+)(\s?)$/) do |match| + if upload = blk.call(match) + "![](#{upload.short_url})" + else + match + end + end + raw end diff --git a/spec/jobs/pull_hotlinked_images_spec.rb b/spec/jobs/pull_hotlinked_images_spec.rb index 3e6cc6088b7..3de3afc1c83 100644 --- a/spec/jobs/pull_hotlinked_images_spec.rb +++ b/spec/jobs/pull_hotlinked_images_spec.rb @@ -350,6 +350,7 @@ describe Jobs::PullHotlinkedImages do before do stub_request(:head, url) stub_request(:get, url).to_return(body: '') + stub_request(:head, image_url) stub_request(:get, api_url).to_return(body: "{ \"query\": { @@ -399,6 +400,7 @@ describe Jobs::PullHotlinkedImages do #{url} + #{image_url} MD stub_image_size @@ -413,6 +415,7 @@ describe Jobs::PullHotlinkedImages do https://commons.wikimedia.org/wiki/File:Brisbane_May_2013201.jpg + ![](upload://z2QSs1KJWoj51uYhDjb6ifCzxH6.gif) MD expect(post.cooked).to match(/