feat: clean up image files when item is deleted from catalog

- Delete photo_path and photo_thumbnail_path files on item deletion
- Handle file not found gracefully with logging
- Preserves audit logs while removing actual image files
This commit is contained in:
2026-04-22 08:54:22 +03:00
parent 092271790c
commit 09a66bd3d5

View File

@@ -276,6 +276,25 @@ def delete_item(
# [CLEANUP] Delete related InterventionItems to prevent foreign key issues
db.query(models.InterventionItem).filter(models.InterventionItem.item_id == item_id).delete()
# [CLEANUP] Delete associated image files
if db_item.photo_path:
try:
photo_file = Path(db_item.photo_path.lstrip("/"))
if photo_file.exists():
photo_file.unlink()
log.info(f"Deleted photo file: {db_item.photo_path}")
except Exception as e:
log.warning(f"Failed to delete photo file {db_item.photo_path}: {e}")
if db_item.photo_thumbnail_path:
try:
thumb_file = Path(db_item.photo_thumbnail_path.lstrip("/"))
if thumb_file.exists():
thumb_file.unlink()
log.info(f"Deleted thumbnail file: {db_item.photo_thumbnail_path}")
except Exception as e:
log.warning(f"Failed to delete thumbnail file {db_item.photo_thumbnail_path}: {e}")
# Audit Logs in database are NOT deleted here to preserve history of actions
db.delete(db_item)
db.commit()