The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

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