FEATURE: Add placeholders to broken and large image files (#5113)

This commit is contained in:
Vinoth Kannan 2017-09-01 19:56:13 +05:30 committed by Sam
parent e283e6aea0
commit 0fb7831749
3 changed files with 96 additions and 12 deletions

View File

@ -420,3 +420,9 @@ a.mention, a.mention-group {
padding-bottom: 15px; padding-bottom: 15px;
} }
} }
.broken-image, .large-image {
border: 1px solid $primary-low;
font-size: 32px;
padding: 16px;
}

View File

@ -25,6 +25,7 @@ module Jobs
raw = post.raw.dup raw = post.raw.dup
start_raw = raw.dup start_raw = raw.dup
downloaded_urls = {} downloaded_urls = {}
broken_images, large_images = [], []
extract_images_from(post.cooked).each do |image| extract_images_from(post.cooked).each do |image|
src = original_src = image['src'] src = original_src = image['src']
@ -56,9 +57,11 @@ module Jobs
end end
else else
log(:info, "Failed to pull hotlinked image for post: #{post_id}: #{src} - Image is bigger than #{@max_size}") log(:info, "Failed to pull hotlinked image for post: #{post_id}: #{src} - Image is bigger than #{@max_size}")
large_images << original_src
end end
else else
log(:info, "There was an error while downloading '#{src}' locally for post: #{post_id}") log(:info, "There was an error while downloading '#{src}' locally for post: #{post_id}")
broken_images << original_src
end end
end end
# have we successfully downloaded that file? # have we successfully downloaded that file?
@ -98,6 +101,28 @@ module Jobs
post.revise(Discourse.system_user, changes, options) post.revise(Discourse.system_user, changes, options)
elsif downloaded_urls.present? elsif downloaded_urls.present?
post.trigger_post_process(true) post.trigger_post_process(true)
elsif broken_images.present? || large_images.present?
start_html = post.cooked
doc = Nokogiri::HTML::fragment(start_html)
images = doc.css("img[src]") - doc.css("img.avatar")
images.each do |tag|
src = tag['src']
if broken_images.include?(src)
tag.name = 'span'
tag.set_attribute('class', 'broken-image fa fa-chain-broken')
tag.remove_attribute('src')
elsif large_images.include?(src)
tag.name = 'a'
tag.set_attribute('href', src)
tag.set_attribute('target', '_blank')
tag.remove_attribute('src')
tag.inner_html = '<span class="large-image fa fa-picture-o"></span>'
end
end
if start_html == post.cooked && doc.to_html != post.cooked
post.update_column(:cooked, doc.to_html)
post.publish_change_to_clients! :revised
end
end end
end end

View File

