Properly delete files

This commit is contained in:
Sebastian Spaeth 2022-11-08 17:53:47 +01:00
parent be6da99c1c
commit 7065a5437e

View File

@ -71,16 +71,18 @@ class File:
def delete(self): def delete(self):
"""Delete db entries, and the file itself""" """Delete db entries, and the file itself"""
if self.fullpath() is None: 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.") logging.info(f"No known path for file id '{self.media_id}', cannot delete.")
return else:
for file in self.fullpath().glob('**/*'): for file in self.fullpath().glob('*'):
file.unlink() file.unlink()
self.fullpath().rmdir() self.fullpath().rmdir()
logging.debug(f"Deleted directory {self.fullpath()}") logging.debug(f"Deleted directory {self.fullpath()}")
with self.repo.conn.cursor() as cur: 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 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 num_media = cur.rowcount
logging.debug(f"Deleted {num_media} + {num_thumbnails} db entries for media id {self.media_id}") 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() files = mr.get_remote_media()
for file in files: for file in files:
if file.create_date < cleantime: if file.create_date < cleantime:
print (file.has_thumbnail(), file.base64hash)
if not file.exists(): if not file.exists():
logging.info(f"file id {file.media_id} does not physically exist (path {file.fullpath()})") logging.info(f"file id {file.media_id} does not physically exist (path {file.fullpath()})")
if not args.dryrun: if not args.dryrun:
file.delete() file.delete()