/**
  * @param AssetInterface $asset
  * @param integer $maximumWidth
  * @param integer $maximumHeight
  * @return array
  */
 protected function getAssetIcon(AssetInterface $asset, $maximumWidth, $maximumHeight)
 {
     // TODO: Could be configurable at some point
     $iconPackage = 'TYPO3.Media';
     $iconSize = $this->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];
 }
 /**
  * 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());
     }
 }
 /**
  * Check if $value is valid. If it is not valid, needs to add an errorw
  * to Result.
  *
  * @param AssetInterface $value
  * @return void
  */
 protected function isValid($value)
 {
     $fileName = $value->getTitle() ?: $value->getResource()->getFilename();
     /** @var Query $query */
     $query = $this->entityManager->createQuery('SELECT a FROM TYPO3\\Media\\Domain\\Model\\Asset a JOIN a.resource r WHERE (a.title = :fileName OR r.filename = :fileName) AND a.Persistence_Object_Identifier != :assetIdentifier');
     $query->setParameter('fileName', $fileName);
     $query->setParameter('assetIdentifier', $this->persistenceManager->getIdentifierByObject($value));
     $result = $query->getArrayResult();
     // We need to exclude ImageVariant objects, but can not do that in the DQL query
     $result = array_filter($result, function ($value) {
         return $value['dtype'] !== 'typo3_media_imagevariant';
     });
     if (count($result) > 0) {
         $this->addError($this->translator->translateById('assetWithTitleAlreadyExists', [$fileName], null, $this->_localizationService->getConfiguration()->getCurrentLocale(), 'Main', 'Flownative.Neos.UniqueFilenames'), 1462705529);
     }
 }
 /**
  * Update the resource on an asset.
  *
  * @param AssetInterface $asset
  * @param FlowResource $resource
  * @param array $options
  * @throws InvalidArgumentValueException
  * @return void
  */
 public function updateAssetResourceAction(AssetInterface $asset, FlowResource $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);
 }
 /**
  * @param AssetInterface $asset
  * @param integer $maximumWidth
  * @param integer $maximumHeight
  * @return array
  */
 protected function getAssetThumbnailImage(AssetInterface $asset, $maximumWidth, $maximumHeight)
 {
     $iconSize = $this->getDocumentIconSize($maximumWidth, $maximumHeight);
     $iconPackage = 'TYPO3.Media';
     if (is_file('resource://TYPO3.Media/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 array('width' => $iconSize, 'height' => $iconSize, 'package' => $iconPackage, 'src' => $icon);
 }
 /**
  * Replace resource on an asset. Takes variants and redirect handling into account.
  *
  * @param AssetInterface $asset
  * @param FlowResource $resource
  * @param array $options
  * @return void
  */
 public function replaceAssetResource(AssetInterface $asset, FlowResource $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();
         foreach ($variants as $variant) {
             $originalVariantResource = $variant->getResource();
             $variant->refresh();
             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);
 }
Beispiel #8
0
 /**
  * @param AssetInterface $asset
  * @param integer $maximumWidth
  * @param integer $maximumHeight
  * @return array
  */
 public function getStaticThumbnailForAsset(AssetInterface $asset, $maximumWidth, $maximumHeight)
 {
     $iconSize = $this->getDocumentIconSize($maximumWidth, $maximumHeight);
     if (is_file('resource://TYPO3.Media/Public/Icons/16px/' . $asset->getResource()->getFileExtension() . '.png')) {
         $icon = sprintf('Icons/%spx/' . $asset->getResource()->getFileExtension() . '.png', $iconSize);
     } else {
         $icon = sprintf('Icons/%spx/_blank.png', $iconSize);
     }
     $icon = $this->resourceManager->getPublicPackageResourceUri('TYPO3.Media', $icon);
     return array('width' => $iconSize, 'height' => $iconSize, 'src' => $icon);
 }