‪TYPO3CMS  ‪main
ResourcePublicationSlot.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 
18 namespace ‪TYPO3\CMS\Form\Slot;
19 
31 
38 {
42  protected ‪$fileIdentifiers = [];
43 
44  public function ‪__construct(private readonly ‪HashService $hashService) {}
45 
46  #[AsEventListener('form-framework/resource-getPublicUrl')]
47  public function ‪getPublicUrl(‪GeneratePublicUrlForResourceEvent $event): void
48  {
49  $resource = $event->‪getResource();
50  if (!$resource instanceof ‪FileInterface
51  || !$this->‪has($resource)
52  || $event->‪getStorage()->getDriverType() !== 'Local'
53  ) {
54  return;
55  }
56  $event->‪setPublicUrl($this->‪getStreamUrl($event->‪getResource()));
57  }
58 
59  public function ‪add(‪FileInterface $resource): void
60  {
61  if ($this->‪has($resource)) {
62  return;
63  }
64  $this->fileIdentifiers[] = $resource->‪getIdentifier();
65  }
66 
67  public function ‪has(‪FileInterface $resource): bool
68  {
69  return in_array($resource->‪getIdentifier(), $this->fileIdentifiers, true);
70  }
71 
72  protected function ‪getStreamUrl(‪ResourceInterface $resource): string
73  {
74  $queryParameterArray = ['eID' => 'dumpFile', 't' => ''];
75  if ($resource instanceof ‪File) {
76  $queryParameterArray['f'] = $resource->getUid();
77  $queryParameterArray['t'] = 'f';
78  } elseif ($resource instanceof ‪ProcessedFile) {
79  $queryParameterArray['p'] = $resource->getUid();
80  $queryParameterArray['t'] = 'p';
81  }
82 
83  $queryParameterArray['token'] = $this->hashService->hmac(implode('|', $queryParameterArray), 'resourceStorageDumpFile');
84  ‪$publicUrl = GeneralUtility::locationHeaderUrl(‪PathUtility::getAbsoluteWebPath(‪Environment::getPublicPath() . '/index.php'));
85  ‪$publicUrl .= '?' . http_build_query($queryParameterArray, '', '&', PHP_QUERY_RFC3986);
86  return ‪$publicUrl;
87  }
88 }
‪TYPO3\CMS\Core\Resource\ResourceInterface\getIdentifier
‪getIdentifier()
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:26
‪TYPO3\CMS\Core\Resource\Event\GeneratePublicUrlForResourceEvent\setPublicUrl
‪setPublicUrl(?string $publicUrl)
Definition: GeneratePublicUrlForResourceEvent.php:60
‪TYPO3\CMS\Core\Attribute\AsEventListener
Definition: AsEventListener.php:25
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\Form\Slot\ResourcePublicationSlot
Definition: ResourcePublicationSlot.php:38
‪TYPO3\CMS\Form\Slot\ResourcePublicationSlot\getStreamUrl
‪getStreamUrl(ResourceInterface $resource)
Definition: ResourcePublicationSlot.php:71
‪TYPO3\CMS\Form\Slot\ResourcePublicationSlot\__construct
‪__construct(private readonly HashService $hashService)
Definition: ResourcePublicationSlot.php:43
‪TYPO3\CMS\Core\Resource\Event\GeneratePublicUrlForResourceEvent
Definition: GeneratePublicUrlForResourceEvent.php:31
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath(string $targetPath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:52
‪TYPO3\CMS\Form\Slot
Definition: FilePersistenceSlot.php:18
‪TYPO3\CMS\Webhooks\Message\$publicUrl
‪identifier readonly string readonly string $publicUrl
Definition: FileUpdatedMessage.php:36
‪TYPO3\CMS\Core\Resource\Event\GeneratePublicUrlForResourceEvent\getStorage
‪getStorage()
Definition: GeneratePublicUrlForResourceEvent.php:45
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Form\Slot\ResourcePublicationSlot\$fileIdentifiers
‪list< string > $fileIdentifiers
Definition: ResourcePublicationSlot.php:41
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:47
‪TYPO3\CMS\Form\Slot\ResourcePublicationSlot\has
‪has(FileInterface $resource)
Definition: ResourcePublicationSlot.php:66
‪TYPO3\CMS\Core\Resource\Event\GeneratePublicUrlForResourceEvent\getResource
‪getResource()
Definition: GeneratePublicUrlForResourceEvent.php:40
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Form\Slot\ResourcePublicationSlot\add
‪add(FileInterface $resource)
Definition: ResourcePublicationSlot.php:58
‪TYPO3\CMS\Core\Resource\ResourceInterface
Definition: ResourceInterface.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Crypto\HashService
Definition: HashService.php:27
‪TYPO3\CMS\Form\Slot\ResourcePublicationSlot\getPublicUrl
‪getPublicUrl(GeneratePublicUrlForResourceEvent $event)
Definition: ResourcePublicationSlot.php:46