Skip to content

Commit

Permalink
updated public file uri listener
Browse files Browse the repository at this point in the history
  • Loading branch information
Ephraim Härer committed Jan 10, 2022
1 parent d4b6963 commit 883ca3f
Showing 1 changed file with 47 additions and 25 deletions.
72 changes: 47 additions & 25 deletions Classes/EventListener/PublicFileUriListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,44 +73,29 @@ public function addTimestampToPublicUrl(GeneratePublicUrlForResourceEvent $event
if (!$storage->isPublic()) {
return;
}
$driver = $event->getDriver();
$publicUrl = $driver->getPublicUrl($fileData['identifier']);
/* check if TYPO3 is in frontend mode and check fileTypes field */
if (!$this->emSettings['disable_fe'] && !empty($this->emSettings['filetypes_fe']) &&
if ($publicUrl && !$this->emSettings['disable_fe'] &&
($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface &&
ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend()) {
$fileTypes = explode(',', $this->emSettings['filetypes_fe']);
if (in_array($fileData['extension'], $fileTypes, true)) {
$this->manipulatePublicUri($event, $fileData);
$newPublicUrl = $this->generateNewPublicUri($this->emSettings['filetypes_fe'], $fileData, $publicUrl);
if (!empty($newPublicUrl)) {
$event->setPublicUrl($newPublicUrl);
}
}
/* check if TYPO3 is in backend mode and check fileTypes field */
if (!$this->emSettings['disable_be'] && !empty($this->emSettings['filetypes_be']) &&
if ($publicUrl && !$this->emSettings['disable_be'] &&
($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface &&
ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isBackend()) {
$fileTypes = explode(',', $this->emSettings['filetypes_be']);
if (in_array($fileData['extension'], $fileTypes, true)) {
$this->manipulatePublicUri($event, $fileData);
$newPublicUrl = $this->generateNewPublicUri($this->emSettings['filetypes_be'], $fileData, $publicUrl);
if (!empty($newPublicUrl)) {
$event->setPublicUrl($newPublicUrl);
}
}
}
}

/**
* @param GeneratePublicUrlForResourceEvent $event
* @param array $fileData
* @return void
*/
protected function manipulatePublicUri($event, $fileData)
{
$publicUrl = $event->getPublicUrl();
if (!empty($publicUrl) && !empty($fileData['modDate'])) {
if (strpos($publicUrl, '?') === false) {
$event->setPublicUrl($publicUrl . '?v=' . $fileData['modDate']);
} else {
$event->setPublicUrl($publicUrl . '&v=' . $fileData['modDate']);
}
}
}

/**
* get the file data as array
*
Expand All @@ -125,4 +110,41 @@ protected function getFileData($resourceObject): array
'extension' => $resourceObject->getExtension(),
];
}

/**
* @param string $fileTypesString Comma separated file types string
* @param array $fileData
* @param string $publicUrl
* @return string|null
*/
protected function generateNewPublicUri($fileTypesString, $fileData, $publicUrl)
{
$newPublicUrl = '';
if (!empty($fileTypesString)) {
$fileTypes = explode(',', $fileTypesString);
if (in_array($fileData['extension'], $fileTypes, true)) {
$newPublicUrl = $this->manipulatePublicUri($publicUrl, $fileData);
}
} else {
$newPublicUrl = $this->manipulatePublicUri($publicUrl, $fileData);
}
return $newPublicUrl;
}

/**
* @param string $publicUrl
* @param array $fileData
* @return string|null
*/
protected function manipulatePublicUri($publicUrl, $fileData)
{
if (!empty($publicUrl) && !empty($fileData['modDate'])) {
if (strpos($publicUrl, '?') === false) {
return $publicUrl . '?v=' . $fileData['modDate'];
} else {
return $publicUrl . '&v=' . $fileData['modDate'];
}
}
return null;
}
}

0 comments on commit 883ca3f

Please sign in to comment.