diff --git a/Classes/Helper/RequestHelper.php b/Classes/Helper/RequestHelper.php new file mode 100644 index 0000000..935b8f3 --- /dev/null +++ b/Classes/Helper/RequestHelper.php @@ -0,0 +1,24 @@ +hasHeader('accept')) { + $accept = $request->getHeader('accept'); + $acceptHeader = []; + if (!empty($accept[0])) { + $acceptHeader = GeneralUtility::trimExplode(',', $accept[0]); + } + + return in_array($mimeType, $acceptHeader); + } + + return false; + } +} diff --git a/Classes/Hook/TsfeHook.php b/Classes/Hook/TsfeHook.php new file mode 100644 index 0000000..372d923 --- /dev/null +++ b/Classes/Hook/TsfeHook.php @@ -0,0 +1,27 @@ +hasHeader('accept')) { + $templates = array_filter($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['image_jack']['templates'], function ($template) { + return in_array(ConverterInterface::class, class_implements($template)); + }); + foreach ($templates as $template) { + $targetMimeType = $template::getTargetMimeType(); + if (RequestHelper::checkForMimeTypeInAcceptHeader($request, $targetMimeType)) { + $params['hashParameters']['accepts'][$targetMimeType] = true; + } + } + } + } +} diff --git a/Classes/Xclass/DriverTrait.php b/Classes/Xclass/DriverTrait.php index d3d79f6..fce5dff 100644 --- a/Classes/Xclass/DriverTrait.php +++ b/Classes/Xclass/DriverTrait.php @@ -1,6 +1,7 @@ fileExists($identifier . $targetFileExtension)) { + if ($this->fileExists($identifier . $targetFileExtension) && + RequestHelper::checkForMimeTypeInAcceptHeader($GLOBALS['TYPO3_REQUEST'], $template::getTargetMimeType())) { $identifier .= $targetFileExtension; break; } diff --git a/README.md b/README.md index 4336a98..a644363 100644 --- a/README.md +++ b/README.md @@ -34,11 +34,12 @@ As we are creating the webP images as copies (keeping the original image untouched) this configuration delivers the webP image if the client supports it. Otherwise, the original image is served. +#### Storage API (beta) As a fallback solution (e.g. for remote storages) there is an option in the extension settings to activate a xclass -for the storage driver. This solution has one major drawback: The driver is only asked once on cache generation. So it -is not possible to check the client accept header in every request. The first successful request defines -which image format is used. Furthermore, there needs to be a class for every storage driver that should be extended. +for the storage driver. There needs to be a class for every storage driver that should be extended. The LocalDriver and the AmazonS3Driver are already available. +To avoid the cache problem (first client defines the served image format) the image formats from the clients accept header +are added to the page cache identifier. ### Scheduler / Cronjob To start the image processing a command is available: @@ -98,6 +99,11 @@ This can lead to high load and very long loading times upon first request (depen It is recommended to stick with the default and use the available command for processing. +#### useFallbackDriver (beta) +When enabled a xclass is used to extend the getPublicUrl function from the LocalDriver and AmazonS3Driver. +This can be used if the htaccess solution is not working, e.g. for remote storages. +See [Storage API (beta)](#storage-api-beta) for more details. + ### WebP #### active (De)activate the conversion. diff --git a/ext_conf_template.txt b/ext_conf_template.txt index d4075de..32f7456 100644 --- a/ext_conf_template.txt +++ b/ext_conf_template.txt @@ -4,7 +4,7 @@ useLiveProcessing = 0 # cat=general/enable/2; type=boolean; label=Only process images in _processed_ folders useSafeProcessing = 1 -# cat=general/enable/3; type=boolean; label=Use the fallback driver xclass to replace image urls:Only use this if the htaccess solution does not work, e.g. for remote storages +# cat=general/enable/3; type=boolean; label=Use the fallback driver xclass to replace image urls (beta):Only use this if the htaccess solution does not work, e.g. for remote storages. Beta, use at your own risk! useFallbackDriver = 0 webp { diff --git a/ext_localconf.php b/ext_localconf.php index 8dcd895..96deefe 100755 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -1,6 +1,7 @@ AmazonS3Driver::class ]; } + + $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['createHashBase'][] = + TsfeHook::class . '->postProcessHashBase'; } });