Add the ability to report damaged images
authorAlexander Ebert <ebert@woltlab.com>
Fri, 20 Dec 2024 09:53:52 +0000 (10:53 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 20 Dec 2024 09:53:52 +0000 (10:53 +0100)
wcfsetup/install/files/lib/event/file/GenerateThumbnail.class.php
wcfsetup/install/files/lib/event/file/GenerateWebpVariant.class.php
wcfsetup/install/files/lib/system/file/processor/FileProcessor.class.php

index 790507c36937a0f3de2c0cf86d8133136fee8ad2..eaa66057a293ed4adf76f8fcb6f12dae3e0fb0ba 100644 (file)
@@ -17,6 +17,7 @@ use wcf\system\file\processor\ThumbnailFormat;
 final class GenerateThumbnail implements IPsr14Event
 {
     private string $pathname;
+    private bool $sourceIsDamaged = false;
 
     public function __construct(
         public readonly File $file,
@@ -50,4 +51,18 @@ final class GenerateThumbnail implements IPsr14Event
     {
         return $this->pathname ?? null;
     }
+
+    /**
+     * Flags the source image as damaged which should stop further processing
+     * of this file.
+     */
+    public function markSourceAsDamaged(): void
+    {
+        $this->sourceIsDamaged = true;
+    }
+
+    public function sourceIsMarkedAsDamaged(): bool
+    {
+        return $this->sourceIsDamaged;
+    }
 }
index c7f180a437a9b0216596ef9a1c54be5ccd8bb45f..3ef63b21e2216f6f4eb59f030fa0383a63f71e2c 100644 (file)
@@ -17,6 +17,7 @@ use wcf\event\IPsr14Event;
 final class GenerateWebpVariant implements IPsr14Event
 {
     private string $pathname;
+    private bool $sourceIsDamaged = false;
 
     public function __construct(
         public readonly File $file
@@ -49,4 +50,18 @@ final class GenerateWebpVariant implements IPsr14Event
     {
         return $this->pathname ?? null;
     }
+
+    /**
+     * Flags the source image as damaged which should stop further processing
+     * of this file.
+     */
+    public function markSourceAsDamaged(): void
+    {
+        $this->sourceIsDamaged = true;
+    }
+
+    public function sourceIsMarkedAsDamaged(): bool
+    {
+        return $this->sourceIsDamaged;
+    }
 }
index cbd3b336e474b668d99546cba471d0a04afc81c9..cafc54505914e9c3ba755c88df6600e5613bb31e 100644 (file)
@@ -164,6 +164,9 @@ final class FileProcessor extends SingletonFactory
 
         $event = new GenerateWebpVariant($file);
         EventHandler::getInstance()->fire($event);
+        if ($event->sourceIsMarkedAsDamaged()) {
+            throw new DamagedImage($file->fileID);
+        }
 
         $filename = $event->getPathname();
         if ($filename === null) {
@@ -262,6 +265,9 @@ final class FileProcessor extends SingletonFactory
 
             $event = new GenerateThumbnail($file, $format);
             EventHandler::getInstance()->fire($event);
+            if ($event->sourceIsMarkedAsDamaged()) {
+                throw new DamagedImage($file->fileID);
+            }
 
             $filename = $event->getPathname();
             if ($filename === null) {