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

DeferredBackendImageProcessor::processTask()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 28
rs 9.7
cc 2
nc 2
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\Resource\Processing;
19
20
use TYPO3\CMS\Backend\Routing\UriBuilder;
21
use TYPO3\CMS\Core\Context\Context;
22
use TYPO3\CMS\Core\Imaging\ImageDimension;
23
use TYPO3\CMS\Core\Resource\ProcessedFileRepository;
24
use TYPO3\CMS\Core\Resource\Processing\ProcessorInterface;
25
use TYPO3\CMS\Core\Resource\Processing\TaskInterface;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
28
/**
29
 * @internal This class is a specific Backend controller implementation and is not considered part of the Public TYPO3 API.
30
 */
31
class DeferredBackendImageProcessor implements ProcessorInterface
32
{
33
    public function canProcessTask(TaskInterface $task): bool
34
    {
35
        $context = GeneralUtility::makeInstance(Context::class);
36
        return TYPO3_MODE === 'BE'
37
               && $task->getType() === 'Image'
38
               && in_array($task->getName(), ['Preview', 'CropScaleMask'], true)
39
               && (!$context->hasAspect('fileProcessing') || $context->getPropertyFromAspect('fileProcessing', 'deferProcessing'))
40
               && $task->getSourceFile()->getProperty('width') > 0
41
               && $task->getSourceFile()->getProperty('height') > 0
42
               // Let the local image processor update the properties in case the target file exists already
43
               && !$task->getSourceFile()->getStorage()->getProcessingFolder()->hasFile($task->getTargetFileName());
44
    }
45
46
    public function processTask(TaskInterface $task): void
47
    {
48
        $imageDimension = ImageDimension::fromProcessingTask($task);
49
        $processedFile = $task->getTargetFile();
50
        if (!$processedFile->isPersisted()) {
51
            // For now, we need to persist the processed file in the repository to be able to reference its uid
52
            // We could instead introduce a processing queue and persist the information there
53
            $processedFileRepository = GeneralUtility::makeInstance(ProcessedFileRepository::class);
54
            $processedFileRepository->add($processedFile);
55
        }
56
        $processedFile->setName($task->getTargetFileName());
57
        $processingUrl = (string)GeneralUtility::makeInstance(UriBuilder::class)
58
            ->buildUriFromRoute(
59
                'image_processing',
60
                [
61
                    'id' => $processedFile->getUid(),
62
                ]
63
            );
64
        $processedFile->updateProcessingUrl(GeneralUtility::locationHeaderUrl($processingUrl));
65
        $processedFile->updateProperties(
66
            [
67
                'width' => $imageDimension->getWidth(),
68
                'height' => $imageDimension->getHeight(),
69
                'size' => 0,
70
                'checksum' => $task->getConfigurationChecksum(),
71
            ]
72
        );
73
        $task->setExecuted(true);
74
    }
75
}
76