TYPO3 CMS  TYPO3_8-7
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 
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  {
88  $file = ResourceFactory::getInstance()->getFileObject($fileId);
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 }
static hmac($input, $additionalSecret='')
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)