/** * Add category image when not already present * * @param Tx_MooxNews_Domain_Model_Category $category * @param $image */ protected function setFileRelationFromImage($category, $image) { // get fileObject by given identifier (file UID, combined identifier or path/filename) try { $newImage = $this->getResourceFactory()->retrieveFileOrFolderObject($image); } catch (\TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException $exception) { $newImage = FALSE; } // only proceed if image is found if (!$newImage instanceof \TYPO3\CMS\Core\Resource\File) { return; } // new image found check if this isn't already $existingImages = $category->getImages(); if (!is_null($existingImages) && $existingImages->count() !== 0) { /** @var $item Tx_MooxNews_Domain_Model_FileReference */ foreach ($existingImages as $item) { // only check already persisted items if ($item->getFileUid() === (int) $newImage->getUid() || $item->getUid() && $item->getOriginalResource()->getName() === $newImage->getName() && $item->getOriginalResource()->getSize() === (int) $newImage->getSize()) { $newImage = FALSE; break; } } } if ($newImage) { // file not inside a storage then search for existing file or copy the one form storage 0 to the import folder if ($newImage->getStorage()->getUid() === 0) { // search DB for same file based on hash (to prevent duplicates) $existingFile = $this->findFileByHash($newImage->getSha1()); // no exciting file then copy file to import folder if ($existingFile === NULL) { $newImage = $this->getResourceStorage()->copyFile($newImage, $this->getImportFolder()); } else { $newImage = $existingFile; } } /** @var Tx_MooxNews_Domain_Model_FileReference $fileReference */ $fileReference = $this->objectManager->get('Tx_MooxNews_Domain_Model_FileReference'); $fileReference->setFileUid($newImage->getUid()); $fileReference->setPid($category->getPid()); $category->addImage($fileReference); } }