‪TYPO3CMS  ‪main
FileLinkHandler.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
25 
32 {
36  protected string ‪$baseUrn = 't3://file';
37 
42 
46  public function ‪asString(array $parameters): string
47  {
48  if ($parameters['file'] === null) {
49  return '';
50  }
51  ‪$uid = $parameters['file']->getUid();
52  // I am not sure about this use case. Maybe if the file was not indexed and saved to DB (migration from old systems)
53  if (‪$uid > 0) {
54  $urn = '?uid=' . ‪$uid;
55  } else {
56  ‪$identifier = $parameters['file']->getIdentifier();
57  $urn = '?identifier=' . urlencode(‪$identifier);
58  }
59  if (!empty($parameters['fragment'])) {
60  $urn .= '#' . $parameters['fragment'];
61  }
62  return $this->baseUrn . $urn;
63  }
64 
71  public function ‪resolveHandlerData(array $data): array
72  {
73  try {
74  $file = $this->‪resolveFile($data);
75  $fileNameValidator = GeneralUtility::makeInstance(FileNameValidator::class);
76  if (
77  !$fileNameValidator->isValid(basename($file->getIdentifier())) ||
78  !$fileNameValidator->isValid($file->getName())
79  ) {
80  $file = null;
81  }
82  } catch (‪FileDoesNotExistException $e) {
83  $file = null;
84  }
85  $result = ['file' => $file];
86  if (!empty($data['fragment'])) {
87  $result['fragment'] = $data['fragment'];
88  }
89  return $result;
90  }
91 
95  protected function ‪resolveFile(array $data): ?‪FileInterface
96  {
97  if (is_numeric($data['uid'] ?? false)) {
98  return $this->‪getResourceFactory()->getFileObject($data['uid']);
99  }
100  if (is_string($data['identifier'] ?? false) && $data['identifier'] !== '') {
101  return $this->‪getResourceFactory()->getFileObjectFromCombinedIdentifier($data['identifier']);
102  }
103  return null;
104  }
105 
110  {
111  return $this->resourceFactory ??= GeneralUtility::makeInstance(ResourceFactory::class);
112  }
113 }
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:26
‪TYPO3\CMS\Core\Resource\Security\FileNameValidator
Definition: FileNameValidator.php:25
‪TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException
Definition: FileDoesNotExistException.php:21
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Webhooks\Message\$uid
‪identifier readonly int $uid
Definition: PageModificationMessage.php:35
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37