handle missing files

This commit is contained in:
rimu 2024-04-07 09:39:50 +12:00
parent 66d05ea860
commit db99ea33e9

View file

@ -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: