/** * Returns an icon for a filetype within given dimensions * * @param AssetInterface $asset * @param integer $maximumWidth * @param integer $maximumHeight * @return array */ public static function getIcon(AssetInterface $asset, $maximumWidth, $maximumHeight) { // TODO: Could be configurable at some point $iconPackage = 'Neos.Media'; $iconSize = self::getDocumentIconSize($maximumWidth, $maximumHeight); if (is_file('resource://' . $iconPackage . '/Public/Icons/16px/' . $asset->getResource()->getFileExtension() . '.png')) { $icon = sprintf('Icons/%spx/' . $asset->getResource()->getFileExtension() . '.png', $iconSize); } else { $icon = sprintf('Icons/%spx/_blank.png', $iconSize); } return ['width' => $iconSize, 'height' => $iconSize, 'src' => 'resource://' . $iconPackage . '/Public/' . $icon, 'alt' => $asset->getResource()->getFileExtension()]; }
/** * Returns a thumbnail of the given asset * * If the maximum width / height is not specified or exceeds the original asset's dimensions, the width / height of * the original asset is used. * * @param AssetInterface $asset The asset to render a thumbnail for * @param ThumbnailConfiguration $configuration * @return ImageInterface * @throws \Exception */ public function getThumbnail(AssetInterface $asset, ThumbnailConfiguration $configuration) { // Calculates the dimensions of the thumbnail to be generated and returns the thumbnail image if the new // dimensions differ from the specified image dimensions, otherwise the original image is returned. if ($asset instanceof ImageInterface) { if ($asset->getWidth() === null && $asset->getHeight() === null) { return $asset; } $maximumWidth = $configuration->getMaximumWidth() > $asset->getWidth() ? $asset->getWidth() : $configuration->getMaximumWidth(); $maximumHeight = $configuration->getMaximumHeight() > $asset->getHeight() ? $asset->getHeight() : $configuration->getMaximumHeight(); if ($configuration->isUpScalingAllowed() === false && $maximumWidth === $asset->getWidth() && $maximumHeight === $asset->getHeight()) { return $asset; } } $assetIdentifier = $this->persistenceManager->getIdentifierByObject($asset); $configurationHash = $configuration->getHash(); if (!isset($this->thumbnailCache[$assetIdentifier])) { $this->thumbnailCache[$assetIdentifier] = []; } if (isset($this->thumbnailCache[$assetIdentifier][$configurationHash])) { $thumbnail = $this->thumbnailCache[$assetIdentifier][$configurationHash]; } else { $thumbnail = $this->thumbnailRepository->findOneByAssetAndThumbnailConfiguration($asset, $configuration); $this->thumbnailCache[$assetIdentifier][$configurationHash] = $thumbnail; } $async = $configuration->isAsync(); if ($thumbnail === null) { try { $thumbnail = new Thumbnail($asset, $configuration, $async); $this->emitThumbnailCreated($thumbnail); // If the thumbnail strategy failed to generate a valid thumbnail if ($async === false && $thumbnail->getResource() === null && $thumbnail->getStaticResource() === null) { $this->thumbnailRepository->remove($thumbnail); return null; } if (!$this->persistenceManager->isNewObject($asset)) { $this->thumbnailRepository->add($thumbnail); } $asset->addThumbnail($thumbnail); // Allow thumbnails to be persisted even if this is a "safe" HTTP request: $this->persistenceManager->whiteListObject($thumbnail); $this->thumbnailCache[$assetIdentifier][$configurationHash] = $thumbnail; } catch (NoThumbnailAvailableException $exception) { $this->systemLogger->logException($exception); return null; } $this->persistenceManager->whiteListObject($thumbnail); $this->thumbnailCache[$assetIdentifier][$configurationHash] = $thumbnail; } elseif ($thumbnail->getResource() === null && $async === false) { $this->refreshThumbnail($thumbnail); } return $thumbnail; }
/** * Fetches possible usages of the asset and registers nodes that use the asset as changed. * * @param AssetInterface $asset * @return void */ public function registerAssetResourceChange(AssetInterface $asset) { if (!$asset->isInUse()) { return; } foreach ($this->assetService->getUsageReferences($asset) as $reference) { if (!$reference instanceof AssetUsageInNodeProperties) { continue; } $this->registerNodeChange($reference->getNode()); } }
/** * Update the resource on an asset. * * @param AssetInterface $asset * @param PersistentResource $resource * @param array $options * @throws InvalidArgumentValueException * @return void */ public function updateAssetResourceAction(AssetInterface $asset, PersistentResource $resource, array $options = []) { $sourceMediaType = MediaTypes::parseMediaType($asset->getMediaType()); $replacementMediaType = MediaTypes::parseMediaType($resource->getMediaType()); // Prevent replacement of image, audio and video by a different mimetype because of possible rendering issues. if (in_array($sourceMediaType['type'], ['image', 'audio', 'video']) && $sourceMediaType['type'] !== $replacementMediaType['type']) { $this->addFlashMessage('Resources of type "%s" can only be replaced by a similar resource. Got type "%s"', '', Message::SEVERITY_WARNING, [$sourceMediaType['type'], $resource->getMediaType()], 1462308179); $this->redirect('index'); } parent::updateAssetResourceAction($asset, $resource, $options); }
/** * Replace resource on an asset. Takes variants and redirect handling into account. * * @param AssetInterface $asset * @param PersistentResource $resource * @param array $options * @return void */ public function replaceAssetResource(AssetInterface $asset, PersistentResource $resource, array $options = []) { $originalAssetResource = $asset->getResource(); $asset->setResource($resource); if (isset($options['keepOriginalFilename']) && (bool) $options['keepOriginalFilename'] === true) { $asset->getResource()->setFilename($originalAssetResource->getFilename()); } $uriMapping = []; $redirectHandlerEnabled = isset($options['generateRedirects']) && (bool) $options['generateRedirects'] === true && $this->packageManager->isPackageAvailable('Neos.RedirectHandler'); if ($redirectHandlerEnabled) { $uriMapping[$this->resourceManager->getPublicPersistentResourceUri($originalAssetResource)] = $this->resourceManager->getPublicPersistentResourceUri($asset->getResource()); } if (method_exists($asset, 'getVariants')) { $variants = $asset->getVariants(); /** @var AssetVariantInterface $variant */ foreach ($variants as $variant) { $originalVariantResource = $variant->getResource(); $variant->refresh(); if (method_exists($variant, 'getAdjustments')) { foreach ($variant->getAdjustments() as $adjustment) { if (method_exists($adjustment, 'refit')) { $adjustment->refit($asset); } } } if ($redirectHandlerEnabled) { $uriMapping[$this->resourceManager->getPublicPersistentResourceUri($originalVariantResource)] = $this->resourceManager->getPublicPersistentResourceUri($variant->getResource()); } $this->getRepository($variant)->update($variant); } } if ($redirectHandlerEnabled) { /** @var \Neos\RedirectHandler\Storage\RedirectStorageInterface $redirectStorage */ $redirectStorage = $this->objectManager->get(\Neos\RedirectHandler\Storage\RedirectStorageInterface::class); foreach ($uriMapping as $originalUri => $newUri) { $existingRedirect = $redirectStorage->getOneBySourceUriPathAndHost($originalUri); if ($existingRedirect === null) { $redirectStorage->addRedirect($originalUri, $newUri, 301); } } } $this->getRepository($asset)->update($asset); $this->emitAssetResourceReplaced($asset); }
/** * Replace the resource on an asset. * * @param AssetInterface $asset * @param PersistentResource $resource * @param array $options * @return void */ public function updateAssetResourceAction(AssetInterface $asset, PersistentResource $resource, array $options = []) { try { $originalFilename = $asset->getLabel(); $this->assetService->replaceAssetResource($asset, $resource, $options); } catch (\Exception $exception) { $this->addFlashMessage('couldNotReplaceAsset', '', Message::SEVERITY_OK, [], 1463472606); $this->forwardToReferringRequest(); } $this->addFlashMessage('assetHasBeenReplaced', '', Message::SEVERITY_OK, [htmlspecialchars($originalFilename)]); $this->redirect('index'); }