‪TYPO3CMS  ‪main
DefaultUploadFolderResolver.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 
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
25 
31 {
32  public function ‪__construct(
33  protected readonly ‪ResourceFactory $resourceFactory,
34  protected readonly ‪EventDispatcher $eventDispatcher,
35  ) {}
36 
37  public function ‪resolve(‪BackendUserAuthentication $user, int $pid = null, string $table = null, string $field = null): ‪Folder | bool
38  {
39  $uploadFolder = $this->‪getDefaultUploadFolderForUser($user);
40  $uploadFolder = $this->‪getDefaultUploadFolderForPage($pid) ?? $uploadFolder;
41 
42  $uploadFolder = $this->eventDispatcher->dispatch(
43  new ‪AfterDefaultUploadFolderWasResolvedEvent($uploadFolder, $pid, $table, $field)
44  )->getUploadFolder() ?? $uploadFolder;
45 
46  $uploadFolder = $uploadFolder ?? $this->‪getDefaultUploadFolder($user);
47 
48  return $uploadFolder instanceof ‪Folder ? $uploadFolder : false;
49  }
50 
52  {
53  $uploadFolder = $backendUser->‪getTSConfig()['options.']['defaultUploadFolder'] ?? '';
54 
55  return $this->‪resolveFolder($uploadFolder);
56  }
57 
58  public function ‪getDefaultUploadFolderForPage(?int $pid): ?‪Folder
59  {
60  $uploadFolder = BackendUtility::getPagesTSconfig($pid)['options.']['defaultUploadFolder'] ?? '';
61 
62  return $this->‪resolveFolder($uploadFolder);
63  }
64 
65  protected function ‪resolveFolder(string $uploadPath): ?‪Folder
66  {
67  $uploadFolder = null;
68 
69  if ($uploadPath) {
70  try {
71  $uploadFolder = $this->resourceFactory->getFolderObjectFromCombinedIdentifier($uploadPath);
72  } catch (‪FolderDoesNotExistException $e) {
73  }
74  }
75 
76  return $uploadFolder;
77  }
78 
87  {
88  $uploadFolder = null;
89 
90  foreach ($backendUser->‪getFileStorages() as $storage) {
91  if ($storage->isDefault() && $storage->isWritable()) {
92  try {
93  $uploadFolder = $storage->getDefaultFolder();
94  if ($uploadFolder->checkActionPermission('write')) {
95  break;
96  }
97  $uploadFolder = null;
98  } catch (‪Exception $folderAccessException) {
99  // If the folder is not accessible (no permissions / does not exist) we skip this one.
100  }
101  break;
102  }
103  }
104  if (!$uploadFolder instanceof ‪Folder) {
105  foreach ($backendUser->‪getFileStorages() as $storage) {
106  if ($storage->isWritable()) {
107  try {
108  $uploadFolder = $storage->getDefaultFolder();
109  if ($uploadFolder->checkActionPermission('write')) {
110  break;
111  }
112  $uploadFolder = null;
113  } catch (‪Exception $folderAccessException) {
114  // If the folder is not accessible (no permissions / does not exist) try the next one.
115  }
116  }
117  }
118  }
119  return $uploadFolder;
120  }
121 }
‪TYPO3\CMS\Core\Resource\DefaultUploadFolderResolver\resolveFolder
‪resolveFolder(string $uploadPath)
Definition: DefaultUploadFolderResolver.php:65
‪TYPO3\CMS\Core\Resource\DefaultUploadFolderResolver\__construct
‪__construct(protected readonly ResourceFactory $resourceFactory, protected readonly EventDispatcher $eventDispatcher,)
Definition: DefaultUploadFolderResolver.php:32
‪TYPO3\CMS\Core\Resource\DefaultUploadFolderResolver\getDefaultUploadFolder
‪Folder null getDefaultUploadFolder(BackendUserAuthentication $backendUser)
Definition: DefaultUploadFolderResolver.php:86
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\getTSConfig
‪array getTSConfig()
Definition: BackendUserAuthentication.php:857
‪TYPO3\CMS\Core\Resource\Event\AfterDefaultUploadFolderWasResolvedEvent
Definition: AfterDefaultUploadFolderWasResolvedEvent.php:26
‪TYPO3\CMS\Core\Resource\DefaultUploadFolderResolver\getDefaultUploadFolderForUser
‪getDefaultUploadFolderForUser(BackendUserAuthentication $backendUser)
Definition: DefaultUploadFolderResolver.php:51
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\getFileStorages
‪TYPO3 CMS Core Resource ResourceStorage[] getFileStorages()
Definition: BackendUserAuthentication.php:1368
‪TYPO3\CMS\Core\EventDispatcher\EventDispatcher
Definition: EventDispatcher.php:30
‪TYPO3\CMS\Core\Resource\DefaultUploadFolderResolver
Definition: DefaultUploadFolderResolver.php:31
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:38
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Core\Resource\Exception\FolderDoesNotExistException
Definition: FolderDoesNotExistException.php:21
‪TYPO3\CMS\Core\Resource\DefaultUploadFolderResolver\getDefaultUploadFolderForPage
‪getDefaultUploadFolderForPage(?int $pid)
Definition: DefaultUploadFolderResolver.php:58
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Resource\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Resource\DefaultUploadFolderResolver\resolve
‪resolve(BackendUserAuthentication $user, int $pid=null, string $table=null, string $field=null)
Definition: DefaultUploadFolderResolver.php:37