Secured Links¶
Following examples clarifies, how to retrieve secured links from different data type sources.
File Object¶
Instance of \TYPO3\CMS\Core\Resource\File given:
/** @var \TYPO3\CMS\Core\Resource\File $file */
$securedUrl = $file->getPublicUrl();
File Reference¶
Instance of \TYPO3\CMS\Core\Resource\FileReference given:
/** @var \TYPO3\CMS\Core\Resource\FileReference $fileReference */
$securedUrl = $fileReference->getPublicUrl();
Fluid Templates¶
Getting secured links within a fluid template is a no-brainer; you don’t have to pay attention to anything here:
<f:image image="{image}" class="img-fluid img-thumbnail" />
<f:image image="{image}" treatIdAsReference="TRUE" class="img-fluid img-thumbnail" />
<img src="{image.publicUrl}" class="img-fluid img-thumbnail" />
API¶
Get a link that is only valid for user 29, with user group 12, is generated on page 89 and expires on 2022/05/08:
$publicUrl = '/fileadmin/secured/invoice.pdf';
$secureDownloadService = GeneralUtility::makeInstance(SecureDownloadService::class);
if ($secureDownloadService->pathShouldBeSecured($publicUrl)) {
    $securedUrl = GeneralUtility::makeInstance(SecureLinkFactory::class)
        ->withResourceUri(rawurlencode($publicUrl)
        ->withUser(29)
        ->withPage(89)
        ->withGroups([12])
        ->withTimeout(1659650400)
        ->getUrl();
}