‪TYPO3CMS  11.5
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 {
37  protected ‪$baseUrn = 't3://file';
38 
43  protected ‪$resourceFactory;
44 
51  public function ‪asString(array $parameters): string
52  {
53  if ($parameters['file'] === null) {
54  return '';
55  }
56  $uid = $parameters['file']->getUid();
57  // I am not sure about this use case. Maybe if the file was not indexed and saved to DB (migration from old systems)
58  if ($uid > 0) {
59  $urn = '?uid=' . $uid;
60  } else {
61  $identifier = $parameters['file']->getIdentifier();
62  $urn = '?identifier=' . urlencode($identifier);
63  }
64  if (!empty($parameters['fragment'])) {
65  $urn .= '#' . $parameters['fragment'];
66  }
67  return $this->baseUrn . $urn;
68  }
69 
77  public function ‪resolveHandlerData(array $data): array
78  {
79  try {
80  $file = $this->‪resolveFile($data);
81  $fileNameValidator = GeneralUtility::makeInstance(FileNameValidator::class);
82  if (
83  !$fileNameValidator->isValid(basename($file->getIdentifier())) ||
84  !$fileNameValidator->isValid($file->getName())
85  ) {
86  $file = null;
87  }
88  } catch (‪FileDoesNotExistException $e) {
89  $file = null;
90  }
91  $result = ['file' => $file];
92  if (!empty($data['fragment'])) {
93  $result['fragment'] = $data['fragment'];
94  }
95  return $result;
96  }
97 
103  protected function ‪resolveFile(array $data): ?‪FileInterface
104  {
105  if (isset($data['uid'])) {
106  return $this->‪getResourceFactory()->‪getFileObject($data['uid']);
107  }
108  if (isset($data['identifier'])) {
109  return $this->‪getResourceFactory()->‪getFileObjectFromCombinedIdentifier($data['identifier']);
110  }
111  return null;
112  }
113 
119  protected function ‪getResourceFactory(): ‪ResourceFactory
120  {
121  if (!$this->resourceFactory) {
122  $this->resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
123  }
125  }
126 }
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪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:41
‪TYPO3\CMS\Core\Resource\ResourceFactory\getFileObjectFromCombinedIdentifier
‪File ProcessedFile null getFileObjectFromCombinedIdentifier($identifier)
Definition: ResourceFactory.php:234
‪TYPO3\CMS\Core\Resource\ResourceFactory\getFileObject
‪File getFileObject($uid, array $fileData=[])
Definition: ResourceFactory.php:209
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50