From 209383faabc13a1724b93ac172e54859c76e43a2 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Fri, 2 Jun 2017 15:09:06 +0530 Subject: [PATCH] FEATURE: pull onebox images --- app/jobs/regular/pull_hotlinked_images.rb | 4 +- lib/cooked_post_processor.rb | 21 +++++++++-- spec/jobs/pull_hotlinked_images_spec.rb | 46 ++++++++++++++++++++++- 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/app/jobs/regular/pull_hotlinked_images.rb b/app/jobs/regular/pull_hotlinked_images.rb index c14dae92fec..316dfda9fcb 100644 --- a/app/jobs/regular/pull_hotlinked_images.rb +++ b/app/jobs/regular/pull_hotlinked_images.rb @@ -94,12 +94,14 @@ module Jobs # we never want that job to bump the topic options = { bypass_bump: true } post.revise(Discourse.system_user, changes, options) + elsif downloaded_urls.present? + post.trigger_post_process(true) end end def extract_images_from(html) doc = Nokogiri::HTML::fragment(html) - doc.css("img[src]") - doc.css(".onebox-result img") - doc.css("img.avatar") + doc.css("img[src]") - doc.css("img.avatar") end def is_valid_image_url(src) diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index 48f1d38b494..48fcfe4a733 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -51,7 +51,7 @@ class CookedPostProcessor end def keep_reverse_index_up_to_date - upload_ids = Set.new + upload_ids = Array.new @doc.css("a/@href", "img/@src").each do |media| if upload = Upload.get_from_url(media.value) @@ -59,6 +59,8 @@ class CookedPostProcessor end end + upload_ids.concat(oneboxed_image_uploads.pluck(:id)) + values = upload_ids.map{ |u| "(#{@post.id},#{u})" }.join(",") PostUpload.transaction do PostUpload.delete_all(post_id: @post.id) @@ -105,7 +107,12 @@ class CookedPostProcessor end def oneboxed_images - @doc.css(".onebox-result img, .onebox img") + @doc.css(".onebox-body img, .onebox img") + end + + def oneboxed_image_uploads + urls = oneboxed_images.map { |img| img["src"] } + Upload.where(origin: urls) end def limit_size!(img) @@ -301,8 +308,14 @@ class CookedPostProcessor Oneboxer.onebox(url, args) end - # make sure we grab dimensions for oneboxed images - oneboxed_images.each { |img| limit_size!(img) } + uploads = oneboxed_image_uploads.select(:url, :origin) + oneboxed_images.each do |img| + upload = uploads.detect { |u| u.origin == img["src"] } + next unless upload.present? + img["src"] = upload.url + # make sure we grab dimensions for oneboxed images + limit_size!(img) + end # respect nofollow admin settings if !@cooking_options[:omit_nofollow] && SiteSetting.add_rel_nofollow_to_user_content diff --git a/spec/jobs/pull_hotlinked_images_spec.rb b/spec/jobs/pull_hotlinked_images_spec.rb index ed680f9983f..220ed4e79cf 100644 --- a/spec/jobs/pull_hotlinked_images_spec.rb +++ b/spec/jobs/pull_hotlinked_images_spec.rb @@ -3,10 +3,12 @@ require 'jobs/regular/pull_hotlinked_images' describe Jobs::PullHotlinkedImages do + let(:image_url) { "http://wiki.mozilla.org/images/2/2e/Longcat1.png" } + before do png = Base64.decode64("R0lGODlhAQABALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//wBiZCH5BAEAAA8ALAAAAAABAAEAAAQC8EUAOw==") - stub_request(:get, "http://wiki.mozilla.org/images/2/2e/Longcat1.png").to_return(body: png) - stub_request(:head, "http://wiki.mozilla.org/images/2/2e/Longcat1.png") + stub_request(:get, image_url).to_return(body: png) + stub_request(:head, image_url) SiteSetting.download_remote_images_to_local = true FastImage.expects(:size).returns([100, 100]).at_least_once end @@ -29,4 +31,44 @@ describe Jobs::PullHotlinkedImages do expect(post.raw).to match(/^