Passed
Push — master ( 811217...98b68e )
by
unknown
12:07
created

ImageProcessController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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
18
namespace TYPO3\CMS\Backend\Controller\File;
19
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
use Psr\Log\LoggerAwareInterface;
23
use Psr\Log\LoggerAwareTrait;
24
use TYPO3\CMS\Core\Http\HtmlResponse;
25
use TYPO3\CMS\Core\Http\RedirectResponse;
26
use TYPO3\CMS\Core\Resource\Service\ImageProcessingService;
27
use TYPO3\CMS\Core\Utility\GeneralUtility;
28
29
/**
30
 * @internal This class is a specific Backend controller implementation and is not considered part of the Public TYPO3 API.
31
 */
32
class ImageProcessController implements LoggerAwareInterface
33
{
34
    use LoggerAwareTrait;
35
36
    /**
37
     * @var ImageProcessingService
38
     */
39
    private $imageProcessingService;
40
41
    public function __construct(ImageProcessingService $imageProcessingService)
42
    {
43
        $this->imageProcessingService = $imageProcessingService;
44
    }
45
46
    /**
47
     * @param ServerRequestInterface $request
48
     * @return ResponseInterface
49
     */
50
    public function process(ServerRequestInterface $request): ResponseInterface
51
    {
52
        $processedFileId = (int)($request->getQueryParams()['id'] ?? 0);
53
        try {
54
            $processedFile = $this->imageProcessingService->process($processedFileId);
55
56
            return new RedirectResponse(
57
                GeneralUtility::locationHeaderUrl($processedFile->getPublicUrl(true))
58
            );
59
        } catch (\Throwable $e) {
60
            // Fatal error occurred, which will be responded as 404
61
            $this->logger->error(sprintf('Processing of file with id "%s" failed', $processedFileId), ['exception' => $e]);
62
        }
63
64
        return new HtmlResponse('', 404);
65
    }
66
}
67