‪TYPO3CMS  ‪main
ResourceController.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 Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
34 
38 #[Controller]
40 {
41  public function ‪__construct(
42  protected readonly ‪ResourceFactory $resourceFactory,
43  protected readonly ‪FlashMessageService $flashMessageService
44  ) {
45  }
46 
47  public function ‪renameResourceAction(ServerRequestInterface $request): ResponseInterface
48  {
49  ‪$identifier = $request->getParsedBody()['identifier'] ?? null;
50  $origin = null;
51  $resource = null;
52 
53  if (‪$identifier) {
54  $origin = $this->resourceFactory->retrieveFileOrFolderObject(‪$identifier);
55  }
56 
57  try {
58  if (!$origin instanceof ‪File && !$origin instanceof ‪Folder) {
59  throw new \InvalidArgumentException('Resource must be a file or a folder', 1676979120);
60  }
61  if ($origin->getStorage()->getUid() === 0) {
62  throw new ‪InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1676299579);
63  }
64  if (!$origin->checkActionPermission('rename')) {
65  throw new ‪InsufficientFileAccessPermissionsException('You are not allowed to rename the resource', 1676979130);
66  }
67  $resourceName = $request->getParsedBody()['resourceName'] ?? null;
68  if (!$resourceName || trim((string)$resourceName) === '') {
69  throw new \InvalidArgumentException('The resource name cannot be empty', 1676978732);
70  }
71  $resource = $origin->rename($resourceName);
72  } catch (\‪Exception $exception) {
73  $message = match ($exception->getCode()) {
74  1676979120 => $this->‪getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_resource.xlf:ajax.error.message.resourceNotFileOrFolder'),
75  1676299579 => $this->‪getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_resource.xlf:ajax.error.message.resourceOutsideOfStorages'),
76  1676979130 => $this->‪getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_resource.xlf:ajax.error.message.resourceNoPermissionRename'),
77  1676978732 => $this->‪getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_resource.xlf:ajax.error.message.resourceNameCannotBeEmpty'),
78  default => $exception->getMessage(),
79  };
80 
81  return new ‪JsonResponse($this->‪getResponseData(false, $message));
82  }
83 
84  return new ‪JsonResponse($this->‪getResponseData(
85  true,
86  sprintf(
87  $this->‪getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_resource.xlf:ajax.success.message.renamed'),
88  $origin->getName(),
89  $resource->getName()
90  ),
91  $origin,
92  $resource,
93  ));
94  }
95 
99  protected function ‪getResponseData(bool $success, string $message, ?‪ResourceInterface $origin = null, ?‪ResourceInterface $resource = null): array
100  {
101  $flashMessageQueue = new ‪FlashMessageQueue('backend');
102  $flashMessageQueue->enqueue(
103  new ‪FlashMessage(
104  $message,
105  $this->‪getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_resource.xlf:ajax.' . ($success ? 'success' : 'error'))
106  )
107  );
108 
109  return [
110  'success' => $success,
111  'status' => $flashMessageQueue,
112  'origin' => $this->‪getResourceResponseData($origin),
113  'resource' => $this->‪getResourceResponseData($resource),
114  ];
115  }
116 
120  protected function ‪getResourceResponseData(?‪ResourceInterface $resource): ?array
121  {
122  if (!$resource) {
123  return null;
124  }
125 
126  return [
127  'type' => $resource instanceof ‪File ? 'file' : 'folder',
128  'identifier' => $resource instanceof ‪File || $resource instanceof ‪Folder ? $resource->getCombinedIdentifier() : null,
129  'stateIdentifier' => $resource->‪getStorage()->getUid() . '_' . ‪GeneralUtility::md5int($resource->‪getIdentifier()),
130  'name' => $resource->‪getName(),
131  'uid' => $resource instanceof ‪File ? $resource->getUid() : null,
132  'metaUid' => $resource instanceof ‪File ? $resource->getMetaData()->offsetGet('uid') : null,
133  ];
134  }
135 
137  {
138  return ‪$GLOBALS['LANG'];
139  }
140 }
‪TYPO3\CMS\Backend\Controller\Resource\ResourceController\getLanguageService
‪getLanguageService()
Definition: ResourceController.php:136
‪TYPO3\CMS\Core\Resource\ResourceInterface\getIdentifier
‪getIdentifier()
‪TYPO3\CMS\Backend\Controller\Resource\ResourceController\getResponseData
‪getResponseData(bool $success, string $message, ?ResourceInterface $origin=null, ?ResourceInterface $resource=null)
Definition: ResourceController.php:99
‪TYPO3\CMS\Backend\Controller\Resource\ResourceController\getResourceResponseData
‪getResourceResponseData(?ResourceInterface $resource)
Definition: ResourceController.php:120
‪TYPO3\CMS\Backend\Controller\Resource\ResourceController\__construct
‪__construct(protected readonly ResourceFactory $resourceFactory, protected readonly FlashMessageService $flashMessageService)
Definition: ResourceController.php:41
‪TYPO3\CMS\Core\Resource\ResourceInterface\getName
‪getName()
‪TYPO3\CMS\Backend\Exception
Definition: Exception.php:24
‪TYPO3\CMS\Core\Resource\ResourceInterface\getStorage
‪getStorage()
‪TYPO3\CMS\Backend\Controller\Resource\ResourceController
Definition: ResourceController.php:40
‪TYPO3\CMS\Backend\Attribute\Controller
Definition: Controller.php:25
‪TYPO3\CMS\Backend\Controller\Resource
Definition: ResourceController.php:18
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
Definition: InsufficientFileAccessPermissionsException.php:24
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:27
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:28
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Resource\ResourceInterface
Definition: ResourceInterface.php:21
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility\md5int
‪static int md5int($str)
Definition: GeneralUtility.php:463
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue
Definition: FlashMessageQueue.php:30
‪TYPO3\CMS\Backend\Controller\Resource\ResourceController\renameResourceAction
‪renameResourceAction(ServerRequestInterface $request)
Definition: ResourceController.php:47
‪TYPO3\CMS\Core\Messaging\FlashMessageService
Definition: FlashMessageService.php:27
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37