From 78c5c4b08454f0bbbf106b8cf98d2467c3c78c19 Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Thu, 12 Aug 2021 00:25:25 +0100 Subject: [PATCH] [Attachment] Allow to delete the associated file --- src/Entity/Attachment.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Entity/Attachment.php b/src/Entity/Attachment.php index 3c5226b5d6..bf8425541f 100644 --- a/src/Entity/Attachment.php +++ b/src/Entity/Attachment.php @@ -71,7 +71,6 @@ class Attachment extends Entity /** * @return int */ - public function getLives(): int { return $this->lives; @@ -205,6 +204,28 @@ class Attachment extends Entity return true; } + /** + * Attachment delete always removes dependencies, cleanups and flushes + */ + public function deleteStorage(): bool + { + if (!is_null($filepath = $this->getPath())) { + if (file_exists($filepath)) { + if (@unlink($filepath) === false) { + Log::error("Failed deleting file for attachment with id={$this->getId()} at {$filepath}."); + return false; + } else { + $this->setFilename(null); + DB::persist($this); + DB::flush(); + } + } else { + Log::warning("File for attachment with id={$this->getId()} at {$filepath} was already deleted when I was going to handle it."); + } + } + return true; + } + /** * Attachment delete always removes dependencies, cleanups and flushes */