/** * Upload a new image, and return its metadata. * * Depending on the $metadata argument it will return asset metadata for the AssetEditor * or image metadata for the ImageEditor * * @param Asset $asset * @param string $metadata Type of metadata to return ("Asset" or "Image") * @return string */ public function uploadAssetAction(Asset $asset, $metadata) { $this->response->setHeader('Content-Type', 'application/json'); /** @var Site $currentSite */ $currentSite = $this->siteRepository->findOneByNodeName($this->request->getInternalArgument('__siteNodeName')); if ($currentSite !== null && $currentSite->getAssetCollection() !== null) { $currentSite->getAssetCollection()->addAsset($asset); $this->assetCollectionRepository->update($currentSite->getAssetCollection()); } switch ($metadata) { case 'Asset': $result = $this->getAssetProperties($asset); if ($this->persistenceManager->isNewObject($asset)) { $this->assetRepository->add($asset); } break; case 'Image': $result = $this->getImageInterfacePreviewData($asset); if ($this->persistenceManager->isNewObject($asset)) { $this->imageRepository->add($asset); } break; default: $this->response->setStatus(400); $result = array('error' => 'Invalid "metadata" type: ' . $metadata); } return json_encode($result); }
protected function importImage($filename) { $resource = $this->resourceManager->importResource($filename); $image = new Image($resource); $this->imageRepository->add($image); $processingInstructions = array(); return $this->objectManager->get('TYPO3\\Media\\Domain\\Model\\ImageVariant', $image, $processingInstructions); }
/** * @param NodeInterface $node * @param Image $newImage * @param string $title * @param string|array $tagLabel * @param string $propertyName * @param boolean $removePreviousProfileImage * @return NodeInterface */ public function setImageToNode(NodeInterface $node, Image $newImage, $title, $tagLabel = NULL, $propertyName = 'image', $removePreviousProfileImage = FALSE) { $newImage->setTitle($title); if ($tagLabel !== NULL) { if (is_array($tagLabel) && !is_string($tagLabel)) { if ($removePreviousProfileImage === TRUE) { $this->removePreviousProfilePictureBasedOnTags($title, $tagLabel); } foreach ($tagLabel as $key => $label) { $tag = $this->tagRepository->findOneByLabel($label); if (!$tag instanceof Tag) { $tag = new Tag($label); $this->tagRepository->add($tag); } $newImage->addTag($tag); } } elseif (is_string($tagLabel) && !is_array($tagLabel)) { $tag = $this->tagRepository->findOneByLabel($tagLabel); if (!$tag instanceof Tag) { $tag = new Tag($tagLabel); $this->tagRepository->add($tag); } $newImage->addTag($tag); } } /** @var Image $image */ $image = $this->imageRepository->findByIdentifier($newImage->getIdentifier()); if ($image !== NULL) { try { $this->imageRepository->update($image); $node->setProperty($propertyName, $image); } catch (\Exception $exception) { // Image repository might give back an image while not stored for some reason. If so, catch that error and store it anyway $image->setTitle($title); $this->imageRepository->add($image); $node->setProperty($propertyName, $image); $this->systemLogger->log('Image with identifier ' . $image->getIdentifier() . ' stored while preceding an error that is not stored yet fetched using ImageRepository', LOG_CRIT); } } else { $this->imageRepository->add($newImage); $node->setProperty($propertyName, $newImage); } return $node; }
/** * Converts the given $objectXml to an ImageVariant instance and returns it * * @param \SimpleXMLElement $objectXml * @param string $className the concrete class name of the ImageVariant to create (ImageVariant or a subclass) * @return ImageVariant * @throws NeosException */ protected function importImageVariant(\SimpleXMLElement $objectXml, $className) { $processingInstructions = unserialize(trim((string) $objectXml->processingInstructions)); if (isset($objectXml->originalImage['__identifier'])) { $image = $this->imageRepository->findByIdentifier((string) $objectXml->originalImage['__identifier']); if (is_object($image)) { return $this->objectManager->get($className, $image, $processingInstructions); } } $resourceHash = (string) $objectXml->originalImage->resource->hash; $resourceData = trim((string) $objectXml->originalImage->resource->content); if ((string) $objectXml->originalImage->resource['__identifier'] !== '') { $resource = $this->persistenceManager->getObjectByIdentifier((string) $objectXml->originalImage->resource['__identifier'], 'TYPO3\\Flow\\Resource\\Resource'); } if (!isset($resource) || $resource === null) { $resource = $this->importResource((string) $objectXml->originalImage->resource->filename, $resourceHash !== '' ? $resourceHash : null, !empty($resourceData) ? $resourceData : null, (string) $objectXml->originalImage->resource['__identifier'] !== '' ? (string) $objectXml->originalImage->resource['__identifier'] : null); } $image = new Image($resource); if ((string) $objectXml->originalImage['__identifier'] !== '') { ObjectAccess::setProperty($image, 'Persistence_Object_Identifier', (string) $objectXml->originalImage['__identifier'], true); } $this->imageRepository->add($image); return $this->objectManager->get($className, $image, $processingInstructions); }
/** * Upload a new image, and return its metadata. * * @param \TYPO3\Media\Domain\Model\Image $image * @return string */ public function uploadImageAction(\TYPO3\Media\Domain\Model\Image $image) { $this->imageRepository->add($image); return $this->imageWithMetadataAction($image); }
/** * @return ImageVariant */ protected function getRandommImageVariant() { $image = new Image($this->resourceManager->importResource(sprintf('resource://Flowpack.NodeGenerator/Private/Images/Sample%d.jpg', rand(1, 3)))); $this->imageRepository->add($image); return $image->createImageVariant(array(array('command' => 'thumbnail', 'options' => array('size' => array('width' => 1500, 'height' => 1024))))); }
/** * Updates the given news object * * @param \Lelesys\Plugin\News\Domain\Model\News $news The news to update * @param array $media News media * @param array $file News file * @param array $tags News tags * @param array $relatedLink News related links * @return void */ public function update(\Lelesys\Plugin\News\Domain\Model\News $news, $media, $file, $relatedLink, $tags) { if (!empty($tags['title'])) { $tagsArray = array_unique(\TYPO3\Flow\Utility\Arrays::trimExplode(',', strtolower($tags['title']))); foreach ($tagsArray as $tag) { $existTag = $this->tagService->findTagByName($tag); if (!empty($existTag)) { $newsTags = $news->getTags()->toArray(); if (!in_array($existTag, $newsTags)) { $news->addTags($existTag); } } else { $newTag = new \Lelesys\Plugin\News\Domain\Model\Tag(); $newTag->setTitle($tag); $this->tagService->create($newTag); $news->addTags($newTag); } } } $news->setUpdatedDate(new \DateTime()); $mediaPath = $media; foreach ($mediaPath as $mediaSource) { if (!empty($mediaSource['uuid'])) { $updateAsset = $this->propertyMapper->convert($mediaSource['uuid']['__identity'], '\\TYPO3\\Media\\Domain\\Model\\Image'); $updateAsset->setCaption($mediaSource['caption']); $this->imageRepository->update($updateAsset); } else { if (!empty($mediaSource['resource']['name'])) { $resource = $this->propertyMapper->convert($mediaSource['resource'], 'TYPO3\\Flow\\Resource\\Resource'); $media = new \TYPO3\Media\Domain\Model\Image($resource); $media->setCaption($mediaSource['caption']); $this->imageRepository->add($media); $news->addAssets($media); } } } $filePath = $file; foreach ($filePath as $fileSource) { if (isset($fileSource['uuid'])) { $updateFile = $this->propertyMapper->convert($fileSource['uuid']['__identity'], '\\TYPO3\\Media\\Domain\\Model\\Document'); $updateFile->setTitle($fileSource['title']); $this->assetRepository->update($updateFile); } else { if (!empty($fileSource['resource']['name'])) { $resource = $this->propertyMapper->convert($fileSource['resource'], 'TYPO3\\Flow\\Resource\\Resource'); $file = new \TYPO3\Media\Domain\Model\Document($resource); $file->setTitle($fileSource['title']); $this->assetRepository->add($file); $news->addFiles($file); } } } $related = $relatedLink; foreach ($related as $link) { if (isset($link['uuid'])) { $updateLink = $this->linkService->findById($link['uuid']); $updateLink->setUri($link['relatedUri']); $updateLink->setTitle($link['relatedUriTitle']); $updateLink->setDescription($link['relatedUriDescription']); $updateLink->setHidden($link['hidden']); $this->linkService->update($updateLink); } else { if (!empty($link['relatedUri'])) { $newLink = new \Lelesys\Plugin\News\Domain\Model\Link(); $newLink->setTitle($link['relatedUriTitle']); $newLink->setUri($link['relatedUri']); $newLink->setDescription($link['relatedUriDescription']); $newLink->setHidden($link['hidden']); $this->linkService->create($newLink); $news->addRelatedLinks($newLink); } } } $this->newsRepository->update($news); $this->emitNewsUpdated($news); }