‪TYPO3CMS  ‪main
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 
46  public function ‪render(ServerRequestInterface $request): ResponseInterface
47  {
48  try {
49  $parameters = $this->‪extractParameters($request->getQueryParams());
50  $response = $this->‪generateThumbnail(
51  $parameters['fileId'] ?? null,
52  $parameters['configuration'] ?? []
53  );
54  } catch (‪Exception $exception) {
55  // catch and handle only resource related exceptions
56  $response = $this->‪generateNotFoundResponse();
57  }
58 
59  return $response;
60  }
61 
65  protected function ‪extractParameters(array $queryParameters)
66  {
67  $expectedHash = ‪GeneralUtility::hmac(
68  $queryParameters['parameters'] ?? '',
69  ThumbnailController::class
70  );
71  if (!hash_equals($expectedHash, $queryParameters['hmac'] ?? '')) {
72  throw new \InvalidArgumentException(
73  'HMAC could not be verified',
74  1534484203
75  );
76  }
77 
78  return json_decode($queryParameters['parameters'] ?? null, true);
79  }
80 
85  protected function ‪generateThumbnail($fileId, array $configuration): ResponseInterface
86  {
87  $file = GeneralUtility::makeInstance(ResourceFactory::class)->getFileObject($fileId);
88  if ($file->isMissing()) {
89  return $this->‪generateNotFoundResponse();
90  }
91 
92  $context = $configuration['_context'] ?? ‪ProcessedFile::CONTEXT_IMAGEPREVIEW;
93  unset($configuration['_context']);
94 
95  $processingConfiguration = ‪$this->defaultConfiguration;
96  ArrayUtility::mergeRecursiveWithOverrule(
97  $processingConfiguration,
98  $configuration
99  );
100 
101  $processedImage = $file->process(
102  $context,
103  $processingConfiguration
104  );
105  if ($processedImage->isImage()) {
106  return new RedirectResponse(
107  GeneralUtility::locationHeaderUrl($processedImage->getPublicUrl() ?? '')
108  );
109  }
110 
111  $iconIdentifier = GeneralUtility::makeInstance(IconFactory::class)
112  ->getIconForResource($processedImage->getOriginalFile())->getIdentifier();
113  $fileName = 'EXT:core/Resources/Public/Icons/T3Icons/mimetypes/' . $iconIdentifier . '.svg';
114  $file = GeneralUtility::getFileAbsFileName($fileName);
115  if (file_exists($file)) {
116  return new RedirectResponse(
117  GeneralUtility::locationHeaderUrl(‪PathUtility::getPublicResourceWebPath($fileName))
118  );
119  }
120 
121  return $this->‪generateNotFoundResponse();
122  }
123 
124  protected function ‪generateNotFoundResponse(): ResponseInterface
125  {
126  return new ‪HtmlResponse('', 404);
127  }
128 }
‪TYPO3\CMS\Core\Resource\ProcessedFile\CONTEXT_IMAGEPREVIEW
‪const CONTEXT_IMAGEPREVIEW
Definition: ProcessedFile.php:55
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Backend\Controller\File\ThumbnailController\render
‪render(ServerRequestInterface $request)
Definition: ThumbnailController.php:45
‪TYPO3\CMS\Backend\Controller\File\ThumbnailController\$defaultConfiguration
‪array $defaultConfiguration
Definition: ThumbnailController.php:39
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Core\Utility\PathUtility\getPublicResourceWebPath
‪static getPublicResourceWebPath(string $resourcePath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:97
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Utility\GeneralUtility\hmac
‪static string hmac($input, $additionalSecret='')
Definition: GeneralUtility.php:475
‪TYPO3\CMS\Backend\Controller\File\ThumbnailController\generateNotFoundResponse
‪generateNotFoundResponse()
Definition: ThumbnailController.php:123
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:30
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:47
‪TYPO3\CMS\Core\Resource\Exception
Definition: Exception.php:22
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:26
‪TYPO3\CMS\Backend\Controller\File\ThumbnailController\generateThumbnail
‪generateThumbnail($fileId, array $configuration)
Definition: ThumbnailController.php:84
‪TYPO3\CMS\Backend\Controller\File
Definition: FileController.php:18
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪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:28
‪TYPO3\CMS\Backend\Controller\File\ThumbnailController\extractParameters
‪array null extractParameters(array $queryParameters)
Definition: ThumbnailController.php:64