@ -3,19 +3,29 @@ require 'jobs/regular/pull_hotlinked_images'
describe Jobs::PullHotlinkedImages do describe Jobs::PullHotlinkedImages do
describe '#execute' do let(:image_url) { "http://wiki.mozilla.org/images/2/2e/Longcat1.png" }
let(:image_url) { "http://wiki.mozilla.org/images/2/2e/Longcat1.png" } let(:broken_image_url) { "http://wiki.mozilla.org/images/2/2e/Longcat2.png" }
let(:png) { Base64.decode64("R0lGODlhAQABALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//wBiZCH5BAEAAA8ALAAAAAABAAEAAAQC8EUAOw==") } let(:large_image_url) { "http://wiki.mozilla.org/images/2/2e/Longcat3.png" }
let(:png) { Base64.decode64("R0lGODlhAQABALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//wBiZCH5BAEAAA8ALAAAAAABAAEAAAQC8EUAOw==") }
let(:large_png) { Base64.decode64("iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIAAAAlC+aJAAAK10lEQVR42r3aeVRTVx4H8Oc2atWO7Sw9OnM6HWvrOON0aFlcAZ3RopZWOyqgoCACKqPWBUVQi4gIqAVllciiKPu+JOyGnQQSNgkIIQgoKljAYVARCZnf4yXhkeXlJmDP+f4hOUF+n3fvffe++y5W0i4qJqWoDU8hKQUPxWFKcq9VnHxJ8gTi5EqS0yJOtiRZfHEyJWE0i0MnJaMJTzopaQ/wpJKS0ogneTQYABANTDlDvpxBCsiu72eUP0zPq8Fzr45e8TircRDFQAAy5ABpcgDCgJV2iCbRQM+rinU/E26ie9NgfrDO1GBtTBy96SH/WhBhaxwfGEjndmfKGeiaGsYAJXIANQyCkfR05u3dhuOKVhLamnmRzocyKp9mNo9QG9IRDDiAiMaG3Nqfo45aoJROzk3DDxNCbjGahBM0yAKoDfIDOpNZE/bNYrVKJyfylB2D91pdA3lAjwE0MDAyS+BCal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AAAAElFTkSuQmCC") }
before do
stub_request(:get, image_url).to_return(body: png, headers: { "Content-Type" => "image/png" })
stub_request(:head, image_url)
stub_request(:head, broken_image_url).to_return(status: 404)
stub_request(:get, large_image_url).to_return(body: large_png, headers: { "Content-Type" => "image/png" })
stub_request(:head, large_image_url)
SiteSetting.download_remote_images_to_local = true
SiteSetting.max_image_size_kb = 2
end
describe '#execute' do
before do before do
stub_request(:get, image_url).to_return(body: png, headers: { "Content-Type" => "image/png" })
stub_request(:head, image_url)
SiteSetting.download_remote_images_to_local = true
FastImage.expects(:size).returns([100, 100]).at_least_once FastImage.expects(:size).returns([100, 100]).at_least_once
end end
it 'replaces images' do it 'replaces images' do
post = Fabricate(:post, raw: "<img src='http://wiki.mozilla.org/images/2/2e/Longcat1.png'>") post = Fabricate(:post, raw: "<img src='#{image_url}'>")
Jobs::PullHotlinkedImages.new.execute(post_id: post.id) Jobs::PullHotlinkedImages.new.execute(post_id: post.id)
post.reload post.reload
@ -24,7 +34,8 @@ describe Jobs::PullHotlinkedImages do
end end
it 'replaces images without protocol' do it 'replaces images without protocol' do
post = Fabricate(:post, raw: "<img src='//wiki.mozilla.org/images/2/2e/Longcat1.png'>") url = image_url.sub(/^https?\:/, '')
post = Fabricate(:post, raw: "<img src='#{url}'>")
Jobs::PullHotlinkedImages.new.execute(post_id: post.id) Jobs::PullHotlinkedImages.new.execute(post_id: post.id)
post.reload post.reload
@ -33,10 +44,10 @@ describe Jobs::PullHotlinkedImages do
end end
it 'replaces images without extension' do it 'replaces images without extension' do
extensionless_url = "http://wiki.mozilla.org/images/2/2e/Longcat1" url = image_url.sub(/\.[a-zA-Z0-9]+$/, '')
stub_request(:get, extensionless_url).to_return(body: png, headers: { "Content-Type" => "image/png" }) stub_request(:get, url).to_return(body: png, headers: { "Content-Type" => "image/png" })
stub_request(:head, extensionless_url) stub_request(:head, url)
post = Fabricate(:post, raw: "<img src='#{extensionless_url}'>") post = Fabricate(:post, raw: "<img src='#{url}'>")
Jobs::PullHotlinkedImages.new.execute(post_id: post.id) Jobs::PullHotlinkedImages.new.execute(post_id: post.id)
post.reload post.reload
@ -80,6 +91,48 @@ describe Jobs::PullHotlinkedImages do
expect(post.cooked).to match(/<img src=.*\/uploads/) expect(post.cooked).to match(/<img src=.*\/uploads/)
end end
it 'all combinations' do
post = Fabricate(:post, raw: "
<img src='#{image_url}'>
#{url}
<img src='#{broken_image_url}'>
<img src='#{large_image_url}'>
")
Jobs::ProcessPost.new.execute(post_id: post.id)
Jobs::PullHotlinkedImages.new.execute(post_id: post.id)
Jobs::ProcessPost.new.execute(post_id: post.id)
Jobs::PullHotlinkedImages.new.execute(post_id: post.id)
post.reload
expect(post.cooked).to match(/<p><img src=.*\/uploads/)
expect(post.cooked).to match(/<img src=.*\/uploads.*\ class="thumbnail"/)
expect(post.cooked).to match(/<span .*\ class="broken-image fa fa-chain-broken/)
expect(post.cooked).to match(/<a .*\ href=.*\ target="_blank"><span class="large-image fa fa-picture-o"><\/span><\/a>/)
end
end
end
describe 'replace' do
it 'broken image with placeholder' do
post = Fabricate(:post, raw: "<img src='#{broken_image_url}'>")
Jobs::ProcessPost.new.execute(post_id: post.id)
Jobs::PullHotlinkedImages.new.execute(post_id: post.id)
post.reload
expect(post.cooked).to match(/<span .*\ class="broken-image fa fa-chain-broken/)
end
it 'large image with placeholder' do
post = Fabricate(:post, raw: "<img src='#{large_image_url}'>")
Jobs::ProcessPost.new.execute(post_id: post.id)
Jobs::PullHotlinkedImages.new.execute(post_id: post.id)
post.reload
expect(post.cooked).to match(/<a .*\ href=.*\ target="_blank"><span class="large-image fa fa-picture-o"><\/span><\/a>/)
end end
end end