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