@@ -43,5 +43,59 @@ def remove
4343
4444 say ( "Removed #{ processed } media attachments (approx. #{ number_to_human_size ( aggregate ) } ) #{ dry_run } " , :green , true )
4545 end
46+
47+ option :account , type : :string
48+ option :domain , type : :string
49+ option :status , type : :numeric
50+ option :concurrency , type : :numeric , default : 5 , aliases : [ :c ]
51+ option :verbose , type : :boolean , default : false , aliases : [ :v ]
52+ option :dry_run , type : :boolean , default : false
53+ desc 'refresh' , 'Fetch remote media files'
54+ long_desc <<-DESC
55+ Re-downloads media attachments from other servers. You must specify the
56+ source of media attachments with one of the following options:
57+
58+ Use the --status option to download attachments from a specific status,
59+ using the status local numeric ID.
60+
61+ Use the --account option to download attachments from a specific account,
62+ using username@domain handle of the account.
63+
64+ Use the --domain option to download attachments from a specific domain.
65+ DESC
66+ def refresh
67+ dry_run = options [ :dry_run ] ? ' (DRY RUN)' : ''
68+
69+ if options [ :status ]
70+ scope = MediaAttachment . where ( status_id : options [ :status ] )
71+ elsif options [ :account ]
72+ username , domain = username . split ( '@' )
73+ account = Account . find_remote ( username , domain )
74+
75+ if account . nil?
76+ say ( 'No such account' , :red )
77+ exit ( 1 )
78+ end
79+
80+ scope = MediaAttachment . where ( account_id : account . id )
81+ elsif options [ :domain ]
82+ scope = MediaAttachment . joins ( :account ) . merge ( Account . by_domain_and_subdomains ( options [ :domain ] ) )
83+ else
84+ exit ( 1 )
85+ end
86+
87+ processed , aggregate = parallelize_with_progress ( scope ) do |media_attachment |
88+ next if media_attachment . remote_url . blank?
89+
90+ unless options [ :dry_run ]
91+ media_attachment . reset_file!
92+ media_attachment . save
93+ end
94+
95+ media_attachment . file_file_size
96+ end
97+
98+ say ( "Downloaded #{ processed } media attachments (approx. #{ number_to_human_size ( aggregate ) } )#{ dry_run } " , :green , true )
99+ end
46100 end
47101end
0 commit comments