TYPO3 v15 dev-main snapshot ()
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Backend\Resource\Processing;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use TYPO3\CMS\Backend\Routing\UriBuilder;
|
||||
use TYPO3\CMS\Core\Context\Context;
|
||||
use TYPO3\CMS\Core\Http\ApplicationType;
|
||||
use TYPO3\CMS\Core\Imaging\Exception\ZeroImageDimensionException;
|
||||
use TYPO3\CMS\Core\Imaging\ImageDimension;
|
||||
use TYPO3\CMS\Core\Resource\ProcessedFileRepository;
|
||||
use TYPO3\CMS\Core\Resource\Processing\ProcessorInterface;
|
||||
use TYPO3\CMS\Core\Resource\Processing\TaskInterface;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* @internal This class is a specific Backend controller implementation and is not considered part of the Public TYPO3 API.
|
||||
*/
|
||||
class DeferredBackendImageProcessor implements ProcessorInterface
|
||||
{
|
||||
public function canProcessTask(TaskInterface $task): bool
|
||||
{
|
||||
$context = GeneralUtility::makeInstance(Context::class);
|
||||
return ($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
|
||||
&& ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isBackend()
|
||||
&& $task->getType() === 'Image'
|
||||
&& in_array($task->getName(), ['Preview', 'CropScaleMask'], true)
|
||||
&& (!$context->hasAspect('fileProcessing') || $context->getPropertyFromAspect('fileProcessing', 'deferProcessing'))
|
||||
&& $task->getSourceFile()->getProperty('width') > 0
|
||||
&& $task->getSourceFile()->getProperty('height') > 0
|
||||
// Let the local image processor update the properties in case the target file exists already
|
||||
&& !$task->getSourceFile()->getStorage()->getProcessingFolder($task->getSourceFile())->hasFile($task->getTargetFileName());
|
||||
}
|
||||
|
||||
public function processTask(TaskInterface $task): void
|
||||
{
|
||||
try {
|
||||
$imageDimension = ImageDimension::fromProcessingTask($task);
|
||||
} catch (ZeroImageDimensionException $e) {
|
||||
// To not fail image processing, we just assume an image dimension here
|
||||
$imageDimension = new ImageDimension(64, 64);
|
||||
}
|
||||
$processedFile = $task->getTargetFile();
|
||||
if (!$processedFile->isPersisted()) {
|
||||
// For now, we need to persist the processed file in the repository to be able to reference its uid
|
||||
// We could instead introduce a processing queue and persist the information there
|
||||
$processedFileRepository = GeneralUtility::makeInstance(ProcessedFileRepository::class);
|
||||
$processedFileRepository->add($processedFile, $task);
|
||||
}
|
||||
$processedFile->setName($task->getTargetFileName());
|
||||
$processingUrl = (string)GeneralUtility::makeInstance(UriBuilder::class)
|
||||
->buildUriFromRoute(
|
||||
'image_processing',
|
||||
[
|
||||
'id' => $processedFile->getUid(),
|
||||
]
|
||||
);
|
||||
// We know $GLOBALS['TYPO3_REQUEST'] exists and is BE because of canProcessTask()
|
||||
$processedFile->updateProcessingUrl(GeneralUtility::locationHeaderUrl($processingUrl, $GLOBALS['TYPO3_REQUEST']));
|
||||
$processedFile->updateProperties(
|
||||
[
|
||||
'width' => $imageDimension->getWidth(),
|
||||
'height' => $imageDimension->getHeight(),
|
||||
'size' => 0,
|
||||
'checksum' => $task->getConfigurationChecksum(),
|
||||
]
|
||||
);
|
||||
$task->setExecuted(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Backend\Resource;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
|
||||
use TYPO3\CMS\Core\Resource\Event\GeneratePublicUrlForResourceEvent;
|
||||
use TYPO3\CMS\Core\Resource\ResourceInterface;
|
||||
use TYPO3\CMS\Core\Utility\PathUtility;
|
||||
|
||||
#[Autoconfigure(public: true)]
|
||||
class PublicUrlPrefixer
|
||||
{
|
||||
/**
|
||||
* Static property to avoid an infinite loop, because this listener is called when
|
||||
* public URLs are generated, but also calls public URL generation to obtain the
|
||||
* URL without prefix from the driver and possibly other listeners
|
||||
*/
|
||||
private static bool $isProcessingUrl = false;
|
||||
|
||||
public function prefixWithSitePath(GeneratePublicUrlForResourceEvent $event): void
|
||||
{
|
||||
$normalizedParams = ($GLOBALS['TYPO3_REQUEST'] ?? null)?->getAttribute('normalizedParams');
|
||||
if (self::$isProcessingUrl || !$normalizedParams) {
|
||||
return;
|
||||
}
|
||||
$resource = $event->getResource();
|
||||
if (!$this->isLocalResource($resource)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Before calling getPublicUrl, we set the static property to true to avoid to be called in a loop
|
||||
self::$isProcessingUrl = true;
|
||||
try {
|
||||
$resource = $event->getResource();
|
||||
$originalUrl = $event->getStorage()->getPublicUrl($resource);
|
||||
if (!$originalUrl || PathUtility::hasProtocolAndScheme($originalUrl)) {
|
||||
return;
|
||||
}
|
||||
// @todo: The "dynamic" event registration in both FE and BE RequestHandler's plus this
|
||||
// ugly late Request dependency resolving within FAL should be refactored by
|
||||
// incorporating Request dependency into FAL URI generation in a more direct way.
|
||||
$event->setPublicUrl($normalizedParams->getSitePath() . $originalUrl);
|
||||
} finally {
|
||||
self::$isProcessingUrl = false;
|
||||
}
|
||||
}
|
||||
|
||||
private function isLocalResource(ResourceInterface $resource): bool
|
||||
{
|
||||
return $resource->getStorage()->getDriverType() === 'Local';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user