From 6c1e0887b6987aab1785f71b4545c8bdb535e1a0 Mon Sep 17 00:00:00 2001 From: Kiril Kovachev Date: Sat, 19 Oct 2024 18:22:51 +0100 Subject: [PATCH] Create command for pruning excess profile picture files --- kanken_online/database.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/kanken_online/database.py b/kanken_online/database.py index 0b0b44d..75531f1 100644 --- a/kanken_online/database.py +++ b/kanken_online/database.py @@ -46,6 +46,18 @@ def init_db_command(): else: click.echo("Aborted.") +@click.command("prune-pfps") +def prune_pfps_command(): + """Removes excess profile pictures that aren't used in the database.""" + import os + db = get_database() + used_pfps = {row[0] for row in db.execute("SELECT pfp_filename FROM user_settings").fetchall()} + stored_pfps = set(os.listdir(current_app.config["PFP_STORE"])) + unused_pfps = stored_pfps - used_pfps + for pfp in unused_pfps: + os.remove(os.path.join(current_app.config["PFP_STORE"], pfp)) + def initialize_app(app: Flask): app.teardown_appcontext(close_database) - app.cli.add_command(init_db_command) \ No newline at end of file + app.cli.add_command(init_db_command) + app.cli.add_command(prune_pfps_command) \ No newline at end of file