/**
  * Add an entry for Resource entity in database at each upload
  * OR: find the already uploaded file based on it's hash
  *
  * @param GaufretteFile $file
  * @param string        $originalFilename
  * @param string        $type
  *
  * @throws FileNotFound
  * @throws \InvalidArgumentException
  * @throws UnexpectedValueException
  * @throws \RuntimeException
  *
  * @return ResourceInterface
  */
 public function addFile(GaufretteFile $file, $originalFilename, $type = null)
 {
     $fs = $this->getFilesystemForType($type);
     $hash = $fs->checksum($file->getKey());
     $resource = $this->findByHash($type, $hash);
     if ($resource) {
         // If resource already uploaded
         if ($fs->has($resource->getFileName())) {
             // If the file is still there
             $file->delete();
             // Delete uploaded file (because we already have one)
             return $resource;
         }
     } else {
         $resource = $this->createByType($type);
     }
     $resource->setOriginalFileName($originalFilename)->setFileName($file->getKey())->setHash($hash);
     $this->updateResourceMetadata($resource, $file);
     $em = $this->doctrine->getManager();
     $em->persist($resource);
     $em->flush();
     return $resource;
 }
 /**
  * @param ResourceInterface $resource
  * @param GaufretteFile $file
  * @throws \InvalidArgumentException|\UnexpectedValueException|FileNotFound
  */
 public function updateResourceMetadata(ResourceInterface $resource, GaufretteFile $file)
 {
     if ($resource instanceof Document) {
         $mimeType = $file->getMimeType();
         if (!$mimeType) {
             $finfo = new \finfo(FILEINFO_MIME_TYPE);
             $mimeType = $finfo->buffer($file->getContent());
         }
         $resource->setFileModifiedAt($file->getMtime())->setFileSize($file->getSize())->setFileType($mimeType);
     }
     if ($resource instanceof Image) {
         $imageSize = getimagesizefromstring($file->getContent());
         $resource->setWidth(isset($imageSize[0]) ? $imageSize[0] : null)->setHeight(isset($imageSize[1]) ? $imageSize[1] : null);
     }
 }