From 888a66bb428e74fff67619b8a9bbbfa5cb8d2ced Mon Sep 17 00:00:00 2001 From: bwaidelich Date: Tue, 30 May 2023 17:59:22 +0200 Subject: [PATCH 1/3] BUGFIX: Allow disabling of auto-created Image Variants MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes support for the setting `autoCreateImageVariantPresets` that was documented for a long time but never actually evaluated. This change set: * Adjusts `AssetService::assetCreated()` signal to only trigger `AssetVariantGenerator::createVariants()` if the `autoCreateImageVariantPresets` flag is set * Sets the default value of the flag to `true` for greater backwards compatibility * Adjusts `AssetVariantGenerator::createVariant()` to only create a variant if it does not exist already – previously multiple variants with the same identifiers could be created for a single asset leading to undeterministic behavior * Adds a button "Create missing Variants" to the `Variants` tab of the Media Module allowing editors to manually trigger creation of (missing) variants. Fixes: #4300 --- .../Classes/Controller/AssetController.php | 33 +++++++++++++++++++ Neos.Media.Browser/Configuration/Policy.yaml | 2 +- .../Private/Templates/Asset/Variants.html | 7 ++++ .../Private/Translations/en/Main.xlf | 3 ++ .../Domain/Service/AssetVariantGenerator.php | 6 ++++ Neos.Media/Classes/Package.php | 9 ++++- Neos.Media/Configuration/Settings.yaml | 2 +- Neos.Media/Documentation/VariantPresets.rst | 6 ++-- 8 files changed, 62 insertions(+), 6 deletions(-) diff --git a/Neos.Media.Browser/Classes/Controller/AssetController.php b/Neos.Media.Browser/Classes/Controller/AssetController.php index e82e3d6df41..b1e9ec03449 100644 --- a/Neos.Media.Browser/Classes/Controller/AssetController.php +++ b/Neos.Media.Browser/Classes/Controller/AssetController.php @@ -50,6 +50,7 @@ use Neos\Media\Domain\Repository\AssetRepository; use Neos\Media\Domain\Repository\TagRepository; use Neos\Media\Domain\Service\AssetService; +use Neos\Media\Domain\Service\AssetVariantGenerator; use Neos\Media\Exception\AssetServiceException; use Neos\Media\TypeConverter\AssetInterfaceConverter; use Neos\Neos\Controller\BackendUserTranslationTrait; @@ -140,6 +141,12 @@ class AssetController extends ActionController */ protected $assetSourceService; + /** + * @Flow\Inject + * @var AssetVariantGenerator + */ + protected $assetVariantGenerator; + /** * @var AssetSourceInterface[] */ @@ -468,6 +475,32 @@ public function variantsAction(string $assetSourceIdentifier, string $assetProxy } } + /** + * (Re-)create all variants for the given image + * + * @param string $assetSourceIdentifier + * @param string $assetProxyIdentifier + * @param string $overviewAction + * @throws StopActionException + * @throws UnsupportedRequestTypeException + */ + public function createVariantsAction(string $assetSourceIdentifier, string $assetProxyIdentifier, string $overviewAction): void + { + $assetSource = $this->assetSources[$assetSourceIdentifier]; + $assetProxyRepository = $assetSource->getAssetProxyRepository(); + + $assetProxy = $assetProxyRepository->getAssetProxy($assetProxyIdentifier); + $asset = $this->persistenceManager->getObjectByIdentifier($assetProxy->getLocalAssetIdentifier(), Asset::class); + + /** @var VariantSupportInterface $originalAsset */ + $originalAsset = ($asset instanceof AssetVariantInterface ? $asset->getOriginalAsset() : $asset); + + $this->assetVariantGenerator->createVariants($originalAsset); + $this->assetRepository->update($originalAsset); + + $this->redirect('variants', null, null, ['assetSourceIdentifier' => $assetSourceIdentifier, 'assetProxyIdentifier' => $assetProxyIdentifier, 'overviewAction' => $overviewAction]); + } + /** * @return void * @throws NoSuchArgumentException diff --git a/Neos.Media.Browser/Configuration/Policy.yaml b/Neos.Media.Browser/Configuration/Policy.yaml index 24e025b8edb..a237114296c 100644 --- a/Neos.Media.Browser/Configuration/Policy.yaml +++ b/Neos.Media.Browser/Configuration/Policy.yaml @@ -6,7 +6,7 @@ privilegeTargets: 'Neos.Media.Browser:ManageAssets': label: Allowed to manage assets - matcher: 'method(Neos\Media\Browser\Controller\(Asset|Image)Controller->(index|new|show|edit|update|initializeCreate|create|replaceAssetResource|updateAssetResource|initializeUpload|upload|tagAsset|delete|createTag|editTag|updateTag|deleteTag|addAssetToCollection|relatedNodes|variants)Action()) || method(Neos\Media\Browser\Controller\ImageVariantController->(update)Action())' + matcher: 'method(Neos\Media\Browser\Controller\(Asset|Image)Controller->(index|new|show|edit|update|initializeCreate|create|replaceAssetResource|updateAssetResource|initializeUpload|upload|tagAsset|delete|createTag|editTag|updateTag|deleteTag|addAssetToCollection|relatedNodes|variants|createVariants)Action()) || method(Neos\Media\Browser\Controller\ImageVariantController->(update)Action())' 'Neos.Media.Browser:AssetUsage': label: Allowed to calculate asset usages diff --git a/Neos.Media.Browser/Resources/Private/Templates/Asset/Variants.html b/Neos.Media.Browser/Resources/Private/Templates/Asset/Variants.html index 0c035452335..e024a452100 100644 --- a/Neos.Media.Browser/Resources/Private/Templates/Asset/Variants.html +++ b/Neos.Media.Browser/Resources/Private/Templates/Asset/Variants.html @@ -24,6 +24,13 @@ + + + diff --git a/Neos.Media.Browser/Resources/Private/Translations/en/Main.xlf b/Neos.Media.Browser/Resources/Private/Translations/en/Main.xlf index 81cddf67e1e..1f9682910d3 100644 --- a/Neos.Media.Browser/Resources/Private/Translations/en/Main.xlf +++ b/Neos.Media.Browser/Resources/Private/Translations/en/Main.xlf @@ -427,6 +427,9 @@ No document node found for this node + + Create missing Variants + diff --git a/Neos.Media/Classes/Domain/Service/AssetVariantGenerator.php b/Neos.Media/Classes/Domain/Service/AssetVariantGenerator.php index 4a8224515c0..77797c1a514 100644 --- a/Neos.Media/Classes/Domain/Service/AssetVariantGenerator.php +++ b/Neos.Media/Classes/Domain/Service/AssetVariantGenerator.php @@ -127,6 +127,12 @@ public function createVariant(AssetInterface $asset, string $presetIdentifier, s $createdVariant = null; $preset = $this->getVariantPresets()[$presetIdentifier] ?? null; if ($preset instanceof VariantPreset && $preset->matchesMediaType($asset->getMediaType())) { + + $existingVariant = $asset->getVariant($presetIdentifier, $variantIdentifier); + if ($existingVariant !== null) { + return $existingVariant; + } + $variantConfiguration = $preset->variants()[$variantIdentifier] ?? null; if ($variantConfiguration instanceof Configuration\Variant) { diff --git a/Neos.Media/Classes/Package.php b/Neos.Media/Classes/Package.php index 299f827307c..dd5b7b06fb5 100644 --- a/Neos.Media/Classes/Package.php +++ b/Neos.Media/Classes/Package.php @@ -11,8 +11,10 @@ * source code. */ +use Neos\Flow\Configuration\ConfigurationManager; use Neos\Flow\Core\Bootstrap; use Neos\Flow\Package\Package as BasePackage; +use Neos\Media\Domain\Model\AssetInterface; use Neos\Media\Domain\Model\ImportedAssetManager; use Neos\Media\Domain\Service\AssetService; use Neos\Media\Domain\Service\AssetVariantGenerator; @@ -30,7 +32,12 @@ class Package extends BasePackage public function boot(Bootstrap $bootstrap) { $dispatcher = $bootstrap->getSignalSlotDispatcher(); - $dispatcher->connect(AssetService::class, 'assetCreated', AssetVariantGenerator::class, 'createVariants'); + $dispatcher->connect(AssetService::class, 'assetCreated', function (AssetInterface $asset) use ($bootstrap) { + $configurationManager = $bootstrap->getObjectManager()->get(ConfigurationManager::class); + if ($configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, 'Neos.Media.autoCreateImageVariantPresets')) { + $bootstrap->getObjectManager()->get(AssetVariantGenerator::class)->createVariants($asset); + } + }); $dispatcher->connect(AssetService::class, 'assetCreated', ThumbnailGenerator::class, 'createThumbnails'); $dispatcher->connect(AssetService::class, 'assetCreated', ImportedAssetManager::class, 'registerCreatedAsset'); $dispatcher->connect(AssetService::class, 'assetRemoved', ImportedAssetManager::class, 'registerRemovedAsset'); diff --git a/Neos.Media/Configuration/Settings.yaml b/Neos.Media/Configuration/Settings.yaml index 035e4020d23..8025a3d5774 100644 --- a/Neos.Media/Configuration/Settings.yaml +++ b/Neos.Media/Configuration/Settings.yaml @@ -65,7 +65,7 @@ Neos: # Variant presets variantPresets: [] # Automatically create asset variants for configured presets when assets are added - autoCreateImageVariantPresets: false + autoCreateImageVariantPresets: true thumbnailGenerators: diff --git a/Neos.Media/Documentation/VariantPresets.rst b/Neos.Media/Documentation/VariantPresets.rst index 02976ec41b2..d43dd11e921 100644 --- a/Neos.Media/Documentation/VariantPresets.rst +++ b/Neos.Media/Documentation/VariantPresets.rst @@ -92,14 +92,14 @@ The following example shows the required structure and possible fields of the pr options: aspectRatio: '1:1' -The automatic variant generation for new assets has to be enabled via setting as -by default this feature is disabled. +The automatic variant generation for new assets is active by default. +It can be disabled via setting: .. code-block:: yaml Neos: Media: - autoCreateImageVariantPresets: true + autoCreateImageVariantPresets: false To show and edit the variants in the media module the variants tab has to be enabled. From 3582ca2b8a3776861641251ec62a967608532e1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anke=20H=C3=A4slich?= Date: Thu, 13 Jul 2023 19:03:48 +0200 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Bastian Waidelich --- Neos.Media.Browser/Classes/Controller/AssetController.php | 2 +- Neos.Media.Browser/Resources/Private/Translations/en/Main.xlf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Neos.Media.Browser/Classes/Controller/AssetController.php b/Neos.Media.Browser/Classes/Controller/AssetController.php index b1e9ec03449..141326a6951 100644 --- a/Neos.Media.Browser/Classes/Controller/AssetController.php +++ b/Neos.Media.Browser/Classes/Controller/AssetController.php @@ -476,7 +476,7 @@ public function variantsAction(string $assetSourceIdentifier, string $assetProxy } /** - * (Re-)create all variants for the given image + * Create missing variants for the given image * * @param string $assetSourceIdentifier * @param string $assetProxyIdentifier diff --git a/Neos.Media.Browser/Resources/Private/Translations/en/Main.xlf b/Neos.Media.Browser/Resources/Private/Translations/en/Main.xlf index 1f9682910d3..07f3354ed02 100644 --- a/Neos.Media.Browser/Resources/Private/Translations/en/Main.xlf +++ b/Neos.Media.Browser/Resources/Private/Translations/en/Main.xlf @@ -428,7 +428,7 @@ No document node found for this node - Create missing Variants + Create missing variants From 5cc5f89da182bc55f72ce6152b8bb9ef3368337a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anke=20H=C3=A4slich?= Date: Wed, 1 Nov 2023 10:11:32 +0100 Subject: [PATCH 3/3] TASK: Fix style ci --- Neos.Media/Classes/Domain/Service/AssetVariantGenerator.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Neos.Media/Classes/Domain/Service/AssetVariantGenerator.php b/Neos.Media/Classes/Domain/Service/AssetVariantGenerator.php index 77797c1a514..69d4c12e379 100644 --- a/Neos.Media/Classes/Domain/Service/AssetVariantGenerator.php +++ b/Neos.Media/Classes/Domain/Service/AssetVariantGenerator.php @@ -127,7 +127,6 @@ public function createVariant(AssetInterface $asset, string $presetIdentifier, s $createdVariant = null; $preset = $this->getVariantPresets()[$presetIdentifier] ?? null; if ($preset instanceof VariantPreset && $preset->matchesMediaType($asset->getMediaType())) { - $existingVariant = $asset->getVariant($presetIdentifier, $variantIdentifier); if ($existingVariant !== null) { return $existingVariant;