Create command for pruning excess profile picture files

This commit is contained in:
Kiril Kovachev 2024-10-19 18:22:51 +01:00
parent 477c61c4b6
commit 6c1e0887b6

View File

@ -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)
app.cli.add_command(init_db_command)
app.cli.add_command(prune_pfps_command)