media_cli.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. include CLIHelper
  9. def self.exit_on_failure?
  10. true
  11. end
  12. option :days, type: :numeric, default: 7, aliases: [:d]
  13. option :concurrency, type: :numeric, default: 5, aliases: [:c]
  14. option :verbose, type: :boolean, default: false, aliases: [:v]
  15. option :dry_run, type: :boolean, default: false
  16. desc 'remove', 'Remove remote media files'
  17. long_desc <<-DESC
  18. Removes locally cached copies of media attachments from other servers.
  19. The --days option specifies how old media attachments have to be before
  20. they are removed. It defaults to 7 days.
  21. DESC
  22. def remove
  23. time_ago = options[:days].days.ago
  24. dry_run = options[:dry_run] ? '(DRY RUN)' : ''
  25. processed, aggregate = parallelize_with_progress(MediaAttachment.cached.where.not(remote_url: '').where('created_at < ?', time_ago)) do |media_attachment|
  26. next if media_attachment.file.blank?
  27. size = media_attachment.file_file_size
  28. unless options[:dry_run]
  29. media_attachment.file.destroy
  30. media_attachment.save
  31. end
  32. size
  33. end
  34. say("Removed #{processed} media attachments (approx. #{number_to_human_size(aggregate)}) #{dry_run}", :green, true)
  35. end
  36. option :account, type: :string
  37. option :domain, type: :string
  38. option :status, type: :numeric
  39. option :concurrency, type: :numeric, default: 5, aliases: [:c]
  40. option :verbose, type: :boolean, default: false, aliases: [:v]
  41. option :dry_run, type: :boolean, default: false
  42. option :force, type: :boolean, default: false
  43. desc 'refresh', 'Fetch remote media files'
  44. long_desc <<-DESC
  45. Re-downloads media attachments from other servers. You must specify the
  46. source of media attachments with one of the following options:
  47. Use the --status option to download attachments from a specific status,
  48. using the status local numeric ID.
  49. Use the --account option to download attachments from a specific account,
  50. using username@domain handle of the account.
  51. Use the --domain option to download attachments from a specific domain.
  52. By default, attachments that are believed to be already downloaded will
  53. not be re-downloaded. To force re-download of every URL, use --force.
  54. DESC
  55. def refresh
  56. dry_run = options[:dry_run] ? ' (DRY RUN)' : ''
  57. if options[:status]
  58. scope = MediaAttachment.where(status_id: options[:status])
  59. elsif options[:account]
  60. username, domain = username.split('@')
  61. account = Account.find_remote(username, domain)
  62. if account.nil?
  63. say('No such account', :red)
  64. exit(1)
  65. end
  66. scope = MediaAttachment.where(account_id: account.id)
  67. elsif options[:domain]
  68. scope = MediaAttachment.joins(:account).merge(Account.by_domain_and_subdomains(options[:domain]))
  69. else
  70. exit(1)
  71. end
  72. processed, aggregate = parallelize_with_progress(scope) do |media_attachment|
  73. next if media_attachment.remote_url.blank? || (!options[:force] && media_attachment.file_file_name.present?)
  74. unless options[:dry_run]
  75. media_attachment.reset_file!
  76. media_attachment.save
  77. end
  78. media_attachment.file_file_size
  79. end
  80. say("Downloaded #{processed} media attachments (approx. #{number_to_human_size(aggregate)})#{dry_run}", :green, true)
  81. end
  82. desc 'usage', 'Calculate disk space consumed by Mastodon'
  83. def usage
  84. say("Attachments:\t#{number_to_human_size(MediaAttachment.sum(:file_file_size))} (#{number_to_human_size(MediaAttachment.where(account: Account.local).sum(:file_file_size))} local)")
  85. say("Custom emoji:\t#{number_to_human_size(CustomEmoji.sum(:image_file_size))} (#{number_to_human_size(CustomEmoji.local.sum(:image_file_size))} local)")
  86. say("Preview cards:\t#{number_to_human_size(PreviewCard.sum(:image_file_size))}")
  87. say("Avatars:\t#{number_to_human_size(Account.sum(:avatar_file_size))} (#{number_to_human_size(Account.local.sum(:avatar_file_size))} local)")
  88. say("Headers:\t#{number_to_human_size(Account.sum(:header_file_size))} (#{number_to_human_size(Account.local.sum(:header_file_size))} local)")
  89. say("Backups:\t#{number_to_human_size(Backup.sum(:dump_file_size))}")
  90. say("Imports:\t#{number_to_human_size(Import.sum(:data_file_size))}")
  91. say("Settings:\t#{number_to_human_size(SiteUpload.sum(:file_file_size))}")
  92. end
  93. desc 'lookup', 'Lookup where media is displayed by passing a media URL'
  94. def lookup
  95. prompt = TTY::Prompt.new
  96. url = prompt.ask('Please enter a URL to the media to lookup:', required: true)
  97. attachment_id = url
  98. .split('/')[0..-2]
  99. .grep(/\A\d+\z/)
  100. .join('')
  101. if url.split('/')[0..-2].include? 'media_attachments'
  102. model = MediaAttachment.find(attachment_id).status
  103. prompt.say(ActivityPub::TagManager.instance.url_for(model))
  104. elsif url.split('/')[0..-2].include? 'accounts'
  105. model = Account.find(attachment_id)
  106. prompt.say(ActivityPub::TagManager.instance.url_for(model))
  107. else
  108. prompt.say('Not found')
  109. end
  110. end
  111. end
  112. end