diff --git a/typo3/sysext/backend/Classes/Controller/OnlineMediaController.php b/typo3/sysext/backend/Classes/Controller/OnlineMediaController.php index 1ada7ffb2d0059f947f0ba9c4eb0eb0931bb8b09..058d399d0d3ef119ddabebc08f6a670b2fd50bab 100644 --- a/typo3/sysext/backend/Classes/Controller/OnlineMediaController.php +++ b/typo3/sysext/backend/Classes/Controller/OnlineMediaController.php @@ -27,6 +27,7 @@ use TYPO3\CMS\Core\Localization\LanguageService; use TYPO3\CMS\Core\Messaging\FlashMessage; use TYPO3\CMS\Core\Messaging\FlashMessageService; use TYPO3\CMS\Core\Resource\DefaultUploadFolderResolver; +use TYPO3\CMS\Core\Resource\Exception\OnlineMediaAlreadyExistsException; use TYPO3\CMS\Core\Resource\File; use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry; use TYPO3\CMS\Core\Resource\ResourceFactory; @@ -59,7 +60,13 @@ class OnlineMediaController if (!empty($url)) { $data = []; - $file = $this->addMediaFromUrl($url, $targetFolderIdentifier, $allowedExtensions); + try { + $file = $this->addMediaFromUrl($url, $targetFolderIdentifier, $allowedExtensions); + } catch (OnlineMediaAlreadyExistsException $e) { + // Ignore this exception since the endpoint is called e.g. in inline context, where the + // folder is not relevant and the same asset can be attached to a record multiple times. + $file = $e->getOnlineMedia(); + } if ($file !== null) { $data['file'] = $file->getUid(); } else { @@ -87,21 +94,34 @@ class OnlineMediaController foreach ($newMedia as $media) { if (!empty($media['url']) && !empty($media['target'])) { $allowed = !empty($media['allowed']) ? GeneralUtility::trimExplode(',', $media['allowed']) : []; - $file = $this->addMediaFromUrl($media['url'], $media['target'], $allowed); - if ($file !== null) { - $flashMessage = GeneralUtility::makeInstance( - FlashMessage::class, - $file->getName(), - $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.new_media.added'), - ContextualFeedbackSeverity::OK, - true - ); - } else { + try { + $file = $this->addMediaFromUrl($media['url'], $media['target'], $allowed); + if ($file !== null) { + $flashMessage = GeneralUtility::makeInstance( + FlashMessage::class, + $file->getName(), + $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.new_media.added'), + ContextualFeedbackSeverity::OK, + true + ); + } else { + $flashMessage = GeneralUtility::makeInstance( + FlashMessage::class, + $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.error.invalid_url'), + $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.error.new_media.failed'), + ContextualFeedbackSeverity::ERROR, + true + ); + } + } catch (OnlineMediaAlreadyExistsException $e) { $flashMessage = GeneralUtility::makeInstance( FlashMessage::class, - $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.error.invalid_url'), + sprintf( + $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.error.already_exists'), + $e->getOnlineMedia()->getName() + ), $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.error.new_media.failed'), - ContextualFeedbackSeverity::ERROR, + ContextualFeedbackSeverity::WARNING, true ); } diff --git a/typo3/sysext/core/Classes/Resource/Exception/OnlineMediaAlreadyExistsException.php b/typo3/sysext/core/Classes/Resource/Exception/OnlineMediaAlreadyExistsException.php new file mode 100644 index 0000000000000000000000000000000000000000..cf2553e54be1b3ae2fd166eef5fabef47a4c67fe --- /dev/null +++ b/typo3/sysext/core/Classes/Resource/Exception/OnlineMediaAlreadyExistsException.php @@ -0,0 +1,42 @@ +<?php + +declare(strict_types=1); + +/* + * This file is part of the TYPO3 CMS project. + * + * It is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License, either version 2 + * of the License, or any later version. + * + * For the full copyright and license information, please read the + * LICENSE.txt file that was distributed with this source code. + * + * The TYPO3 project - inspiring people to share! + */ + +namespace TYPO3\CMS\Core\Resource\Exception; + +use TYPO3\CMS\Core\Resource\Exception; +use TYPO3\CMS\Core\Resource\File; + +/** + * Exception indicating that a online media asset is already present in the target folder + */ +class OnlineMediaAlreadyExistsException extends Exception +{ + public function __construct( + private readonly File $onlineMedia, + int $code = 0 + ) { + parent::__construct( + sprintf('Online media asset "%s" does already exist in the target folder.', $onlineMedia->getName()), + $code + ); + } + + public function getOnlineMedia(): File + { + return $this->onlineMedia; + } +} diff --git a/typo3/sysext/core/Classes/Resource/OnlineMedia/Helpers/AbstractOEmbedHelper.php b/typo3/sysext/core/Classes/Resource/OnlineMedia/Helpers/AbstractOEmbedHelper.php index cbd374ca45b02a8c5650e79bd55555af2e4d4df0..0a135de68d9b2fc398a880ffa447120df48b3f3e 100644 --- a/typo3/sysext/core/Classes/Resource/OnlineMedia/Helpers/AbstractOEmbedHelper.php +++ b/typo3/sysext/core/Classes/Resource/OnlineMedia/Helpers/AbstractOEmbedHelper.php @@ -15,6 +15,7 @@ namespace TYPO3\CMS\Core\Resource\OnlineMedia\Helpers; +use TYPO3\CMS\Core\Resource\Exception\OnlineMediaAlreadyExistsException; use TYPO3\CMS\Core\Resource\File; use TYPO3\CMS\Core\Resource\Folder; use TYPO3\CMS\Core\Utility\GeneralUtility; @@ -42,18 +43,17 @@ abstract class AbstractOEmbedHelper extends AbstractOnlineMediaHelper protected function transformMediaIdToFile($mediaId, Folder $targetFolder, $fileExtension) { $file = $this->findExistingFileByOnlineMediaId($mediaId, $targetFolder, $fileExtension); - + if ($file !== null) { + throw new OnlineMediaAlreadyExistsException($file, 1695236851); + } // no existing file create new - if ($file === null) { - $oEmbed = $this->getOEmbedData($mediaId); - if (!empty($oEmbed['title'])) { - $fileName = $oEmbed['title'] . '.' . $fileExtension; - } else { - $fileName = $mediaId . '.' . $fileExtension; - } - $file = $this->createNewFile($targetFolder, $fileName, $mediaId); + $oEmbed = $this->getOEmbedData($mediaId); + if (!empty($oEmbed['title'])) { + $fileName = $oEmbed['title'] . '.' . $fileExtension; + } else { + $fileName = $mediaId . '.' . $fileExtension; } - return $file; + return $this->createNewFile($targetFolder, $fileName, $mediaId); } /** diff --git a/typo3/sysext/core/Resources/Private/Language/locallang_core.xlf b/typo3/sysext/core/Resources/Private/Language/locallang_core.xlf index 9ace3c98a9af7ee8aaff8bb250d6fa26e4c2ee6e..5ab2679d0d94e7473bb53ba103b156848bf584e1 100644 --- a/typo3/sysext/core/Resources/Private/Language/locallang_core.xlf +++ b/typo3/sysext/core/Resources/Private/Language/locallang_core.xlf @@ -796,6 +796,9 @@ Do you want to continue WITHOUT saving?</source> <trans-unit id="online_media.error.invalid_url" resname="online_media.error.invalid_url"> <source>Unknown/not allowed URL</source> </trans-unit> + <trans-unit id="online_media.error.already_exists" resname="online_media.error.already_exists"> + <source>Online media "%s" does already exist in this folder.</source> + </trans-unit> <trans-unit id="online_media.update.success" resname="online_media.update.success"> <source>Successfully updated online media</source> </trans-unit>