From a937f2cbfbca97610488db45e0ea406a4deb0167 Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Mon, 12 Aug 2024 01:32:59 -0400 Subject: Implement the media cleanup command. --- utils/notectl.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'utils/notectl.py') diff --git a/utils/notectl.py b/utils/notectl.py index 12e804e..7117e58 100644 --- a/utils/notectl.py +++ b/utils/notectl.py @@ -11,7 +11,7 @@ from datetime import datetime, timezone import postutil from atomgen import gen_atom -from monolith import Monolith +from monolith import Monolith, MediaType from config import conf ''' @@ -142,11 +142,32 @@ def init_instance(): ''' Clean up any media file that isn't used in the monolith file. -TODO. ''' def media_cleanup(): conf.require() - pass + origs = [os.path.relpath(os.path.join(a, x), conf.LOCAL_DATA_ROOT) for (a, b, c) in os.walk(os.path.join(conf.LOCAL_DATA_ROOT, "media_orig")) for x in c] + thmbs = [os.path.relpath(os.path.join(a, x), conf.LOCAL_DATA_ROOT) for (a, b, c) in os.walk(os.path.join(conf.LOCAL_DATA_ROOT, "media_thmb")) for x in c] + referee = set() + m = Monolith(os.path.join(conf.LOCAL_DATA_ROOT, "posts.monolith")) + m.load_index() + for d in m.get_all_dates(): + p = m.get_post(d) + for media in p.media: + if media.type == MediaType.IMAGE: + referee.add(media.thumbnail) + referee.add(media.original) + pdel = [] + for f in origs + thmbs: + if f not in referee: + pdel.append(f) + if len(pdel) > 0: + for p in pdel: print(p) + print(f"Found {len(pdel)} unsed media file(s). Delete these files? [y/n]") + if input() not in ['Y', 'y']: + return + for p in pdel: os.remove(os.path.join(conf.LOCAL_DATA_ROOT, p)) + else: + print("No unused media files found.") def main(): if len(sys.argv) < 2: @@ -157,6 +178,7 @@ def main(): print("regen Regenerate the entire monolith file.") print("sync Sync data to remote for hosting.") print("init Initialize a new Notekins instance. Requires path to the instance.") + print("clean Clean up unused media files.") print("dump Dump the content of the monolith file.") return match sys.argv[1]: @@ -172,6 +194,8 @@ def main(): sync_remote() case "init": init_instance() + case "clean": + media_cleanup() case "dump": m = Monolith(os.path.join(conf.LOCAL_DATA_ROOT, "posts.monolith")) m.load_index() -- cgit v1.2.3