Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

upload: Clean up empty directories in local storage.

Alex Vandiver229dcd02185f
diff --git a/zerver/lib/upload/local.py b/zerver/lib/upload/local.py
index 4deed9668d7..2dd03f8d2dc 100644
--- a/zerver/lib/upload/local.py
+++ b/zerver/lib/upload/local.py
@@ -52,8 +52,16 @@ def delete_local_file(type: Literal["avatars", "files"], path: str) -> bool:
assert_is_local_storage_path(type, file_path)
if os.path.isfile(file_path):
- # This removes the file but the empty folders still remain.
os.remove(file_path)
+
+ # Remove as many directories up the tree as are now empty
+ directory = os.path.dirname(file_path)
+ while directory != settings.LOCAL_UPLOADS_DIR:
+ try:
+ os.rmdir(directory)
+ directory = os.path.dirname(directory)
+ except OSError:
+ break
return True
file_name = path.split("/")[-1]
logging.warning("%s does not exist. Its entry in the database will be removed.", file_name)