Clean up files on delete
authorAlexander Ebert <ebert@woltlab.com>
Wed, 8 May 2024 10:02:15 +0000 (12:02 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sat, 8 Jun 2024 10:19:39 +0000 (12:19 +0200)
wcfsetup/install/files/lib/data/file/File.class.php
wcfsetup/install/files/lib/data/file/FileEditor.class.php
wcfsetup/install/files/lib/data/file/thumbnail/FileThumbnailEditor.class.php

index 4e5b8d02a48c3a0a854bd07b413aec67853c6ac1..9ad660d93d6a8c69bacd6aee5eb62c4f743a7836 100644 (file)
@@ -138,6 +138,14 @@ class File extends DatabaseObject
         return $this->thumbnails[$identifier] ?? null;
     }
 
+    /**
+     * @return array<string, FileThumbnail>
+     */
+    public function getThumbnails(): array
+    {
+        return $this->thumbnails;
+    }
+
     public function toHtmlElement(array $metaData): string
     {
         $thumbnails = [];
index 5d3a42d249c93c3e3d5538599c52bf8ddb29da4d..940edce3412eada391c81664c6f1720e56d0ff51 100644 (file)
@@ -4,6 +4,8 @@ namespace wcf\data\file;
 
 use wcf\data\DatabaseObjectEditor;
 use wcf\data\file\temporary\FileTemporary;
+use wcf\data\file\thumbnail\FileThumbnailEditor;
+use wcf\data\file\thumbnail\FileThumbnailList;
 use wcf\util\FileUtil;
 
 /**
@@ -23,6 +25,40 @@ class FileEditor extends DatabaseObjectEditor
      */
     protected static $baseClass = File::class;
 
+    public function deleteFiles(): void
+    {
+        @\unlink($this->getPathname());
+
+        $thumbnailIDs = \array_column($this->getThumbnails(), 'thumbnailID');
+        if ($thumbnailIDs !== []) {
+            FileThumbnailEditor::deleteAll($thumbnailIDs);
+        }
+    }
+
+    public static function deleteAll(array $objectIDs = [])
+    {
+        $fileList = new FileList();
+        $fileList->getConditionBuilder()->add("fileID IN (?)", [$objectIDs]);
+        $fileList->readObjects();
+        $files = $fileList->getObjects();
+        if (\count($files) === 0) {
+            return 0;
+        }
+
+        $thumbnailList = new FileThumbnailList();
+        $thumbnailList->getConditionBuilder()->add("fileID IN (?)", [$objectIDs]);
+        $thumbnailList->readObjects();
+        foreach ($thumbnailList as $thumbnail) {
+            $files[$thumbnail->fileID]->addThumbnail($thumbnail);
+        }
+
+        foreach ($files as $file) {
+            (new FileEditor($file))->deleteFiles();
+        }
+
+        return parent::deleteAll($objectIDs);
+    }
+
     public static function createFromTemporary(FileTemporary $fileTemporary): File
     {
         $pathname = $fileTemporary->getPathname();
index 2f34c47bc2d7c86c103f5db0553bb89538da5a0b..c3ffabaf2cf593a3e4858ac90ab1cc40b16481ea 100644 (file)
@@ -5,7 +5,6 @@ namespace wcf\data\file\thumbnail;
 use wcf\data\DatabaseObjectEditor;
 use wcf\data\file\File;
 use wcf\system\file\processor\ThumbnailFormat;
-use wcf\util\FileUtil;
 
 /**
  * @author Alexander Ebert
@@ -24,6 +23,28 @@ class FileThumbnailEditor extends DatabaseObjectEditor
      */
     protected static $baseClass = FileThumbnail::class;
 
+    public function deleteFiles(): void
+    {
+        @\unlink($this->getPath() . $this->getSourceFilename());
+    }
+
+    public static function deleteAll(array $objectIDs = [])
+    {
+        $thumbnailList = new FileThumbnailList();
+        $thumbnailList->getConditionBuilder()->add("thumbnailID IN (?)", [$objectIDs]);
+        $thumbnailList->readObjects();
+
+        if (\count($thumbnailList) === 0) {
+            return 0;
+        }
+
+        foreach ($thumbnailList as $thumbnail) {
+            (new FileThumbnailEditor($thumbnail))->deleteFiles();
+        }
+
+        return parent::deleteAll($objectIDs);
+    }
+
     public static function createFromTemporaryFile(
         File $file,
         ThumbnailFormat $format,