/**
  * Add category image when not already present
  *
  * @param 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 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 FileReference $fileReference */
         $fileReference = $this->objectManager->get(\GeorgRinger\News\Domain\Model\FileReference::class);
         $fileReference->setFileUid($newImage->getUid());
         $fileReference->setPid($category->getPid());
         $category->addImage($fileReference);
     }
 }