From db99ea33e9f79672150408c306d2cccb7d4e3796 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Sun, 7 Apr 2024 09:39:50 +1200 Subject: [PATCH] handle missing files --- app/models.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/app/models.py b/app/models.py index fac1e213..4981498e 100644 --- a/app/models.py +++ b/app/models.py @@ -208,14 +208,23 @@ class File(db.Model): def delete_from_disk(self): purge_from_cache = [] if self.file_path and os.path.isfile(self.file_path): - os.unlink(self.file_path) + try: + os.unlink(self.file_path) + except FileNotFoundError as e: + ... purge_from_cache.append(self.file_path.replace('app/', f"https://{current_app.config['SERVER_NAME']}/")) if self.thumbnail_path and os.path.isfile(self.thumbnail_path): - os.unlink(self.thumbnail_path) + try: + os.unlink(self.thumbnail_path) + except FileNotFoundError as e: + ... purge_from_cache.append(self.thumbnail_path.replace('app/', f"https://{current_app.config['SERVER_NAME']}/")) if self.source_url and self.source_url.startswith('http') and current_app.config['SERVER_NAME'] in self.source_url: # self.source_url is always a url rather than a file path, which makes deleting the file a bit fiddly - os.unlink(self.source_url.replace(f"https://{current_app.config['SERVER_NAME']}/", 'app/')) + try: + os.unlink(self.source_url.replace(f"https://{current_app.config['SERVER_NAME']}/", 'app/')) + except FileNotFoundError as e: + ... purge_from_cache.append(self.source_url) # otoh it makes purging the cdn cache super easy. if purge_from_cache: