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