You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.9 KiB

  1. # frozen_string_literal: true
  2. require 'tty-prompt'
  3. require_relative '../../config/boot'
  4. require_relative '../../config/environment'
  5. require_relative 'cli_helper'
  6. module Mastodon
  7. class PreviewCardsCLI < Thor
  8. include ActionView::Helpers::NumberHelper
  9. include CLIHelper
  10. def self.exit_on_failure?
  11. true
  12. end
  13. option :days, type: :numeric, default: 180
  14. option :concurrency, type: :numeric, default: 5, aliases: [:c]
  15. option :verbose, type: :boolean, aliases: [:v]
  16. option :dry_run, type: :boolean, default: false
  17. option :link, type: :boolean, default: false
  18. desc 'remove', 'Remove preview cards'
  19. long_desc <<-DESC
  20. Removes local thumbnails for preview cards.
  21. The --days option specifies how old preview cards have to be before
  22. they are removed. It defaults to 180 days. Since preview cards will
  23. not be re-fetched unless the link is re-posted after 2 weeks from
  24. last time, it is not recommended to delete preview cards within the
  25. last 14 days.
  26. With the --link option, only link-type preview cards will be deleted,
  27. leaving video and photo cards untouched.
  28. DESC
  29. def remove
  30. time_ago = options[:days].days.ago
  31. dry_run = options[:dry_run] ? ' (DRY RUN)' : ''
  32. link = options[:link] ? 'link-type ' : ''
  33. scope = PreviewCard.cached
  34. scope = scope.where(type: :link) if options[:link]
  35. scope = scope.where('updated_at < ?', time_ago)
  36. processed, aggregate = parallelize_with_progress(scope) do |preview_card|
  37. next if preview_card.image.blank?
  38. size = preview_card.image_file_size
  39. unless options[:dry_run]
  40. preview_card.image.destroy
  41. preview_card.save
  42. end
  43. size
  44. end
  45. say("Removed #{processed} #{link}preview cards (approx. #{number_to_human_size(aggregate)})#{dry_run}", :green, true)
  46. end
  47. end
  48. end