‪TYPO3CMS  10.4
ThumbnailController.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
31 
36 {
40  protected ‪$defaultConfiguration = [
41  'width' => 64,
42  'height' => 64,
43  'crop' => null,
44  ];
45 
50  public function ‪render(ServerRequestInterface $request): ResponseInterface
51  {
52  try {
53  $parameters = $this->‪extractParameters($request->getQueryParams());
54  $response = $this->‪generateThumbnail(
55  $parameters['fileId'] ?? null,
56  $parameters['configuration'] ?? []
57  );
58  } catch (‪Exception $exception) {
59  // catch and handle only resource related exceptions
60  $response = $this->‪generateNotFoundResponse();
61  }
62 
63  return $response;
64  }
65 
70  protected function ‪extractParameters(array $queryParameters)
71  {
72  $expectedHash = GeneralUtility::hmac(
73  $queryParameters['parameters'] ?? '',
74  ThumbnailController::class
75  );
76  if (!hash_equals($expectedHash, $queryParameters['hmac'] ?? '')) {
77  throw new \InvalidArgumentException(
78  'HMAC could not be verified',
79  1534484203
80  );
81  }
82 
83  return json_decode($queryParameters['parameters'] ?? null, true);
84  }
85 
92  protected function ‪generateThumbnail($fileId, array $configuration): ResponseInterface
93  {
94  $file = GeneralUtility::makeInstance(ResourceFactory::class)->getFileObject($fileId);
95  if ($file === null || $file->isMissing()) {
96  return $this->‪generateNotFoundResponse();
97  }
98 
99  $context = $configuration['_context'] ?? ‪ProcessedFile::CONTEXT_IMAGEPREVIEW;
100  unset($configuration['_context']);
101 
102  $processingConfiguration = ‪$this->defaultConfiguration;
104  $processingConfiguration,
105  $configuration
106  );
107 
108  $processedImage = $file->process(
109  $context,
110  $processingConfiguration
111  );
112  if ($processedImage->isImage()) {
113  return new ‪RedirectResponse(
114  GeneralUtility::locationHeaderUrl($processedImage->getPublicUrl(true) ?? '')
115  );
116  }
117 
118  $iconIdentifier = GeneralUtility::makeInstance(IconFactory::class)
119  ->getIconForResource($processedImage->getOriginalFile())->getIdentifier();
120  $fileName = 'EXT:core/Resources/Public/Icons/T3Icons/mimetypes/' . $iconIdentifier . '.svg';
121  $file = GeneralUtility::getFileAbsFileName($fileName);
122  if (file_exists($file)) {
123  return new ‪RedirectResponse(
124  GeneralUtility::locationHeaderUrl(‪PathUtility::getAbsoluteWebPath($file))
125  );
126  }
127 
128  return $this->‪generateNotFoundResponse();
129  }
130 
134  protected function ‪generateNotFoundResponse(): ResponseInterface
135  {
136  return new ‪HtmlResponse('', 404);
137  }
138 }
‪TYPO3\CMS\Core\Resource\ProcessedFile\CONTEXT_IMAGEPREVIEW
‪const CONTEXT_IMAGEPREVIEW
Definition: ProcessedFile.php:52
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:24
‪TYPO3\CMS\Backend\Controller\File\ThumbnailController\$defaultConfiguration
‪array $defaultConfiguration
Definition: ThumbnailController.php:39
‪TYPO3\CMS\Backend\Controller\File\ThumbnailController\generateNotFoundResponse
‪ResponseInterface generateNotFoundResponse()
Definition: ThumbnailController.php:133
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:33
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:654
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:28
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:44
‪TYPO3\CMS\Core\Resource\Exception
Definition: Exception.php:22
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Backend\Controller\File\ThumbnailController\generateThumbnail
‪ResponseInterface generateThumbnail($fileId, array $configuration)
Definition: ThumbnailController.php:91
‪TYPO3\CMS\Backend\Controller\File
Definition: CreateFolderController.php:18
‪TYPO3\CMS\Backend\Controller\File\ThumbnailController\render
‪ResponseInterface render(ServerRequestInterface $request)
Definition: ThumbnailController.php:49
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath($targetPath)
Definition: PathUtility.php:43
‪TYPO3\CMS\Backend\Controller\File\ThumbnailController
Definition: ThumbnailController.php:36
‪TYPO3\CMS\Core\Resource\Exception
Definition: AbstractFileOperationException.php:16
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:26
‪TYPO3\CMS\Backend\Controller\File\ThumbnailController\extractParameters
‪array null extractParameters(array $queryParameters)
Definition: ThumbnailController.php:69