1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-23 23:09:37 +02:00

added-fix-image-deletion (#681)

* added-fix-image-deletion

* feat(commands): add image cleanup command to find and delete unused files

* fix(models): ensure associated AdventureImages are deleted and files cleaned up on Adventure deletion

* fix(models): ensure associated Attachment files are deleted and their filesystem cleaned up on Adventure deletion

---------

Co-authored-by: ferdousahmed <taninme@gmail.com>
Co-authored-by: Sean Morley
This commit is contained in:
taninme 2025-06-23 18:48:35 -04:00 committed by GitHub
parent 295ecd1362
commit 5308ec21d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 115 additions and 1 deletions

View file

@ -656,6 +656,15 @@ class Adventure(models.Model):
return result
def delete(self, *args, **kwargs):
# Delete all associated AdventureImages first to trigger their filesystem cleanup
for image in self.images.all():
image.delete()
# Delete all associated Attachment files first to trigger their filesystem cleanup
for attachment in self.attachments.all():
attachment.delete()
super().delete(*args, **kwargs)
def __str__(self):
return self.name
@ -838,6 +847,12 @@ class AdventureImage(models.Model):
self.full_clean() # This calls clean() method
super().save(*args, **kwargs)
def delete(self, *args, **kwargs):
# Remove file from disk when deleting AdventureImage
if self.image and os.path.isfile(self.image.path):
os.remove(self.image.path)
super().delete(*args, **kwargs)
def __str__(self):
return self.image.url if self.image else f"Immich ID: {self.immich_id or 'No image'}"
@ -849,6 +864,11 @@ class Attachment(models.Model):
adventure = models.ForeignKey(Adventure, related_name='attachments', on_delete=models.CASCADE)
name = models.CharField(max_length=200, null=True, blank=True)
def delete(self, *args, **kwargs):
if self.file and os.path.isfile(self.file.path):
os.remove(self.file.path)
super().delete(*args, **kwargs)
def __str__(self):
return self.file.url