‪TYPO3CMS  ‪main
LinkController.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;
33 
37 #[AsController]
38 final class ‪LinkController
39 {
40  public function ‪__construct(
41  protected readonly ‪LinkService $linkService,
42  protected readonly ‪ResourceFactory $resourceFactory
43  ) {}
44 
45  public function ‪resourceAction(ServerRequestInterface $request): ResponseInterface
46  {
47  ‪$identifier = $request->getParsedBody()['identifier'] ?? null;
48  $link = null;
49  $resource = null;
50 
51  if (‪$identifier) {
52  $resource = $this->resourceFactory->retrieveFileOrFolderObject(‪$identifier);
53  }
54 
55  try {
56  if (!$resource instanceof ‪File && !$resource instanceof ‪Folder) {
57  throw new \InvalidArgumentException('Resource must be a file or a folder', 1679039649);
58  }
59  if ($resource->getStorage()->isFallbackStorage()) {
60  throw new ‪InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1679039650);
61  }
62  if ($resource instanceof ‪File) {
63  $parameters = [
64  'type' => ‪LinkService::TYPE_FILE,
65  'file' => $resource,
66  ];
67  }
68  if ($resource instanceof ‪Folder) {
69  $parameters = [
71  'folder' => $resource,
72  ];
73  }
74  $link = $this->linkService->asString($parameters);
75  } catch (\‪Exception $exception) {
76  $message = match ($exception->getCode()) {
77  1679039649 => $this->‪getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_resource.xlf:ajax.error.message.resourceNotFileOrFolder'),
78  1679039650 => $this->‪getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_resource.xlf:ajax.error.message.resourceOutsideOfStorages'),
79  default => $exception->getMessage(),
80  };
81 
82  return new ‪JsonResponse($this->‪getResponseData(false, $message));
83  }
84 
85  return new ‪JsonResponse($this->‪getResponseData(true, null, $link));
86  }
87 
91  protected function ‪getResponseData(bool $success, ?string $message = null, ?string $link = null): array
92  {
93  $flashMessageQueue = new ‪FlashMessageQueue('backend');
94  if ($message) {
95  $flashMessageQueue->enqueue(
96  new ‪FlashMessage(
97  $message,
98  $this->‪getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_resource.xlf:ajax.' . ($success ? 'success' : 'error')),
99  $success ? ContextualFeedbackSeverity::OK : ContextualFeedbackSeverity::ERROR
100  )
101  );
102  }
103  return [
104  'success' => $success,
105  'status' => $flashMessageQueue,
106  'link' => $link,
107  ];
108  }
109 
111  {
112  return ‪$GLOBALS['LANG'];
113  }
114 }
‪TYPO3\CMS\Backend\Exception
Definition: Exception.php:23
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
Definition: InsufficientFileAccessPermissionsException.php:23
‪TYPO3\CMS\Core\Type\ContextualFeedbackSeverity
‪ContextualFeedbackSeverity
Definition: ContextualFeedbackSeverity.php:25
‪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\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\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue
Definition: FlashMessageQueue.php:29
‪TYPO3\CMS\Backend\Controller
Definition: AboutController.php:18
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37