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

    Rails.logger.info "Iterating over screenshots"
    # TODO: fix for Shrine - this only works for Paperclip
    Screenshot.all.each do |screenshot|
      # Check that the file actually exists
      path = Rails.root.join('public' + screenshot.simage_url(:large))
      unless path and (File.file? path)
        Rails.logger.info "Screenshot ##{screenshot.id}'s image file not found at path '#{path}'. Removing."
        screenshot.delete
      end
    end
    Rails.logger.info "Done."
  end
end