闭社主体 forked from https://github.com/tootsuite/mastodon
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.

70 lines
2.7 KiB

  1. # frozen_string_literal: true
  2. require_relative '../../config/boot'
  3. require_relative '../../config/environment'
  4. require_relative 'cli_helper'
  5. module Mastodon
  6. class MediaCLI < Thor
  7. include ActionView::Helpers::NumberHelper
  8. def self.exit_on_failure?
  9. true
  10. end
  11. option :days, type: :numeric, default: 7
  12. option :background, type: :boolean, default: false
  13. option :verbose, type: :boolean, default: false
  14. option :dry_run, type: :boolean, default: false
  15. desc 'remove', 'Remove remote media files'
  16. long_desc <<-DESC
  17. Removes locally cached copies of media attachments from other servers.
  18. The --days option specifies how old media attachments have to be before
  19. they are removed. It defaults to 7 days.
  20. With the --background option, instead of deleting the files sequentially,
  21. they will be queued into Sidekiq and the command will exit as soon as
  22. possible. In Sidekiq they will be processed with higher concurrency, but
  23. it may impact other operations of the Mastodon server, and it may overload
  24. the underlying file storage.
  25. With the --dry-run option, no work will be done.
  26. With the --verbose option, when media attachments are processed sequentially in the
  27. foreground, the IDs of the media attachments will be printed.
  28. DESC
  29. def remove
  30. time_ago = options[:days].days.ago
  31. queued = 0
  32. processed = 0
  33. size = 0
  34. dry_run = options[:dry_run] ? '(DRY RUN)' : ''
  35. if options[:background]
  36. MediaAttachment.where.not(remote_url: '').where.not(file_file_name: nil).where('created_at < ?', time_ago).select(:id, :file_file_size).reorder(nil).find_in_batches do |media_attachments|
  37. queued += media_attachments.size
  38. size += media_attachments.reduce(0) { |sum, m| sum + (m.file_file_size || 0) }
  39. Maintenance::UncacheMediaWorker.push_bulk(media_attachments.map(&:id)) unless options[:dry_run]
  40. end
  41. else
  42. MediaAttachment.where.not(remote_url: '').where.not(file_file_name: nil).where('created_at < ?', time_ago).reorder(nil).find_in_batches do |media_attachments|
  43. media_attachments.each do |m|
  44. size += m.file_file_size || 0
  45. Maintenance::UncacheMediaWorker.new.perform(m) unless options[:dry_run]
  46. options[:verbose] ? say(m.id) : say('.', :green, false)
  47. processed += 1
  48. end
  49. end
  50. end
  51. say
  52. if options[:background]
  53. say("Scheduled the deletion of #{queued} media attachments (approx. #{number_to_human_size(size)}) #{dry_run}", :green, true)
  54. else
  55. say("Removed #{processed} media attachments (approx. #{number_to_human_size(size)}) #{dry_run}", :green, true)
  56. end
  57. end
  58. end
  59. end