From 29bf6af1f81d940978925c04b2f2bbba23e33cdf Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Sun, 12 Mar 2023 12:21:43 +0100 Subject: [PATCH] Asset: delete thumbnails when deleting an assets --- models/Asset.php | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/models/Asset.php b/models/Asset.php index 8b31c0f..f107b4b 100644 --- a/models/Asset.php +++ b/models/Asset.php @@ -490,9 +490,7 @@ class Asset { $db = Registry::get('db'); - if (!unlink(ASSETSDIR . '/' . $this->subdir . '/' . $this->filename)) - return false; - + // First: delete associated metadata $db->query(' DELETE FROM assets_meta WHERE id_asset = {int:id_asset}', @@ -500,6 +498,7 @@ class Asset 'id_asset' => $this->id_asset, ]); + // Second: figure out what tags to recount cardinality for $recount_tags = $db->queryValues(' SELECT id_tag FROM assets_tags @@ -517,13 +516,30 @@ class Asset Tag::recount($recount_tags); - $return = $db->query(' - DELETE FROM assets + // Third: figure out what associated thumbs to delete + $thumbs_to_delete = $db->queryValues(' + SELECT filename + FROM assets_thumbs WHERE id_asset = {int:id_asset}', [ 'id_asset' => $this->id_asset, ]); + foreach ($thumbs_to_delete as $filename) + { + $thumb_path = THUMBSDIR . '/' . $this->subdir . '/' . $filename; + if (is_file($thumb_path)) + unlink($thumb_path); + } + + $db->query(' + DELETE FROM assets_thumbs + WHERE id_asset = {int:id_asset}', + [ + 'id_asset' => $this->id_asset, + ]); + + // Reset asset ID for tags that use this asset for their thumbnail $rows = $db->query(' SELECT id_tag FROM tags @@ -541,6 +557,17 @@ class Asset } } + // Finally, delete the actual asset + if (!unlink(ASSETSDIR . '/' . $this->subdir . '/' . $this->filename)) + return false; + + $return = $db->query(' + DELETE FROM assets + WHERE id_asset = {int:id_asset}', + [ + 'id_asset' => $this->id_asset, + ]); + return $return; }