‪TYPO3CMS  ‪main
OnlineMediaController.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;
36 
41 #[AsController]
43 {
44  public function ‪__construct(
45  protected readonly ‪ResourceFactory $resourceFactory,
46  protected readonly ‪DefaultUploadFolderResolver $uploadFolderResolver,
47  protected readonly ‪OnlineMediaHelperRegistry $onlineMediaHelperRegistry,
48  protected readonly ‪FlashMessageService $flashMessageService
49  ) {}
50 
54  public function ‪createAction(ServerRequestInterface $request): ResponseInterface
55  {
56  ‪$url = $request->getParsedBody()['url'];
57  $targetFolderIdentifier = $request->getParsedBody()['targetFolder'];
58  $allowedExtensions = ‪GeneralUtility::trimExplode(',', $request->getParsedBody()['allowed'] ?: '');
59 
60  if (!empty(‪$url)) {
61  $data = [];
62  try {
63  $file = $this->‪addMediaFromUrl(‪$url, $targetFolderIdentifier, $allowedExtensions);
65  // Ignore this exception since the endpoint is called e.g. in inline context, where the
66  // folder is not relevant and the same asset can be attached to a record multiple times.
67  $file = $e->‪getOnlineMedia();
68  }
69  if ($file !== null) {
70  $data['file'] = $file->getUid();
71  } else {
72  $data['error'] = $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.error.invalid_url');
73  }
74  return new ‪JsonResponse($data);
75  }
76  return new ‪JsonResponse();
77  }
78 
84  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
85  {
86  $files = $request->getParsedBody()['data'];
87  $redirect = $request->getParsedBody()['redirect'];
88  $newMedia = [];
89  if (isset($files['newMedia'])) {
90  $newMedia = (array)$files['newMedia'];
91  }
92 
93  foreach ($newMedia as $media) {
94  if (!empty($media['url']) && !empty($media['target'])) {
95  $allowed = !empty($media['allowed']) ? ‪GeneralUtility::trimExplode(',', $media['allowed']) : [];
96  try {
97  $file = $this->‪addMediaFromUrl($media['url'], $media['target'], $allowed);
98  if ($file !== null) {
99  $flashMessage = GeneralUtility::makeInstance(
100  FlashMessage::class,
101  $file->getName(),
102  $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.new_media.added'),
103  ContextualFeedbackSeverity::OK,
104  true
105  );
106  } else {
107  $flashMessage = GeneralUtility::makeInstance(
108  FlashMessage::class,
109  $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.error.invalid_url'),
110  $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.error.new_media.failed'),
111  ContextualFeedbackSeverity::ERROR,
112  true
113  );
114  }
116  $flashMessage = GeneralUtility::makeInstance(
117  FlashMessage::class,
118  sprintf(
119  $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.error.already_exists'),
120  $e->‪getOnlineMedia()->getName()
121  ),
122  $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.error.new_media.failed'),
123  ContextualFeedbackSeverity::WARNING,
124  true
125  );
126  }
127  $this->‪addFlashMessage($flashMessage);
128  if (empty($redirect) && $media['redirect']) {
129  $redirect = $media['redirect'];
130  }
131  }
132  }
133 
134  $redirect = GeneralUtility::sanitizeLocalUrl($redirect);
135  if ($redirect) {
136  return new ‪RedirectResponse($redirect, 303);
137  }
138 
139  throw new \RuntimeException('No redirect after uploading a media found, probably a mis-use of the template not sending the proper Return URL.', 1511945040);
140  }
141 
148  protected function ‪addMediaFromUrl(‪$url, $targetFolderIdentifier, array $allowedExtensions = [])
149  {
150  $targetFolder = null;
151  if ($targetFolderIdentifier) {
152  try {
153  $targetFolder = $this->resourceFactory->getFolderObjectFromCombinedIdentifier($targetFolderIdentifier);
154  } catch (\‪Exception $e) {
155  $targetFolder = null;
156  }
157  }
158  if ($targetFolder === null) {
159  $targetFolder = $this->uploadFolderResolver->resolve($this->‪getBackendUser());
160  }
161  return $this->onlineMediaHelperRegistry->transformUrlToFile(‪$url, $targetFolder, $allowedExtensions);
162  }
163 
167  protected function ‪addFlashMessage(‪FlashMessage $flashMessage): void
168  {
169  $defaultFlashMessageQueue = $this->flashMessageService->getMessageQueueByIdentifier();
170  $defaultFlashMessageQueue->enqueue($flashMessage);
171  }
172 
174  {
175  return ‪$GLOBALS['BE_USER'];
176  }
177 
179  {
180  return ‪$GLOBALS['LANG'];
181  }
182 }
‪TYPO3\CMS\Core\Resource\Exception\OnlineMediaAlreadyExistsException\getOnlineMedia
‪getOnlineMedia()
Definition: OnlineMediaAlreadyExistsException.php:38
‪TYPO3\CMS\Backend\Controller\OnlineMediaController\mainAction
‪mainAction(ServerRequestInterface $request)
Definition: OnlineMediaController.php:84
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry
Definition: OnlineMediaHelperRegistry.php:27
‪TYPO3\CMS\Backend\Controller\OnlineMediaController\addMediaFromUrl
‪File null addMediaFromUrl($url, $targetFolderIdentifier, array $allowedExtensions=[])
Definition: OnlineMediaController.php:148
‪TYPO3\CMS\Backend\Exception
Definition: Exception.php:23
‪TYPO3\CMS\Backend\Controller\OnlineMediaController\getLanguageService
‪getLanguageService()
Definition: OnlineMediaController.php:178
‪TYPO3\CMS\Backend\Controller\OnlineMediaController\getBackendUser
‪getBackendUser()
Definition: OnlineMediaController.php:173
‪TYPO3\CMS\Core\Type\ContextualFeedbackSeverity
‪ContextualFeedbackSeverity
Definition: ContextualFeedbackSeverity.php:25
‪TYPO3\CMS\Backend\Controller\OnlineMediaController\__construct
‪__construct(protected readonly ResourceFactory $resourceFactory, protected readonly DefaultUploadFolderResolver $uploadFolderResolver, protected readonly OnlineMediaHelperRegistry $onlineMediaHelperRegistry, protected readonly FlashMessageService $flashMessageService)
Definition: OnlineMediaController.php:44
‪TYPO3\CMS\Backend\Controller\OnlineMediaController\addFlashMessage
‪addFlashMessage(FlashMessage $flashMessage)
Definition: OnlineMediaController.php:167
‪TYPO3\CMS\Core\Resource\DefaultUploadFolderResolver
Definition: DefaultUploadFolderResolver.php:31
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:30
‪TYPO3\CMS\Backend\Controller\OnlineMediaController\createAction
‪createAction(ServerRequestInterface $request)
Definition: OnlineMediaController.php:54
‪TYPO3\CMS\Webhooks\Message\$url
‪identifier readonly UriInterface $url
Definition: LoginErrorOccurredMessage.php:36
‪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\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Resource\Exception\OnlineMediaAlreadyExistsException
Definition: OnlineMediaAlreadyExistsException.php:27
‪TYPO3\CMS\Backend\Controller
Definition: AboutController.php:18
‪TYPO3\CMS\Core\Messaging\FlashMessageService
Definition: FlashMessageService.php:27
‪TYPO3\CMS\Backend\Controller\OnlineMediaController
Definition: OnlineMediaController.php:43
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822