]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Clear out stored files and reltaed thumbnails when a File is deleted.
authorMikael Nordfeldth <mmn@hethane.se>
Mon, 12 May 2014 13:16:41 +0000 (15:16 +0200)
committerMikael Nordfeldth <mmn@hethane.se>
Mon, 12 May 2014 13:16:41 +0000 (15:16 +0200)
EVENTS.txt
classes/File.php

index 6e32042453895261b9537884224bdb604c21e62a..fffaa20fd06d1a6f7cf032a0eca858a76a876cbc 100644 (file)
@@ -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 <title> element
 - $action: action being shown
 
index cafbae8fb0cab5b73936c8aa3644fb5e78c80c22..6db1c7b806c79a5f6219d7dcbdacb10db5d885e8 100644 (file)
@@ -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);
+    }
 }