-
Notifications
You must be signed in to change notification settings - Fork 1
/
DeferredFrontendImageProcessor.php
61 lines (51 loc) · 2.46 KB
/
DeferredFrontendImageProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
declare(strict_types=1);
namespace FriendsOfTYPO3\DeferredImageProcessing\Resource\Processing;
use FriendsOfTYPO3\DeferredImageProcessing\Resource\ProcessedFileQueueRepository;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use TYPO3\CMS\Core\Configuration\Features;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Http\ApplicationType;
use TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor;
use TYPO3\CMS\Core\Resource\Processing\ProcessorInterface;
use TYPO3\CMS\Core\Resource\Processing\TaskInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class DeferredFrontendImageProcessor implements ProcessorInterface, LoggerAwareInterface
{
use LoggerAwareTrait;
public function __construct(
private readonly Features $features,
private readonly ProcessedFileQueueRepository $processedFileQueueRepository
) {
}
public function canProcessTask(TaskInterface $task): bool
{
return $this->features->isFeatureEnabled('deferredFrontendImageProcessing')
&& ($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
&& ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend()
&& $task->getType() === 'Image'
&& in_array($task->getName(), ['Preview', 'CropScaleMask'], true);
}
public function processTask(TaskInterface $task): void
{
$context = GeneralUtility::makeInstance(Context::class);
// it will be set to false by the DeferredImageProcessing middleware
// to trigger the processing of the image requested by the frontend
$isDeferredExecutionEnabled = !$context->hasAspect('fileProcessing') || $context->getPropertyFromAspect('fileProcessing', 'deferProcessing');
if (!$isDeferredExecutionEnabled) {
// in the final implementation, maybe handle different processors here as they are configured
/** @var LocalImageProcessor $localImageProcessor */
$localImageProcessor = GeneralUtility::makeInstance(LocalImageProcessor::class);
$localImageProcessor->processTask($task);
}
if (!$this->processedFileQueueRepository->isEnqueued($task)) {
if (!$task->getTargetFile()->isPersisted()) {
$task->getTargetFile()->setName($task->getTargetFileName());
}
$this->processedFileQueueRepository->enqueue($task);
}
$task->setExecuted(true);
}
}