FIX: UploadRecovery should look through posts for img src and bbcode.

This commit is contained in:
Guo Xiang Tan 2019-04-02 11:41:00 +08:00
parent 1f9799c979
commit 6a95d3fded
2 changed files with 68 additions and 19 deletions

View File

@ -15,34 +15,29 @@ class UploadRecovery
analyzer = PostAnalyzer.new(post.raw, post.topic_id)
analyzer.cooked_stripped.css("img", "a").each do |media|
if media.name == "img"
if media.name == "img" && orig_src = media["data-orig-src"]
if dom_class = media["class"]
if (Post.white_listed_image_classes & dom_class.split).count > 0
next
end
end
orig_src = media["data-orig-src"]
if orig_src
if @dry_run
puts "#{post.full_url} #{orig_src}"
else
recover_post_upload(post, Upload.sha1_from_short_url(orig_src))
end
if @dry_run
puts "#{post.full_url} #{orig_src}"
else
recover_post_upload(post, Upload.sha1_from_short_url(orig_src))
end
elsif media.name == "a"
href = media["href"]
elsif url = (media["href"] || media["src"])
data = Upload.extract_upload_url(url)
next unless data
if href && data = Upload.extract_upload_url(href)
sha1 = data[2]
sha1 = data[2]
unless upload = Upload.get_from_url(href)
if @dry_run
puts "#{post.full_url} #{href}"
else
recover_post_upload(post, sha1)
end
unless upload = Upload.get_from_url(url)
if @dry_run
puts "#{post.full_url} #{url}"
else
recover_post_upload(post, sha1)
end
end
end

View File

@ -113,6 +113,60 @@ RSpec.describe UploadRecovery do
expect(File.read(Discourse.store.path_for(post.uploads.first)))
.to eq(File.read(file_from_fixtures("smallest.png")))
end
describe 'image tag' do
let(:post) do
Fabricate(:post,
raw: <<~SQL,
<img src='#{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,
raw: <<~SQL,
[img]#{upload.url}[/img]
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
end
describe "#recover_user_profile_backgrounds" do