Adds a new function to the `IFileProcessor` which is called to check if the uploaded...
authorCyperghost <olaf_schmitz_1@t-online.de>
Wed, 10 Jul 2024 09:36:56 +0000 (11:36 +0200)
committerCyperghost <olaf_schmitz_1@t-online.de>
Wed, 10 Jul 2024 09:36:56 +0000 (11:36 +0200)
ts/WoltLabSuite/Core/Component/File/Helper.ts
wcfsetup/install/files/js/WoltLabSuite/Core/Component/File/Helper.js
wcfsetup/install/files/lib/system/endpoint/controller/core/files/upload/SaveChunk.class.php
wcfsetup/install/files/lib/system/file/processor/AbstractFileProcessor.class.php
wcfsetup/install/files/lib/system/file/processor/IFileProcessor.class.php

index 93f3f8ddd3c71dd87460d83b91c1b7bf54bc6cc8..a16c877829ff1b32d4b0879cbc94e41892acbad3 100644 (file)
@@ -51,6 +51,9 @@ export function fileInitializationFailed(element: HTMLElement, file: WoltlabCore
       case "preflight":
         errorMessage = getPhrase(`wcf.upload.error.${validationError.code}`);
         break;
+      case "validation":
+        errorMessage = getPhrase(`wcf.upload.validation.error.${validationError.code}`);
+        break;
 
       default:
         errorMessage = "Unrecognized error type: " + JSON.stringify(validationError);
index b4eb53777228ba7d0957a2c95ce2788fb0c407d7..22960c29af5decc78f172c6cd0063a93fd2d7c37 100644 (file)
@@ -43,6 +43,9 @@ define(["require", "exports", "WoltLabSuite/Core/Language", "WoltLabSuite/Core/F
                 case "preflight":
                     errorMessage = (0, Language_1.getPhrase)(`wcf.upload.error.${validationError.code}`);
                     break;
+                case "validation":
+                    errorMessage = (0, Language_1.getPhrase)(`wcf.upload.validation.error.${validationError.code}`);
+                    break;
                 default:
                     errorMessage = "Unrecognized error type: " + JSON.stringify(validationError);
                     break;
index 02f65da5a87090d4c7e5b5291adcc41270650ee4..0a03944b78fcd417ecd2da0d49691d17f973ed24 100644 (file)
@@ -3,8 +3,8 @@
 namespace wcf\system\endpoint\controller\core\files\upload;
 
 use Laminas\Diactoros\Response\JsonResponse;
-use Psr\Http\Message\ServerRequestInterface;
 use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
 use wcf\data\file\FileEditor;
 use wcf\data\file\temporary\FileTemporary;
 use wcf\data\file\temporary\FileTemporaryEditor;
@@ -109,6 +109,17 @@ final class SaveChunk implements IController
             unset($fileTemporary);
 
             $processor = $file->getProcessor();
+
+            if ($processor !== null) {
+                try {
+                    $processor->validateUpload($file);
+                } catch (UserInputException $exception) {
+                    (new FileEditor($file))->delete();
+
+                    throw new UserInputException('validation', $exception->getType());
+                }
+            }
+
             $processor?->adopt($file, $context);
 
             $generateThumbnails = false;
index 8784a477bd3713c8e29799927e21f39236c040b2..750edfeb35a1c0322ca1ed1e88d759fda2cda55f 100644 (file)
@@ -22,6 +22,12 @@ abstract class AbstractFileProcessor implements IFileProcessor
         // There are no thumbnails in the default implementation.
     }
 
+    #[\Override]
+    public function validateUpload(File $file): void
+    {
+        // There is no need to validate the uploaded file.
+    }
+
     #[\Override]
     public function countExistingFiles(array $context): ?int
     {
index 9b6d926b6a4ad0e13abefa43d09c75dc46654cf6..bb8f9b01d12ec5c9de48efcc6e3fa4c8249a2d46 100644 (file)
@@ -4,6 +4,7 @@ namespace wcf\system\file\processor;
 
 use wcf\data\file\File;
 use wcf\data\file\thumbnail\FileThumbnail;
+use wcf\system\exception\UserInputException;
 
 /**
  * File processors are responsible to validate and process any file uploads
@@ -28,6 +29,13 @@ interface IFileProcessor
      */
     public function acceptUpload(string $filename, int $fileSize, array $context): FileProcessorPreflightResult;
 
+    /**
+     * Validates the uploaded file.
+     *
+     * @throws UserInputException if the file is invalid.
+     */
+    public function validateUpload(File $file): void;
+
     /**
      * Notifies the file processor that the upload of a file has been completed
      * that belongs to this type.