Simplify the usage of the helper function
authorAlexander Ebert <ebert@woltlab.com>
Mon, 10 Jun 2024 10:03:21 +0000 (12:03 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 10 Jun 2024 10:03:21 +0000 (12:03 +0200)
wcfsetup/install/files/lib/http/Helper.class.php
wcfsetup/install/files/lib/system/endpoint/controller/core/files/DeleteFile.class.php
wcfsetup/install/files/lib/system/endpoint/controller/core/files/PostGenerateThumbnails.class.php
wcfsetup/install/files/lib/system/endpoint/controller/core/files/upload/PostChunk.class.php

index 21874e5e547fdd4e66abdb691ea24233a08f68a5..bbe5856a4ec2a5d2a4bb1ac5808902e613da4c5a 100644 (file)
@@ -171,25 +171,18 @@ final class Helper
      * field name.
      *
      * @template T
-     * @param array<string, string> $variables
      * @param string-string<T> $className
      * @return T
      * @throws UserInputException
      * @since 6.1
      */
-    public static function fetchObjectFromRequestParameter(array $variables, string $key, string $className): object
+    public static function fetchObjectFromRequestParameter(int|string $objectID, string $className): object
     {
         if (!\is_subclass_of($className, DatabaseObject::class)) {
             throw new ParentClassException($className, DatabaseObject::class);
         }
 
-        if (!isset($variables[$key])) {
-            throw new \RuntimeException(
-                "The variable '{$key}' does not appear in the request variables, please check its spelling and if it appears in the route definition.",
-            );
-        }
-
-        $dbo = new $className($variables[$key]);
+        $dbo = new $className($objectID);
         \assert($dbo instanceof DatabaseObject);
 
         if (!$dbo->getObjectID()) {
index 9c686ec1d36f6a589913a270e8e9cc294b429a98..b721dcc714befc181baf500b68007fe72f338bfc 100644 (file)
@@ -7,10 +7,10 @@ use Psr\Http\Message\ResponseInterface;
 use Psr\Http\Message\ServerRequestInterface;
 use wcf\data\file\File;
 use wcf\data\file\FileAction;
+use wcf\http\Helper;
 use wcf\system\endpoint\DeleteRequest;
 use wcf\system\endpoint\IController;
 use wcf\system\exception\PermissionDeniedException;
-use wcf\system\exception\UserInputException;
 
 #[DeleteRequest('/core/files/{id}')]
 final class DeleteFile implements IController
@@ -18,16 +18,11 @@ final class DeleteFile implements IController
     #[\Override]
     public function __invoke(ServerRequestInterface $request, array $variables): ResponseInterface
     {
-        $file = new File($variables['id']);
-        if (!$file->fileID) {
-            throw new UserInputException('id');
-        }
-
+        $file = Helper::fetchObjectFromRequestParameter($variables['id'], File::class);
         if (!$file->canDelete()) {
             throw new PermissionDeniedException();
         }
 
-        // TODO: How do we handle the cleanup of files?
         $fileAction = new FileAction([$file], 'delete');
         $fileAction->executeAction();
 
index e1cca557b0cd850d05def10e85317518193fbabe..01887f8faaf1c2800ea5fae9c60762e3885bd708 100644 (file)
@@ -7,6 +7,7 @@ use Psr\Http\Message\ServerRequestInterface;
 use Psr\Http\Message\ResponseInterface;
 use wcf\data\file\File;
 use wcf\data\file\thumbnail\FileThumbnailList;
+use wcf\http\Helper;
 use wcf\system\endpoint\IController;
 use wcf\system\endpoint\PostRequest;
 use wcf\system\exception\UserInputException;
@@ -17,10 +18,7 @@ final class PostGenerateThumbnails implements IController
 {
     public function __invoke(ServerRequestInterface $request, array $variables): ResponseInterface
     {
-        $file = new File($variables['id']);
-        if (!$file->fileID) {
-            throw new UserInputException('id');
-        }
+        $file = Helper::fetchObjectFromRequestParameter($variables['id'], File::class);
 
         FileProcessor::getInstance()->generateWebpVariant($file);
         FileProcessor::getInstance()->generateThumbnails($file);
index a8c5ce2a6012d2b84d027a0423984ff6edc53227..00e3345577456145444a66058423d128d52b4784 100644 (file)
@@ -25,7 +25,7 @@ final class PostChunk implements IController
 
     public function __invoke(ServerRequestInterface $request, array $variables): ResponseInterface
     {
-        $fileTemporary = Helper::fetchObjectFromRequestParameter($variables, 'identifier', FileTemporary::class);
+        $fileTemporary = Helper::fetchObjectFromRequestParameter($variables['identifier'], FileTemporary::class);
 
         $checksum = $request->getHeaderLine('chunk-checksum-sha256');
         if ($checksum === '' || \str_contains($checksum, ',')) {