public function addFile(File $file, $originalFilename, $type = null)
    {
        $fs = $this->getFilesystemForType($type);
        $fs->addPlugin(new HashPlugin());

        /** @noinspection PhpUndefinedMethodInspection */
        $hash = $fs->hash($file->getPath());
        $resource = $this->findByHash($type, $hash);

        if ($resource) { // If resource already uploaded
            if ($fs->has($resource->getPath())) { // 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)
            ->setPath($file->getPath())
            ->setHash($hash);

        if ($this->metadataUpdater) {
            $this->metadataUpdater->updateResourceMetadata($resource, $file);
        }

        $className = ClassUtils::getClass($resource);
        $entityManager = $this->doctrine->getManagerForClass($className);
        if (!$entityManager instanceof EntityManagerInterface) {
            throw new UnexpectedValueException("No manager found for class {$className}");
        }
        $entityManager->persist($resource);
        $entityManager->flush();

        return $resource;
    }
public function addFile(File $file, $originalFilename, $type = null)
    {
        $fs = $this->getFilesystemForType($type);
        $fs->addPlugin(new HashPlugin());

        /** @noinspection PhpUndefinedMethodInspection */
        $hash = $fs->hash($file->getPath());
        $resource = $this->findByHash($type, $hash);

        if ($resource) { // If resource already uploaded
            if ($fs->has($resource->getPath())) { // 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)
            ->setPath($file->getPath())
            ->setHash($hash);

        if ($this->metadataUpdater) {
            $this->metadataUpdater->updateResourceMetadata($resource, $file);
        }

        $className = ClassUtils::getClass($resource);
        $entityManager = $this->doctrine->getManagerForClass($className);
        if (!$entityManager instanceof EntityManagerInterface) {
            throw new UnexpectedValueException("No manager found for class {$className}");
        }
        $entityManager->persist($resource);
        $entityManager->flush();

        return $resource;
    }
{@inheritdoc}