FEATURE: allow UploadRecovery to be run on a single post (#8094)

This commit is contained in:
Michael Brown 2019-10-02 00:57:36 -04:00 committed by Sam
parent 35b1185a08
commit dbe0111822
1 changed files with 28 additions and 27 deletions

View File

@ -7,43 +7,44 @@ class UploadRecovery
end end
def recover(posts = Post) def recover(posts = Post)
posts.have_uploads.find_each do |post| posts.have_uploads.find_each { |post| recover_post post }
end
begin def recover_post(post)
analyzer = PostAnalyzer.new(post.raw, post.topic_id) begin
analyzer = PostAnalyzer.new(post.raw, post.topic_id)
analyzer.cooked_stripped.css("img", "a").each do |media| analyzer.cooked_stripped.css("img", "a").each do |media|
if media.name == "img" && orig_src = media["data-orig-src"] if media.name == "img" && orig_src = media["data-orig-src"]
if dom_class = media["class"] if dom_class = media["class"]
if (Post.white_listed_image_classes & dom_class.split).count > 0 if (Post.white_listed_image_classes & dom_class.split).count > 0
next next
end
end end
end
if @dry_run
puts "#{post.full_url} #{orig_src}"
else
recover_post_upload(post, Upload.sha1_from_short_url(orig_src))
end
elsif url = (media["href"] || media["src"])
data = Upload.extract_url(url)
next unless data
sha1 = data[2]
unless upload = Upload.get_from_url(url)
if @dry_run if @dry_run
puts "#{post.full_url} #{orig_src}" puts "#{post.full_url} #{url}"
else else
recover_post_upload(post, Upload.sha1_from_short_url(orig_src)) recover_post_upload(post, sha1)
end
elsif url = (media["href"] || media["src"])
data = Upload.extract_url(url)
next unless data
sha1 = data[2]
unless upload = Upload.get_from_url(url)
if @dry_run
puts "#{post.full_url} #{url}"
else
recover_post_upload(post, sha1)
end
end end
end end
end end
rescue => e
raise e if @stop_on_error
puts "#{post.full_url} #{e.class}: #{e.message}"
end end
rescue => e
raise e if @stop_on_error
puts "#{post.full_url} #{e.class}: #{e.message}"
end end
end end