From 7065a5437ea21a4044698ab5b25785e70c769db4 Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Tue, 8 Nov 2022 17:53:47 +0100 Subject: [PATCH] Properly delete files --- cleanmedia | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/cleanmedia b/cleanmedia index e3ef767..9d1fe6c 100755 --- a/cleanmedia +++ b/cleanmedia @@ -71,16 +71,18 @@ class File: def delete(self): """Delete db entries, and the file itself""" if self.fullpath() is None: + logging.info(f"No known path for file id '{self.media_id}', cannot delete file.") + elif not self.fullpath().is_dir(): logging.info(f"No known path for file id '{self.media_id}', cannot delete.") - return - for file in self.fullpath().glob('**/*'): - file.unlink() - self.fullpath().rmdir() - logging.debug(f"Deleted directory {self.fullpath()}") + else: + for file in self.fullpath().glob('*'): + file.unlink() + self.fullpath().rmdir() + logging.debug(f"Deleted directory {self.fullpath()}") with self.repo.conn.cursor() as cur: - cur.execute(f"DELETE from mediaapi_thumbnail WHERE media_id='%s';", self.media_id) + cur.execute("DELETE from mediaapi_thumbnail WHERE media_id=%s;", (self.media_id,)) num_thumbnails = cur.rowcount - cur.execute(f"DELETE from mediaapi_media_repository WHERE media_id='%s';", self.media_id) + cur.execute("DELETE from mediaapi_media_repository WHERE media_id=%s;", (self.media_id,)) num_media = cur.rowcount logging.debug(f"Deleted {num_media} + {num_thumbnails} db entries for media id {self.media_id}") @@ -162,10 +164,9 @@ if __name__ == '__main__': files = mr.get_remote_media() for file in files: if file.create_date < cleantime: - print (file.has_thumbnail(), file.base64hash) if not file.exists(): logging.info(f"file id {file.media_id} does not physically exist (path {file.fullpath()})") - if not args.dryrun: - file.delete() + if not args.dryrun: + file.delete()