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"))