/**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = null, array $context = [])
 {
     if (empty($data)) {
         return null;
     }
     return $this->repository->findOneByIdentifier($data['filePath']);
 }
 /**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = null, array $context = [])
 {
     if (null === $data || '' === $data) {
         return null;
     }
     if (is_file($data)) {
         return $this->storer->store(new \SplFileInfo($data), FileStorage::CATALOG_STORAGE_ALIAS);
     }
     return $this->repository->findOneByIdentifier($data);
 }
 /**
  * {@inheritdoc}
  *
  * Expected data input format :
  * {
  *     "originalFilename": "original_filename.extension",
  *     "filePath": "/absolute/file/path/filename.extension"
  * }
  */
 public function setAttributeData(ProductInterface $product, AttributeInterface $attribute, $data, array $options = [])
 {
     $options = $this->resolver->resolve($options);
     $this->checkLocaleAndScope($attribute, $options['locale'], $options['scope'], 'media');
     $this->checkData($attribute, $data);
     if (null === $data || empty($data['filePath'])) {
         $file = null;
     } elseif (null === ($file = $this->repository->findOneByIdentifier($data['filePath']))) {
         $file = $this->storeFile($attribute, $data);
     }
     $this->setMedia($product, $attribute, $file, $options['locale'], $options['scope']);
 }
 /**
  * {@inheritdoc}
  */
 public function compare($data, $originals)
 {
     $default = ['locale' => null, 'scope' => null, 'data' => ['filePath' => null]];
     $originals = array_merge($default, $originals);
     if (!isset($data['data']['filePath']) && !isset($originals['data']['filePath']) || $this->filesMatch($data, $originals)) {
         return null;
     }
     // compare a local file and a stored file (can happen during an import for instance)
     if (isset($data['data']['filePath']) && isset($originals['data']['filePath']) && is_file($data['data']['filePath'])) {
         $originalFile = $this->repository->findOneByIdentifier($originals['data']['filePath']);
         if (null !== $originalFile && $originalFile->getHash() === $this->getHashFile($data['data']['filePath'])) {
             return null;
         }
     }
     return $data;
 }
 /**
  * Returns the Mime type of a file.
  * If the file is linked to a FileInfo, returns its Mime type.
  *
  * @param string $filename
  *
  * @return string
  */
 protected function getMimeType($filename)
 {
     $mimeType = null;
     $file = $this->fileInfoRepository->findOneByIdentifier($filename);
     if (null !== $file) {
         $mimeType = $file->getMimeType();
     }
     if (null === $mimeType && file_exists($filename)) {
         $mimeType = MimeTypeGuesser::getInstance()->guess($filename);
     }
     return $mimeType;
 }
 /**
  * @param string $filename
  *
  * @throws NotFoundHttpException
  *
  * @return StreamedFileResponse
  */
 public function downloadAction($filename)
 {
     $filename = urldecode($filename);
     foreach ($this->filesystemAliases as $alias) {
         $fs = $this->mountManager->getFilesystem($alias);
         if ($fs->has($filename)) {
             $stream = $fs->readStream($filename);
             $headers = [];
             if (null !== ($fileInfo = $this->fileInfoRepository->findOneByIdentifier($filename))) {
                 $headers['Content-Disposition'] = sprintf('attachment; filename="%s"', $fileInfo->getOriginalFilename());
             }
             return new StreamedFileResponse($stream, 200, $headers);
         }
     }
     throw $this->createNotFoundException(sprintf('File with key "%s" could not be found.', $filename));
 }