‪TYPO3CMS  9.5
OnlineMediaController.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
28 
34 {
41  public function ‪createAction(ServerRequestInterface $request): ResponseInterface
42  {
43  $url = $request->getParsedBody()['url'];
44  $targetFolderIdentifier = $request->getParsedBody()['targetFolder'];
45  $allowedExtensions = GeneralUtility::trimExplode(',', $request->getParsedBody()['allowed'] ?: '');
46 
47  if (!empty($url)) {
48  $data = [];
49  $file = $this->‪addMediaFromUrl($url, $targetFolderIdentifier, $allowedExtensions);
50  if ($file !== null) {
51  $data['file'] = $file->getUid();
52  } else {
53  $data['error'] = $this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.error.invalid_url');
54  }
55  return new ‪JsonResponse($data);
56  }
57  return new ‪JsonResponse();
58  }
59 
67  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
68  {
69  $files = $request->getParsedBody()['data'];
70  $redirect = $request->getParsedBody()['redirect'];
71  $newMedia = [];
72  if (isset($files['newMedia'])) {
73  $newMedia = (array)$files['newMedia'];
74  }
75 
76  foreach ($newMedia as $media) {
77  if (!empty($media['url']) && !empty($media['target'])) {
78  $allowed = !empty($media['allowed']) ? GeneralUtility::trimExplode(',', $media['allowed']) : [];
79  $file = $this->‪addMediaFromUrl($media['url'], $media['target'], $allowed);
80  if ($file !== null) {
81  $flashMessage = GeneralUtility::makeInstance(
82  FlashMessage::class,
83  $file->getName(),
84  $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.new_media.added'),
86  true
87  );
88  } else {
89  $flashMessage = GeneralUtility::makeInstance(
90  FlashMessage::class,
91  $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.error.invalid_url'),
92  $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.error.new_media.failed'),
94  true
95  );
96  }
97  $this->‪addFlashMessage($flashMessage);
98  if (empty($redirect) && $media['redirect']) {
99  $redirect = $media['redirect'];
100  }
101  }
102  }
103 
104  $redirect = GeneralUtility::sanitizeLocalUrl($redirect);
105  if ($redirect) {
106  return new ‪RedirectResponse($redirect, 303);
107  }
108 
109  throw new \RuntimeException('No redirect after uploading a media found, probably a mis-use of the template not sending the proper Return URL.', 1511945040);
110  }
111 
118  protected function ‪addMediaFromUrl($url, $targetFolderIdentifier, array $allowedExtensions = [])
119  {
120  $targetFolder = null;
121  if ($targetFolderIdentifier) {
122  try {
123  $targetFolder = ‪ResourceFactory::getInstance()->‪getFolderObjectFromCombinedIdentifier($targetFolderIdentifier);
124  } catch (\‪Exception $e) {
125  $targetFolder = null;
126  }
127  }
128  if ($targetFolder === null) {
129  $targetFolder = $this->‪getBackendUser()->‪getDefaultUploadFolder();
130  }
131  return ‪OnlineMediaHelperRegistry::getInstance()->‪transformUrlToFile($url, $targetFolder, $allowedExtensions);
132  }
133 
139  protected function ‪addFlashMessage(‪FlashMessage $flashMessage)
140  {
142  $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
143 
145  $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
146  $defaultFlashMessageQueue->enqueue($flashMessage);
147  }
148 
152  protected function ‪getBackendUser()
153  {
154  return ‪$GLOBALS['BE_USER'];
155  }
156 
160  protected function ‪getLanguageService()
161  {
162  return ‪$GLOBALS['LANG'];
163  }
164 }
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry
Definition: OnlineMediaHelperRegistry.php:25
‪TYPO3\CMS\Backend\Controller\OnlineMediaController\addMediaFromUrl
‪File null addMediaFromUrl($url, $targetFolderIdentifier, array $allowedExtensions=[])
Definition: OnlineMediaController.php:118
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry\transformUrlToFile
‪File null transformUrlToFile($url, Folder $targetFolder, $allowedExtensions=[])
Definition: OnlineMediaHelperRegistry.php:59
‪TYPO3\CMS\Backend\Exception
Definition: Exception.php:23
‪TYPO3\CMS\Core\Resource\ResourceFactory\getFolderObjectFromCombinedIdentifier
‪Folder getFolderObjectFromCombinedIdentifier($identifier)
Definition: ResourceFactory.php:539
‪TYPO3\CMS\Backend\Controller\OnlineMediaController\mainAction
‪ResponseInterface mainAction(ServerRequestInterface $request)
Definition: OnlineMediaController.php:67
‪TYPO3\CMS\Core\Resource\ResourceFactory\getInstance
‪static ResourceFactory getInstance()
Definition: ResourceFactory.php:39
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:158
‪TYPO3\CMS\Backend\Controller\OnlineMediaController\addFlashMessage
‪addFlashMessage(FlashMessage $flashMessage)
Definition: OnlineMediaController.php:139
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:33
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Backend\Controller\OnlineMediaController\createAction
‪ResponseInterface createAction(ServerRequestInterface $request)
Definition: OnlineMediaController.php:41
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\getDefaultUploadFolder
‪TYPO3 CMS Core Resource Folder bool getDefaultUploadFolder($pid=null, $table=null, $field=null)
Definition: BackendUserAuthentication.php:2000
‪TYPO3\CMS\Backend\Controller\OnlineMediaController\getBackendUser
‪TYPO3 CMS Core Authentication BackendUserAuthentication getBackendUser()
Definition: OnlineMediaController.php:152
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:27
‪TYPO3\CMS\Core\Messaging\AbstractMessage\OK
‪const OK
Definition: AbstractMessage.php:27
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry\getInstance
‪static OnlineMediaHelperRegistry getInstance()
Definition: OnlineMediaHelperRegistry.php:31
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:22
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:25
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:29
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\Controller
Definition: AbstractFormEngineAjaxController.php:3
‪TYPO3\CMS\Core\Messaging\FlashMessageService
Definition: FlashMessageService.php:25
‪TYPO3\CMS\Backend\Controller\OnlineMediaController
Definition: OnlineMediaController.php:34
‪TYPO3\CMS\Core\Messaging\AbstractMessage\ERROR
‪const ERROR
Definition: AbstractMessage.php:29
‪TYPO3\CMS\Backend\Controller\OnlineMediaController\getLanguageService
‪LanguageService getLanguageService()
Definition: OnlineMediaController.php:160