From d0fe42e2ef581b7aed5428a0f16c757140df90bc Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Wed, 10 Apr 2019 13:52:35 +0530 Subject: [PATCH] FIX: should look through posts for image markdown Downloaded onebox images only included in the cooked HTML content. So we have to check 'post.cooked' instead of 'raw'. bfdd0fe64cc90707569a158b0dbdecedf3d03986 --- app/models/post.rb | 7 +++++++ lib/tasks/posts.rake | 5 ++--- lib/upload_recovery.rb | 7 +------ spec/lib/upload_recovery_spec.rb | 27 +++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index b26510ce04d..f2a93c50237 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -108,6 +108,13 @@ class Post < ActiveRecord::Base end } + scope :have_uploads, -> { + where( + "(posts.cooked LIKE '% :environment do name = "missing_uploads" - db_name = RailsMultisite::ConnectionManagement.current_db PostCustomField.where(name: name).destroy_all - posts = Post.where("(posts.cooked LIKE '% :environment do Nokogiri::HTML::fragment(post.cooked).css("a/@href", "img/@src").each do |media| src = media.value - next if src.blank? || (src =~ /\/uploads\/#{db_name}\//).blank? + next if src.blank? || (src =~ /\/uploads\/#{RailsMultisite::ConnectionManagement.current_db}\//).blank? src = "#{SiteSetting.force_https ? "https" : "http"}:#{src}" if src.start_with?("//") next unless Discourse.store.has_been_uploaded?(src) || src =~ /\A\/[^\/]/i diff --git a/lib/upload_recovery.rb b/lib/upload_recovery.rb index 4e5bec5ca25..eba98491041 100644 --- a/lib/upload_recovery.rb +++ b/lib/upload_recovery.rb @@ -4,12 +4,7 @@ class UploadRecovery end def recover(posts = Post) - posts.where(" - raw LIKE '%upload:\/\/%' - OR raw LIKE '%href=%' - OR raw LIKE '%src=%' - OR raw LIKE '%[img]%' - ").find_each do |post| + posts.have_uploads.find_each do |post| begin analyzer = PostAnalyzer.new(post.raw, post.topic_id) diff --git a/spec/lib/upload_recovery_spec.rb b/spec/lib/upload_recovery_spec.rb index fca10f57213..79eccb2e302 100644 --- a/spec/lib/upload_recovery_spec.rb +++ b/spec/lib/upload_recovery_spec.rb @@ -141,6 +141,33 @@ RSpec.describe UploadRecovery do end end + describe 'image markdown' do + let(:post) do + Fabricate(:post, + raw: <<~SQL, + ![image](#{upload.url}) + SQL + user: user + ).tap(&:link_post_uploads) + end + + it 'should recover the upload' do + stub_request(:get, "http://test.localhost#{upload.url}") + .to_return(status: 200) + + expect do + upload.destroy! + end.to change { post.reload.uploads.count }.from(1).to(0) + + expect do + upload_recovery.recover + end.to change { post.reload.uploads.count }.from(0).to(1) + + expect(File.read(Discourse.store.path_for(post.uploads.first))) + .to eq(File.read(file_from_fixtures("smallest.png"))) + end + end + describe 'bbcode' do let(:post) do Fabricate(:post,