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
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;
}
{
$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);
--- /dev/null
+<?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,
+ ),
+ );
+ }
+}
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.
*/