clean up icons folder and database if they get unsynced in the manage site assets page

This commit is contained in:
cube
2026-03-19 00:53:17 +00:00
parent 3fb7c7964f
commit 5058c326c5
3 changed files with 106 additions and 20 deletions

View File

@@ -62,26 +62,10 @@ def edit(mid):
# this specific chunk here is checking whether icons in the myriad/static/icons folder have a link in the database
# in case the database was rebuilt, or something else happened, it is a waste of storage keeping an unlinked image
icons = db.execute("SELECT * FROM icons").fetchall()
icon_storage = os.listdir(current_app.config["ICON_UPLOAD_FOLDER"])
for icon in icon_storage:
in_database = False
for i in icons:
print(i[2], icon)
if i[2] == icon:
in_database = True
if not in_database:
os.remove(os.path.join(current_app.config["ICON_UPLOAD_FOLDER"], icon))
# and now for the same in reverse - clean the database of references to images that don't exist
for i in icons:
in_storage = False
print(i[2], i[0])
if i[2] in icon_storage:
in_storage = True
if not in_storage:
db.execute("DELETE FROM icons WHERE id=(?)", (i[0],),)
db.commit()
# the above cleanup operations should be a button in the manage sidebar but for now they are here.
@@ -187,4 +171,65 @@ def reset_theme(mid):
db.execute("UPDATE member SET card_border=(?), card_bg=(?), heading_bg=(?), heading_border=(?), heading_name=(?), heading_subtitle=(?), card_text=(?), icon_border=(?), a1=(?), a2=(?) WHERE id=(?)",(c9, c10, c11, c12, c13, c14, c15, c16, c21, c22, mid))
db.commit()
return redirect(url_for("manage.edit", mid=mid))
return redirect(url_for("manage.edit", mid=mid))
@bp.route("/assets")
@login_required
def assets():
db = get_db()
icons = db.execute("SELECT * FROM icons").fetchall()
icon_storage = os.listdir(current_app.config["ICON_UPLOAD_FOLDER"])
i_storage = []
for icon in icon_storage:
in_database = False
for i in icons:
if i[2] == icon:
in_database = True
if not in_database:
i_storage.append(icon)
unlinked_icons = []
for i in icons:
in_storage = False
if i[2] in icon_storage:
in_storage = True
if not in_storage:
unlinked_icons.append(i)
return render_template("manage/assets.html", icons=unlinked_icons, icon_storage=i_storage)
@bp.route("/delete_idb")
@login_required
def delete_idb():
db = get_db()
icons = db.execute("SELECT * FROM icons").fetchall()
icon_storage = os.listdir(current_app.config["ICON_UPLOAD_FOLDER"])
for i in icons:
in_storage = False
if i[2] in icon_storage:
in_storage = True
if not in_storage:
db.execute("DELETE FROM icons WHERE id=(?)", (i[0],))
db.commit()
return redirect(url_for("manage.assets"))
@bp.route("/delete_ifiles")
@login_required
def delete_ifiles():
db = get_db()
icons = db.execute("SELECT * FROM icons").fetchall()
icon_storage = os.listdir(current_app.config["ICON_UPLOAD_FOLDER"])
for icon in icon_storage:
in_database = False
for i in icons:
if i[2] == icon:
in_database = True
if not in_database:
os.remove(os.path.join(current_app.config["ICON_UPLOAD_FOLDER"], icon))
return redirect(url_for("manage.assets"))

View File

@@ -14,7 +14,7 @@
{% if g.user %}
<div class="heading">Manage</div>
<div class="navitem">> <a href="{{ url_for('manage.new') }}">Add New Member</a></div>
<div class="navitem">> <a href="">Site Assets</a></div>
<div class="navitem">> <a href="{{ url_for('manage.assets') }}">Site Assets</a></div>
{% endif %}
<div class="heading">Blog</div>

View File

@@ -0,0 +1,41 @@
{% extends 'base.html' %}
{% block header %}
<div class="title">{% block title %}Manage site assets{% endblock %}</div>
{% endblock %}
{% block content %}
<div class="heading big">Icons</div>
{% if icon_storage|length > 0 %}
{% for icon in icons %}
<img class="icon" src="{{ url_for('static', filename='icons/'+icon[2]) }}">
{% endfor %}
<br class="clear" />
<div class="maintext">
If there are icons above that won't load, the file itself may be changed or removed<br><br>
<a href="{{ url_for('manage.delete_idb') }}">Delete links to missing icons from database</a><br><br>
</div>
{% else %}
<div class="maintext">The database does not contain any moved/deleted icons!</div>
{% endif %}
{% if icon_storage|length > 0 %}
{% for i in icon_storage %}
<img class="icon" src="{{ url_for('static', filename='icons/'+i) }}">
{% endfor %}
<br class="clear" />
<div class="maintext">
These icons are not linked to any particular member but were found in the icons folder
<a href="{{ url_for('manage.delete_ifiles') }}">Delete files in the icons folder that have no link in the database</a>
</div>
{% else %}
<div class="maintext">There are no images in the icons folder without a database link!</div>
{% endif %}
<div class="heading big">Blinkies</div>
<div class="heading big">Stamps</div>
{% endblock %}