From 0ccf6c0eb73f612f1e727bac64dddea62b296e30 Mon Sep 17 00:00:00 2001 From: Claire Date: Tue, 27 Jun 2023 09:36:11 +0200 Subject: [PATCH] Fix batch attachment deletion leaving empty directories (#25587) --- app/lib/attachment_batch.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/lib/attachment_batch.rb b/app/lib/attachment_batch.rb index 6372b0131..1f87b9433 100644 --- a/app/lib/attachment_batch.rb +++ b/app/lib/attachment_batch.rb @@ -64,7 +64,15 @@ class AttachmentBatch keys << attachment.style_name_as_path(style) when :filesystem logger.debug { "Deleting #{attachment.path(style)}" } - FileUtils.remove_file(attachment.path(style), true) + path = attachment.path(style) + FileUtils.remove_file(path, true) + + begin + FileUtils.rmdir(File.dirname(path), parents: true) + rescue Errno::EEXIST, Errno::ENOTEMPTY, Errno::ENOENT, Errno::EINVAL, Errno::ENOTDIR, Errno::EACCES + # Ignore failure to delete a directory, with the same ignored errors + # as Paperclip + end when :fog logger.debug { "Deleting #{attachment.path(style)}" } attachment.directory.files.new(key: attachment.path(style)).destroy