Skip to content

Commit

Permalink
[TASK] Use accepted image formats from client in cache identifier to …
Browse files Browse the repository at this point in the history
…avoid the caching problem
  • Loading branch information
Atomschinken committed Mar 6, 2024
1 parent b62fcbf commit 3b4dd42
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 5 deletions.
24 changes: 24 additions & 0 deletions Classes/Helper/RequestHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Sitegeist\ImageJack\Helper;

use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class RequestHelper
{
public static function checkForMimeTypeInAcceptHeader(ServerRequest $request, string $mimeType): bool
{
if ($request->hasHeader('accept')) {
$accept = $request->getHeader('accept');
$acceptHeader = [];
if (!empty($accept[0])) {
$acceptHeader = GeneralUtility::trimExplode(',', $accept[0]);
}

return in_array($mimeType, $acceptHeader);
}

return false;
}
}
27 changes: 27 additions & 0 deletions Classes/Hook/TsfeHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Sitegeist\ImageJack\Hook;

use Sitegeist\ImageJack\Helper\RequestHelper;
use Sitegeist\ImageJack\Templates\ConverterInterface;
use TYPO3\CMS\Core\Http\ServerRequest;

class TsfeHook
{
public function postProcessHashBase($params): void
{
/** @var ServerRequest $request */
$request = $GLOBALS['TYPO3_REQUEST'];
if ($request->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;
}
}
}
}
}
4 changes: 3 additions & 1 deletion Classes/Xclass/DriverTrait.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Sitegeist\ImageJack\Xclass;

use Sitegeist\ImageJack\Helper\RequestHelper;
use Sitegeist\ImageJack\Templates\ConverterInterface;

trait DriverTrait
Expand All @@ -16,7 +17,8 @@ public function getPublicUrl($identifier): ?string
/** @var ConverterInterface $template */
foreach ($templates as $template) {
$targetFileExtension = $template::getTargetFileExtension();
if ($this->fileExists($identifier . $targetFileExtension)) {
if ($this->fileExists($identifier . $targetFileExtension) &&
RequestHelper::checkForMimeTypeInAcceptHeader($GLOBALS['TYPO3_REQUEST'], $template::getTargetMimeType())) {
$identifier .= $targetFileExtension;
break;
}
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion ext_conf_template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions ext_localconf.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Psr\Log\LogLevel;
use Sitegeist\ImageJack\Hook\TsfeHook;
use Sitegeist\ImageJack\Templates\JpegTemplate;
use Sitegeist\ImageJack\Templates\PngTemplate;
use Sitegeist\ImageJack\Templates\WebpTemplate;
Expand Down Expand Up @@ -49,5 +50,8 @@
'className' => AmazonS3Driver::class
];
}

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['createHashBase'][] =
TsfeHook::class . '->postProcessHashBase';
}
});

0 comments on commit 3b4dd42

Please sign in to comment.