The code powering m.abunchtell.com https://m.abunchtell.com
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

114 строки
4.3 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. 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. desc 'refresh', 'Fetch remote media files'
  43. long_desc <<-DESC
  44. Re-downloads media attachments from other servers. You must specify the
  45. source of media attachments with one of the following options:
  46. Use the --status option to download attachments from a specific status,
  47. using the status local numeric ID.
  48. Use the --account option to download attachments from a specific account,
  49. using username@domain handle of the account.
  50. Use the --domain option to download attachments from a specific domain.
  51. DESC
  52. def refresh
  53. dry_run = options[:dry_run] ? ' (DRY RUN)' : ''
  54. if options[:status]
  55. scope = MediaAttachment.where(status_id: options[:status])
  56. elsif options[:account]
  57. username, domain = username.split('@')
  58. account = Account.find_remote(username, domain)
  59. if account.nil?
  60. say('No such account', :red)
  61. exit(1)
  62. end
  63. scope = MediaAttachment.where(account_id: account.id)
  64. elsif options[:domain]
  65. scope = MediaAttachment.joins(:account).merge(Account.by_domain_and_subdomains(options[:domain]))
  66. else
  67. exit(1)
  68. end
  69. processed, aggregate = parallelize_with_progress(scope) do |media_attachment|
  70. next if media_attachment.remote_url.blank?
  71. unless options[:dry_run]
  72. media_attachment.reset_file!
  73. media_attachment.save
  74. end
  75. media_attachment.file_file_size
  76. end
  77. say("Downloaded #{processed} media attachments (approx. #{number_to_human_size(aggregate)})#{dry_run}", :green, true)
  78. end
  79. desc 'usage', 'Calculate disk space consumed by Mastodon'
  80. def usage
  81. 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)")
  82. say("Custom emoji:\t#{number_to_human_size(CustomEmoji.sum(:image_file_size))} (#{number_to_human_size(CustomEmoji.local.sum(:image_file_size))} local)")
  83. say("Preview cards:\t#{number_to_human_size(PreviewCard.sum(:image_file_size))}")
  84. say("Avatars:\t#{number_to_human_size(Account.sum(:avatar_file_size))} (#{number_to_human_size(Account.local.sum(:avatar_file_size))} local)")
  85. say("Headers:\t#{number_to_human_size(Account.sum(:header_file_size))} (#{number_to_human_size(Account.local.sum(:header_file_size))} local)")
  86. say("Backups:\t#{number_to_human_size(Backup.sum(:dump_file_size))}")
  87. say("Imports:\t#{number_to_human_size(Import.sum(:data_file_size))}")
  88. say("Settings:\t#{number_to_human_size(SiteUpload.sum(:file_file_size))}")
  89. end
  90. end
  91. end