mirror of
https://github.com/mastodon/mastodon.git
synced 2024-11-21 21:57:19 +00:00
Refactor common checks in tootctl media remove-orphans
(#32720)
This commit is contained in:
parent
ff490daa58
commit
dfccd207bb
|
@ -6,6 +6,8 @@ module Mastodon::CLI
|
||||||
class Media < Base
|
class Media < Base
|
||||||
include ActionView::Helpers::NumberHelper
|
include ActionView::Helpers::NumberHelper
|
||||||
|
|
||||||
|
class UnrecognizedOrphanType < StandardError; end
|
||||||
|
|
||||||
VALID_PATH_SEGMENTS_SIZE = [7, 10].freeze
|
VALID_PATH_SEGMENTS_SIZE = [7, 10].freeze
|
||||||
|
|
||||||
option :days, type: :numeric, default: 7, aliases: [:d]
|
option :days, type: :numeric, default: 7, aliases: [:d]
|
||||||
|
@ -120,23 +122,10 @@ module Mastodon::CLI
|
||||||
object.acl.put(acl: s3_permissions) if options[:fix_permissions] && !dry_run?
|
object.acl.put(acl: s3_permissions) if options[:fix_permissions] && !dry_run?
|
||||||
|
|
||||||
path_segments = object.key.split('/')
|
path_segments = object.key.split('/')
|
||||||
path_segments.delete('cache')
|
|
||||||
|
|
||||||
unless VALID_PATH_SEGMENTS_SIZE.include?(path_segments.size)
|
|
||||||
progress.log(pastel.yellow("Unrecognized file found: #{object.key}"))
|
|
||||||
next
|
|
||||||
end
|
|
||||||
|
|
||||||
model_name = path_segments.first.classify
|
|
||||||
attachment_name = path_segments[1].singularize
|
|
||||||
record_id = path_segments[2...-2].join.to_i
|
|
||||||
file_name = path_segments.last
|
|
||||||
record = record_map.dig(model_name, record_id)
|
|
||||||
attachment = record&.public_send(attachment_name)
|
|
||||||
|
|
||||||
progress.increment
|
progress.increment
|
||||||
|
|
||||||
next unless attachment.blank? || !attachment.variant?(file_name)
|
next unless orphaned_file?(path_segments, record_map)
|
||||||
|
|
||||||
begin
|
begin
|
||||||
object.delete unless dry_run?
|
object.delete unless dry_run?
|
||||||
|
@ -148,6 +137,8 @@ module Mastodon::CLI
|
||||||
rescue => e
|
rescue => e
|
||||||
progress.log(pastel.red("Error processing #{object.key}: #{e}"))
|
progress.log(pastel.red("Error processing #{object.key}: #{e}"))
|
||||||
end
|
end
|
||||||
|
rescue UnrecognizedOrphanType
|
||||||
|
progress.log(pastel.yellow("Unrecognized file found: #{object.key}"))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
when :fog
|
when :fog
|
||||||
|
@ -165,26 +156,10 @@ module Mastodon::CLI
|
||||||
key = path.gsub("#{root_path}#{File::SEPARATOR}", '')
|
key = path.gsub("#{root_path}#{File::SEPARATOR}", '')
|
||||||
|
|
||||||
path_segments = key.split(File::SEPARATOR)
|
path_segments = key.split(File::SEPARATOR)
|
||||||
path_segments.delete('cache')
|
|
||||||
|
|
||||||
unless VALID_PATH_SEGMENTS_SIZE.include?(path_segments.size)
|
|
||||||
progress.log(pastel.yellow("Unrecognized file found: #{key}"))
|
|
||||||
next
|
|
||||||
end
|
|
||||||
|
|
||||||
model_name = path_segments.first.classify
|
|
||||||
record_id = path_segments[2...-2].join.to_i
|
|
||||||
attachment_name = path_segments[1].singularize
|
|
||||||
file_name = path_segments.last
|
|
||||||
|
|
||||||
next unless PRELOADED_MODELS.include?(model_name)
|
|
||||||
|
|
||||||
record = model_name.constantize.find_by(id: record_id)
|
|
||||||
attachment = record&.public_send(attachment_name)
|
|
||||||
|
|
||||||
progress.increment
|
progress.increment
|
||||||
|
|
||||||
next unless attachment.blank? || !attachment.variant?(file_name)
|
next unless orphaned_file?(path_segments)
|
||||||
|
|
||||||
begin
|
begin
|
||||||
size = File.size(path)
|
size = File.size(path)
|
||||||
|
@ -205,6 +180,8 @@ module Mastodon::CLI
|
||||||
rescue => e
|
rescue => e
|
||||||
progress.log(pastel.red("Error processing #{key}: #{e}"))
|
progress.log(pastel.red("Error processing #{key}: #{e}"))
|
||||||
end
|
end
|
||||||
|
rescue UnrecognizedOrphanType
|
||||||
|
progress.log(pastel.yellow("Unrecognized file found: #{path}"))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -364,5 +341,23 @@ module Mastodon::CLI
|
||||||
model_map[model_name] = model_name.constantize.where(id: record_ids).index_by(&:id)
|
model_map[model_name] = model_name.constantize.where(id: record_ids).index_by(&:id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def orphaned_file?(path_segments, record_map = nil)
|
||||||
|
path_segments.delete('cache')
|
||||||
|
|
||||||
|
raise UnrecognizedOrphanType unless VALID_PATH_SEGMENTS_SIZE.include?(path_segments.size)
|
||||||
|
|
||||||
|
model_name = path_segments.first.classify
|
||||||
|
record_id = path_segments[2...-2].join.to_i
|
||||||
|
attachment_name = path_segments[1].singularize
|
||||||
|
file_name = path_segments.last
|
||||||
|
|
||||||
|
raise UnrecognizedOrphanType unless PRELOADED_MODELS.include?(model_name)
|
||||||
|
|
||||||
|
record = record_map.present? ? record_map.dig(model_name, record_id) : model_name.constantize.find_by(id: record_id)
|
||||||
|
attachment = record&.public_send(attachment_name)
|
||||||
|
|
||||||
|
attachment.blank? || !attachment.variant?(file_name)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue