[BUGFIX] Decouple thumbnail generation from controllers
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / File / ThumbnailController.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Controller\File;
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;
20 use TYPO3\CMS\Core\Http\Response;
21 use TYPO3\CMS\Core\Resource\File;
22 use TYPO3\CMS\Core\Resource\ProcessedFile;
23 use TYPO3\CMS\Core\Resource\ResourceFactory;
24 use TYPO3\CMS\Core\Utility\ArrayUtility;
25
26 /**
27 * Class ThumbnailController
28 */
29 class ThumbnailController
30 {
31 /**
32 * @param ServerRequestInterface $request
33 * @return ResponseInterface
34 */
35 public function render(ServerRequestInterface $request): ResponseInterface
36 {
37 $fileObject = $this->getFileObjectByCombinedIdentifier($request->getQueryParams()['fileIdentifier']);
38 if (!$fileObject->isMissing()) {
39 $processingInstructions = [
40 'width' => 64,
41 'height' => 64,
42 'crop' => null,
43 ];
44 ArrayUtility::mergeRecursiveWithOverrule($processingInstructions, $request->getQueryParams()['processingInstructions']);
45 $processedImage = $fileObject->process(ProcessedFile::CONTEXT_IMAGECROPSCALEMASK, $processingInstructions);
46 $filePath = $processedImage->getForLocalProcessing(false);
47 return new Response($filePath, 200, [
48 'Content-Type' => $processedImage->getMimeType()
49 ]);
50 }
51 return new Response('', 404);
52 }
53
54 /**
55 * @param string $combinedIdentifier
56 * @return File
57 * @throws \InvalidArgumentException
58 */
59 protected function getFileObjectByCombinedIdentifier(string $combinedIdentifier): File
60 {
61 return ResourceFactory::getInstance()->getFileObjectFromCombinedIdentifier($combinedIdentifier);
62 }
63 }