‪TYPO3CMS  9.5
ThumbnailController.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
25 
30 {
34  protected ‪$defaultConfiguration = [
35  'width' => 64,
36  'height' => 64,
37  'crop' => null,
38  ];
39 
44  public function ‪render(ServerRequestInterface $request): ResponseInterface
45  {
46  try {
47  $parameters = $this->‪extractParameters($request->getQueryParams());
48  $response = $this->‪generateThumbnail(
49  $parameters['fileId'] ?? null,
50  $parameters['configuration'] ?? []
51  );
52  } catch (\‪TYPO3\CMS\Core\Resource\‪Exception $exception) {
53  // catch and handle only resource related exceptions
54  $response = $this->‪generateNotFoundResponse();
55  }
56 
57  return $response;
58  }
59 
64  protected function ‪extractParameters(array $queryParameters)
65  {
66  $expectedHash = GeneralUtility::hmac(
67  $queryParameters['parameters'] ?? '',
68  ThumbnailController::class
69  );
70  if (!hash_equals($expectedHash, $queryParameters['hmac'] ?? '')) {
71  throw new \InvalidArgumentException(
72  'HMAC could not be verified',
73  1534484203
74  );
75  }
76 
77  return json_decode($queryParameters['parameters'] ?? null, true);
78  }
79 
86  protected function ‪generateThumbnail($fileId, array $configuration): ResponseInterface
87  {
89  if (empty($file) || $file->isMissing()) {
90  return $this->‪generateNotFoundResponse();
91  }
92 
93  $processingConfiguration = ‪$this->defaultConfiguration;
95  $processingConfiguration,
96  $configuration
97  );
98 
99  $processedImage = $file->process(
101  $processingConfiguration
102  );
103  $filePath = $processedImage->getForLocalProcessing(false);
104  return new ‪Response($filePath, 200, [
105  'Content-Type' => $processedImage->getMimeType()
106  ]);
107  }
108 
112  protected function ‪generateNotFoundResponse(): ResponseInterface
113  {
114  return new ‪Response('', 404);
115  }
116 }
‪TYPO3\CMS\Core\Resource\ProcessedFile\CONTEXT_IMAGECROPSCALEMASK
‪const CONTEXT_IMAGECROPSCALEMASK
Definition: ProcessedFile.php:56
‪TYPO3\CMS\Backend\Controller\File\ThumbnailController\$defaultConfiguration
‪array $defaultConfiguration
Definition: ThumbnailController.php:33
‪TYPO3
‪TYPO3\CMS\Backend\Exception
Definition: Exception.php:23
‪TYPO3\CMS\Core\Resource\ResourceFactory\getInstance
‪static ResourceFactory getInstance()
Definition: ResourceFactory.php:39
‪TYPO3\CMS\Backend\Controller\File\ThumbnailController\generateNotFoundResponse
‪ResponseInterface generateNotFoundResponse()
Definition: ThumbnailController.php:111
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:614
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:28
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:33
‪TYPO3\CMS\Backend\Controller\File\ThumbnailController\generateThumbnail
‪Response generateThumbnail($fileId, array $configuration)
Definition: ThumbnailController.php:85
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:42
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:23
‪TYPO3\CMS\Core\Resource\ResourceFactory\getFileObject
‪File getFileObject($uid, array $fileData=[])
Definition: ResourceFactory.php:399
‪TYPO3\CMS\Backend\Controller\File
Definition: CreateFolderController.php:3
‪TYPO3\CMS\Backend\Controller\File\ThumbnailController\render
‪ResponseInterface render(ServerRequestInterface $request)
Definition: ThumbnailController.php:43
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\Controller\File\ThumbnailController
Definition: ThumbnailController.php:30
‪TYPO3\CMS\Backend\Controller\File\ThumbnailController\extractParameters
‪array null extractParameters(array $queryParameters)
Definition: ThumbnailController.php:63