From 2755e23707d04f9088208dc0aab4451b4482e123 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Mon, 12 May 2014 15:16:41 +0200 Subject: [PATCH] Clear out stored files and reltaed thumbnails when a File is deleted. --- EVENTS.txt | 3 +++ classes/File.php | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/EVENTS.txt b/EVENTS.txt index 6e32042453..fffaa20fd0 100644 --- a/EVENTS.txt +++ b/EVENTS.txt @@ -924,6 +924,9 @@ EndRssEntryArray: at the end of copying a notice to an array NoticeDeleteRelated: at the beginning of deleting related fields to a notice - $notice: notice being deleted +FileDeleteRelated: at the beginning of deleting related fields to a File +- $notice: File being deleted + StartShowHeadTitle: when beginning to show the element - $action: action being shown diff --git a/classes/File.php b/classes/File.php index cafbae8fb0..6db1c7b806 100644 --- a/classes/File.php +++ b/classes/File.php @@ -509,4 +509,29 @@ class File extends Managed_DataObject { return !empty($this->filename); } + + public function delete($useWhere=false) + { + // Delete the file, if it exists locally + if (!empty($this->filename) && file_exists(self::path($this->filename))) { + $deleted = @unlink(self::path($this->filename)); + if (!$deleted) { + common_log(LOG_ERR, sprintf('Could not unlink existing file: "%s"', self::path($this->filename))); + } + } + + // Clear out related things in the database and filesystem, such as thumbnails + if (Event::handle('FileDeleteRelated', array($this))) { + $thumbs = new File_thumbnail(); + $thumbs->file_id = $this->id; + if ($thumbs->find()) { + while ($thumbs->fetch()) { + $thumbs->delete(); + } + } + } + + // And finally remove the entry from the database + return parent::delete($useWhere); + } }