Merge pull request #4643 from LeoMcA/fix-pull-img

FIX: Handle img src starting with "//" in pull_hotlinked_images job
This commit is contained in:
Régis Hanol 2017-01-23 17:56:53 +01:00 committed by GitHub
commit 7e52d29a5b
2 changed files with 30 additions and 2 deletions

View File

@ -26,7 +26,7 @@ module Jobs
downloaded_urls = {} downloaded_urls = {}
extract_images_from(post.cooked).each do |image| extract_images_from(post.cooked).each do |image|
src = image['src'] src = original_src = image['src']
src = "http:" + src if src.start_with?("//") src = "http:" + src if src.start_with?("//")
if is_valid_image_url(src) if is_valid_image_url(src)
@ -53,7 +53,7 @@ module Jobs
# have we successfully downloaded that file? # have we successfully downloaded that file?
if downloaded_urls[src].present? if downloaded_urls[src].present?
url = downloaded_urls[src] url = downloaded_urls[src]
escaped_src = Regexp.escape(src) escaped_src = Regexp.escape(original_src)
# there are 6 ways to insert an image in a post # there are 6 ways to insert an image in a post
# HTML tag - <img src="http://..."> # HTML tag - <img src="http://...">
raw.gsub!(/src=["']#{escaped_src}["']/i, "src='#{url}'") raw.gsub!(/src=["']#{escaped_src}["']/i, "src='#{url}'")

View File

@ -0,0 +1,28 @@
require 'rails_helper'
require 'jobs/regular/pull_hotlinked_images'
describe Jobs::PullHotlinkedImages do
png = Base64.decode64("R0lGODlhAQABALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//wBiZCH5BAEAAA8ALAAAAAABAAEAAAQC8EUAOw==")
FakeWeb.register_uri(:get, "http://wiki.mozilla.org/images/2/2e/Longcat1.png", body: png)
SiteSetting.download_remote_images_to_local = true
it 'replaces image src' do
post = Fabricate(:post, raw: "<img src='http://wiki.mozilla.org/images/2/2e/Longcat1.png'>")
Jobs::PullHotlinkedImages.new.execute(post_id: post.id)
post.reload
expect(post.raw).to match(/^<img src='\/uploads/)
end
it 'replaces image src without protocol' do
post = Fabricate(:post, raw: "<img src='//wiki.mozilla.org/images/2/2e/Longcat1.png'>")
Jobs::PullHotlinkedImages.new.execute(post_id: post.id)
post.reload
expect(post.raw).to match(/^<img src='\/uploads/)
end
end