Properly delete files
This commit is contained in:
parent
be6da99c1c
commit
7065a5437e
21
cleanmedia
21
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()
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user