From ca2bd380cf18bad4b0979ced00450ad9dc3e24eb Mon Sep 17 00:00:00 2001 From: Arksine Date: Sun, 14 Mar 2021 14:24:51 -0400 Subject: [PATCH] file_manager: set a metadata version to force re-processing When an entry is removed from metadata also remove associated thumbnails. Signed-off-by: Eric Callahan --- moonraker/plugins/file_manager.py | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/moonraker/plugins/file_manager.py b/moonraker/plugins/file_manager.py index bdfd0f7..aa924bb 100644 --- a/moonraker/plugins/file_manager.py +++ b/moonraker/plugins/file_manager.py @@ -622,6 +622,7 @@ class FileManager: METADATA_PRUNE_TIME = 600000 METADATA_NAMESPACE = "gcode_metadata" +METADATA_VERSION = 2 class MetadataStorage: def __init__(self, server, gc_path, database): @@ -629,6 +630,14 @@ class MetadataStorage: database.register_local_namespace(METADATA_NAMESPACE) self.mddb = database.wrap_namespace( METADATA_NAMESPACE, parse_keys=False) + version = database.get_item( + "moonraker", "file_manager.metadata_version", 0) + if version != METADATA_VERSION: + # Clear existing metadata when version is bumped + self.mddb.clear() + database.insert_item( + "moonraker", "file_manager.metadata_version", + METADATA_VERSION) self.pending_requests = {} self.events = {} self.busy = False @@ -657,7 +666,7 @@ class MetadataStorage: for fname in list(self.mddb.keys()): fpath = os.path.join(self.gc_path, fname) if not os.path.exists(fpath): - del self.mddb[fname] + self.remove_file(fname) logging.info(f"Pruned file: {fname}") continue @@ -666,10 +675,21 @@ class MetadataStorage: return mdata['size'] == fsize and mdata['modified'] == modified def remove_file(self, fname): - try: - del self.mddb[fname] - except Exception: - pass + metadata = self.mddb.pop(fname, None) + if metadata is None: + return + # Delete associated thumbnails + fdir = os.path.dirname(os.path.join(self.gc_path, fname)) + if "thumbnails" in metadata: + for thumb in metadata["thumbnails"]: + path = thumb.get("relative_path", None) + if path is None: + continue + thumb_path = os.path.join(fdir, path) + try: + os.remove(thumb_path) + except Exception: + logging.debug(f"Error removing thumb at {thumb_path}") def parse_metadata(self, fname, fsize, modified, notify=False): evt = Event()