From 72834f19ff448ce6d0012d0ddbbbd61d9a43ac37 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 5 Sep 2018 16:54:15 +0800 Subject: [PATCH] DEV: Add rake tasks to list posts with broken images. --- lib/tasks/uploads.rake | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lib/tasks/uploads.rake b/lib/tasks/uploads.rake index 660a0484a8f..0cf836dfab8 100644 --- a/lib/tasks/uploads.rake +++ b/lib/tasks/uploads.rake @@ -715,3 +715,38 @@ task "uploads:fix_incorrect_extensions" => :environment do require_dependency "upload_fixer" UploadFixer.fix_all_extensions end + +task "uploads:list_posts_with_broken_images" => :environment do + if ENV["RAILS_DB"] + list_broken_posts + else + RailsMultisite::ConnectionManagement.each_connection do |db| + list_broken_posts + end + end +end + +def list_broken_posts + Post.where("raw LIKE '%upload:\/\/%'").find_each do |post| + begin + begin + analyzer = PostAnalyzer.new(post.raw, post.topic_id) + cooked_stripped = analyzer.send(:cooked_stripped) + end + + cooked_stripped.css("img").each do |img| + if dom_class = img["class"] + if (Post.white_listed_image_classes & dom_class.split).count > 0 + next + end + end + + if img["data-orig-src"] + puts "#{post.full_url} #{img["data-orig-src"]}" + end + end + rescue => e + puts "#{post.full_url} Error: #{e.message}" + end + end +end