Clean up some TODOs
authorAlexander Ebert <ebert@woltlab.com>
Sat, 13 Apr 2024 22:11:49 +0000 (00:11 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sat, 8 Jun 2024 10:19:38 +0000 (12:19 +0200)
wcfsetup/install/files/lib/system/file/processor/AttachmentFileProcessor.class.php
wcfsetup/install/files/lib/system/file/processor/exception/UnexpectedThumbnailIdentifier.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/util/FileUtil.class.php

index 65e62d4e0f1de2d3db7f4b0564b03e944b983a08..598b2121409dfa69e61c6043a801bcc4531b63cd 100644 (file)
@@ -9,6 +9,8 @@ use wcf\data\file\File;
 use wcf\data\file\thumbnail\FileThumbnail;
 use wcf\http\Helper;
 use wcf\system\attachment\AttachmentHandler;
+use wcf\system\file\processor\exception\UnexpectedThumbnailIdentifier;
+use wcf\util\FileUtil;
 
 /**
  * @author Alexander Ebert
@@ -70,21 +72,7 @@ final class AttachmentFileProcessor implements IFileProcessor
             return FileProcessorPreflightResult::FileSizeTooLarge;
         }
 
-        // TODO: This is a typical use case and should be provided through a helper function.
-        $extensions = \implode(
-            "|",
-            \array_map(
-                static function (string $extension) {
-                    $extension = \preg_quote($extension, '/');
-                    $extension = \str_replace('\*', '.*', $extension);
-
-                    return $extension;
-                },
-                $attachmentHandler->getAllowedExtensions()
-            )
-        );
-        $extensionsPattern = '/(' . $extensions . ')$/i';
-        if (!\preg_match($extensionsPattern, \mb_strtolower($filename))) {
+        if (!FileUtil::endsWithAllowedExtension($filename, $attachmentHandler->getAllowedExtensions())) {
             return FileProcessorPreflightResult::FileExtensionNotPermitted;
         }
 
@@ -163,14 +151,16 @@ final class AttachmentFileProcessor implements IFileProcessor
     {
         $attachment = Attachment::findByFileID($thumbnail->fileID);
         if ($attachment === null) {
-            // TODO: How to handle this case?
+            // The associated attachment (or file) has vanished while the
+            // thumbnail was being created. There is nothing to do here, it will
+            // cleaned up eventually.
             return;
         }
 
         $columnName = match ($thumbnail->identifier) {
             '' => 'thumbnailID',
             'tiny' => 'tinyThumbnailID',
-            'default' => throw new \RuntimeException('TODO'), // TODO
+            'default' => throw new UnexpectedThumbnailIdentifier($thumbnail->identifier),
         };
 
         $attachmentEditor = new AttachmentEditor($attachment);
diff --git a/wcfsetup/install/files/lib/system/file/processor/exception/UnexpectedThumbnailIdentifier.class.php b/wcfsetup/install/files/lib/system/file/processor/exception/UnexpectedThumbnailIdentifier.class.php
new file mode 100644 (file)
index 0000000..426a11a
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+
+namespace wcf\system\file\processor\exception;
+
+/**
+ * @author Alexander Ebert
+ * @copyright 2001-2024 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since 6.1
+ */
+final class UnexpectedThumbnailIdentifier extends \Exception
+{
+    public function __construct(string $identifier)
+    {
+        parent::__construct(
+            \sprintf(
+                "The thumbnail identifier '%s' is unsupported for this type.",
+                $identifier,
+            ),
+        );
+    }
+}
index e7a86de3e55f311adb911bbac94069c015832435..df9a58bf287ead6f57c40684f713600e2b812864 100644 (file)
@@ -633,6 +633,33 @@ final class FileUtil
         return !!\preg_match('/^\.?(php[0-9]*|phtml)$/i', $extension);
     }
 
+    /**
+     * @param list<string> $allowedExtensions
+     * @since 6.1
+     */
+    public static function endsWithAllowedExtension(string $filename, array $allowedExtensions): bool
+    {
+        $extensions = \implode(
+            "|",
+            \array_map(
+                static function (string $extension) {
+                    $extension = \preg_quote($extension, '/');
+                    $extension = \str_replace('\*', '.*', $extension);
+
+                    return $extension;
+                },
+                $allowedExtensions
+            )
+        );
+
+        $extensionsPattern = '/(' . $extensions . ')$/i';
+        if (!\preg_match($extensionsPattern, $filename)) {
+            return false;
+        }
+
+        return true;
+    }
+
     /**
      * Forbid creation of FileUtil objects.
      */