about summary refs log tree commit diff
path: root/lib/mastodon/media_cli.rb
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2019-12-09 04:26:00 +0100
committerGitHub <noreply@github.com>2019-12-09 04:26:00 +0100
commitd7bcc0930cea66c646b56db3587426880b4368df (patch)
tree18c5f684c1d32b660462f6b175209f776ad8a68e /lib/mastodon/media_cli.rb
parentf3d232381d60cbc93cb7a35285eb24c30cd0aba0 (diff)
Fix error handling in `tootctl media remove-orphans` (#12571)
Diffstat (limited to 'lib/mastodon/media_cli.rb')
-rw-r--r--lib/mastodon/media_cli.rb40
1 files changed, 31 insertions, 9 deletions
diff --git a/lib/mastodon/media_cli.rb b/lib/mastodon/media_cli.rb
index 96ad8556a..d842b986f 100644
--- a/lib/mastodon/media_cli.rb
+++ b/lib/mastodon/media_cli.rb
@@ -67,7 +67,15 @@ module Mastodon
         last_key           = options[:start_after]
 
         loop do
-          objects = bucket.objects(start_after: last_key, prefix: 'media_attachments/files/').limit(1000).map { |x| x }
+          objects = begin
+            begin
+              bucket.objects(start_after: last_key, prefix: 'media_attachments/files/').limit(1000).map { |x| x }
+            rescue => e
+              progress.log(pastel.red("Error fetching list of files: #{e}"))
+              progress.log("If you want to continue from this point, add --start-after=#{last_key} to your command") if last_key
+              break
+            end
+          end
 
           break if objects.empty?
 
@@ -82,10 +90,16 @@ module Mastodon
 
             next unless attachments_map[attachment_id].nil? || !attachments_map[attachment_id].variant?(filename)
 
-            reclaimed_bytes += object.size
-            removed += 1
-            object.delete unless options[:dry_run]
-            progress.log("Found and removed orphan: #{object.key}")
+            begin
+              object.delete unless options[:dry_run]
+
+              reclaimed_bytes += object.size
+              removed += 1
+
+              progress.log("Found and removed orphan: #{object.key}")
+            rescue => e
+              progress.log(pastel.red("Error processing #{object.key}: #{e}"))
+            end
           end
         end
       when :fog
@@ -108,10 +122,18 @@ module Mastodon
 
           next unless attachment.nil? || !attachment.variant?(filename)
 
-          reclaimed_bytes += File.size(path)
-          removed += 1
-          File.delete(path) unless options[:dry_run]
-          progress.log("Found and removed orphan: #{key}")
+          begin
+            size = File.size(path)
+
+            File.delete(path) unless options[:dry_run]
+
+            reclaimed_bytes += size
+            removed += 1
+
+            progress.log("Found and removed orphan: #{key}")
+          rescue => e
+            progress.log(pastel.red("Error processing #{key}: #{e}"))
+          end
         end
       end