-
Notifications
You must be signed in to change notification settings - Fork 1
/
DeferredImageProcessing.php
72 lines (59 loc) · 2.92 KB
/
DeferredImageProcessing.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
62
63
64
65
66
67
68
69
70
71
72
<?php
declare(strict_types=1);
namespace FriendsOfTYPO3\DeferredImageProcessing\Middleware;
use FriendsOfTYPO3\DeferredImageProcessing\Resource\Dto\QueuedTask;
use FriendsOfTYPO3\DeferredImageProcessing\Resource\ProcessedFileQueueRepository;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Configuration\Features;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\FileProcessingAspect;
use TYPO3\CMS\Core\Imaging\ImageManipulation\Area;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
readonly class DeferredImageProcessing implements MiddlewareInterface
{
public function __construct(private Features $features)
{
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$path = $request->getUri()->getPath();
$inputFormat = strtolower(pathinfo($path, PATHINFO_EXTENSION));
if (
!GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $inputFormat)
|| !$this->features->isFeatureEnabled('deferredFrontendImageProcessing')
) {
return $handler->handle($request);
}
/** @var ProcessedFileQueueRepository $processedFileQueueRepository */
$processedFileQueueRepository = GeneralUtility::makeInstance(ProcessedFileQueueRepository::class);
$queuedTask = $processedFileQueueRepository->findByPublicUrl($path);
if ($queuedTask instanceof QueuedTask === false) {
return $handler->handle($request);
}
$storage = GeneralUtility::makeInstance(ResourceFactory::class)->getStorageObject($queuedTask->getStorage());
// set fileProcessing aspect to inform the file processor to not defer processing
$context = GeneralUtility::makeInstance(Context::class);
$context->setAspect('fileProcessing', new FileProcessingAspect(false));
$processedFile = $storage->processFile(
GeneralUtility::makeInstance(ResourceFactory::class)->getFileObject($queuedTask->getOriginal()),
$queuedTask->getTaskType(),
$queuedTask->getConfiguration()
);
if ($processedFile->exists()) {
$response = GeneralUtility::makeInstance(ResponseFactoryInterface::class)->createResponse();
$processedFileQueueRepository->dequeue($queuedTask->getUid());
$response = $response
->withStatus(200)
->withHeader('Content-type', $processedFile->getMimeType())
->withHeader('Content-length', (string)$processedFile->getSize());
$response->getBody()->write($processedFile->getContents());
return $response;
}
return $handler->handle($request);
}
}