aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--utils/notectl.py30
1 files changed, 27 insertions, 3 deletions
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()