From 806a7cbceef4472728762a5d5946b8d3c6d52b92 Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Mon, 15 Jun 2015 13:05:01 +0200 Subject: [PATCH] Added rake task to remove orphaned screenshot records --- lib/tasks/remove_broken_screenshots.rake | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 lib/tasks/remove_broken_screenshots.rake diff --git a/lib/tasks/remove_broken_screenshots.rake b/lib/tasks/remove_broken_screenshots.rake new file mode 100644 index 0000000..28b98ab --- /dev/null +++ b/lib/tasks/remove_broken_screenshots.rake @@ -0,0 +1,23 @@ +namespace :debshots do + desc "Remove screenshot objects with non-existing images" + + task :remove_broken_screenshots => :environment do + Rails.logger = Logger.new(STDOUT) + Rails.logger.level = Logger::INFO + #Rails.logger.level = Logger::DEBUG + + base_path = Rails.root.join('public') + + Rails.logger.info "Iterating over screenshots" + Screenshot.all.each do |screenshot| + path = File.join(base_path, screenshot.image_url(:large)) + Rails.logger.debug "- #{screenshot.id} (package: #{screenshot.package.name}) (path: #{path})" + + # Check that the file actually exists + unless File.file? path + Rails.logger.error "Screenshots not found at #{path}. Removing." + next + end + end + end +end \ No newline at end of file