diff --git a/Classes/Middleware/AssetMiddleware.php b/Classes/Middleware/AssetMiddleware.php new file mode 100644 index 0000000..6bf2303 --- /dev/null +++ b/Classes/Middleware/AssetMiddleware.php @@ -0,0 +1,108 @@ +imageService = $imageService; + } + + public function injectResponseFactory(ResponseFactoryInterface $responseFactory) + { + $this->responseFactory = $responseFactory; + } + + public function injectResourceFactory(ResourceFactory $resourceFactory) + { + $this->resourceFactory = $resourceFactory; + } + + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + /** @var \TYPO3\CMS\Core\Routing\SiteRouteResult $routing */ + $routing = $request->getAttribute('routing'); + $path = $routing->getUri()->getPath(); + $queryParams = $request->getQueryParams(); + $uid = $queryParams['uid'] ?? null; + if ($path === '/asset' && $uid) { + $resource = $this->resourceFactory->getFileReferenceObject($uid); + + $tstamp = intval($resource->getProperty('tstamp')); + $lastModified = gmdate('D, d M Y H:i:s', $tstamp) . ' GMT'; + $ifModifiedSince = $request->getHeader('if-modified-since')[0] ?? null; + + if ($lastModified === $ifModifiedSince) { + return $this->responseFactory->createResponse(304); + } + + $type = (int) $resource->getProperty('type'); + + $processedResource = $resource; + + if ($type === AbstractFile::FILETYPE_IMAGE) { + $targetFileExtension = $queryParams['fileExtension'] ?? null; + + // Skip processing for SVGs without changing image type + if ($resource->getExtension() !== 'svg' || (!$targetFileExtension || $targetFileExtension === 'svg')) { + $cropVariant = $queryParams['breakpoint'] ?? 'default'; + + $crop = $resource->getProperty('crop'); + $cropVariantCollection = CropVariantCollection::create($crop); + $cropArea = $cropVariantCollection->getCropArea($cropVariant); + + // Use default cropVariant if breakpoint cropVariant does not exist + if ($cropArea == Area::createEmpty()) { + $cropArea = $cropVariantCollection->getCropArea(); + } + + $processingInstructions = [ + 'width' => $queryParams['width'] ?? null, + 'height' => $queryParams['height'] ?? null, + 'maxWidth' => $queryParams['maxWidth'] ?? null, + 'maxHeight' => $queryParams['maxHeight'] ?? null, + 'fileExtension' => $queryParams['fileExtension'] ?? null, + 'crop' => $cropArea->makeAbsoluteBasedOnFile($resource), + ]; + + $processedResource = $this->imageService->applyProcessingInstructions($resource, $processingInstructions); + } + } + + $mimeType = $processedResource->getMimeType(); + $contents = $processedResource->getContents(); + + $title = $resource->getProperty('title') ?? $resource->getNameWithoutExtension(); + $filename = str_replace(' ', '-', strtolower($title)) . '.' . $processedResource->getExtension(); + + $response = $this->responseFactory + ->createResponse() + ->withHeader('Content-Type', $mimeType) + ->withHeader('Content-Disposition', 'inline;filename="' . $filename . '"') + ->withHeader('Cache-Control', 'no-cache') + ->withHeader('Last-Modified', $lastModified); + + $response->getBody()->write($contents); + return $response; + } + return $handler->handle($request); + } +} diff --git a/Classes/Middleware/ImageProcessingMiddleware.php b/Classes/Middleware/ImageProcessingMiddleware.php deleted file mode 100644 index fb58869..0000000 --- a/Classes/Middleware/ImageProcessingMiddleware.php +++ /dev/null @@ -1,98 +0,0 @@ -imageService = GeneralUtility::makeInstance(ImageService::class); - } - - public function injectResponseFactory(ResponseFactoryInterface $responseFactory) - { - $this->responseFactory = $responseFactory; - } - - public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface - { - /** @var \TYPO3\CMS\Core\Routing\SiteRouteResult $routing */ - $routing = $request->getAttribute('routing'); - $path = $routing->getUri()->getPath(); - $queryParams = $request->getQueryParams(); - $uid = $queryParams['uid'] ?? null; - if ($path === '/image' && $uid) { - $image = $this->imageService->getImage($uid, null, true); - - $tstamp = intval($image->getProperty('tstamp')); - $lastModified = gmdate('D, d M Y H:i:s', $tstamp) . ' GMT'; - $ifModifiedSince = $request->getHeader('if-modified-since')[0] ?? null; - - if ($lastModified === $ifModifiedSince) { - return $this->responseFactory->createResponse(304); - } - - $targetFileExtension = $queryParams['fileExtension'] ?? null; - - // Skip processing for SVGs without changing image type - if ($image->getExtension() === 'svg' && (!$targetFileExtension || $targetFileExtension === 'svg')) { - $processedImage = $image; - } else { - $cropVariant = $queryParams['breakpoint'] ?? 'default'; - - $crop = $image->getProperty('crop'); - $cropVariantCollection = CropVariantCollection::create($crop); - $cropArea = $cropVariantCollection->getCropArea($cropVariant); - - // Use default cropVariant if breakpoint cropVariant does not exist - if ($cropArea == Area::createEmpty()) { - $cropArea = $cropVariantCollection->getCropArea(); - } - - $processingInstructions = [ - 'width' => $queryParams['width'] ?? null, - 'height' => $queryParams['height'] ?? null, - 'maxWidth' => $queryParams['maxWidth'] ?? null, - 'maxHeight' => $queryParams['maxHeight'] ?? null, - 'fileExtension' => $queryParams['fileExtension'] ?? null, - 'crop' => $cropArea->makeAbsoluteBasedOnFile($image), - ]; - - $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions); - } - - $mimeType = $processedImage->getMimeType(); - $contents = $processedImage->getContents(); - - $title = $image->getProperty('title') ?? $image->getNameWithoutExtension(); - $filename = str_replace(' ', '-', strtolower($title)) . '.' . $targetFileExtension; - - $response = $this->responseFactory - ->createResponse() - ->withHeader('Content-Type', $mimeType) - ->withHeader('Content-Disposition', 'inline;filename="' . $filename . '"') - ->withHeader('Cache-Control', 'no-cache') - ->withHeader('Last-Modified', $lastModified); - - $response->getBody()->write($contents); - return $response; - } - return $handler->handle($request); - } -} diff --git a/Configuration/RequestMiddlewares.php b/Configuration/RequestMiddlewares.php index b245b48..5a4eb04 100644 --- a/Configuration/RequestMiddlewares.php +++ b/Configuration/RequestMiddlewares.php @@ -1,11 +1,11 @@ [ - 'rmnd_headless/imageprocessing' => [ - 'target' => ImageProcessingMiddleware::class, + 'rmnd_headless/asset' => [ + 'target' => AssetMiddleware::class, 'after' => [ 'typo3/cms-frontend/site', ],