Exemple #1
0
 /**
  * This method is going to be invoked recursively to generate thumbnails, classes with AlternativeFileTrait are
  * going to serve as markers that thumbnail needs to be generated.
  *
  * {@inheritdoc}
  */
 public function onPut(StoredFile $storedFile, \SplFileInfo $file, Repository $repository)
 {
     $repoConfig = $storedFile->getRepository()->getConfig();
     if (!isset($repoConfig['thumbnail_sizes']) || count($repoConfig['thumbnail_sizes']) == 0) {
         return self::RESULT_NO_CONFIG_AVAILABLE;
     }
     $isAlternative = $this->isAlternative($file);
     // given $storedFile and $file could be alternative already so we need to resolve
     // an original file that we are going to use to generate a thumbnail from
     if ($isAlternative) {
         /* @var AlternativeFileTrait $file */
         $originalStoredFile = $file->getOriginalStoredFile();
         $originalFile = $file->getOriginalFile();
         $originalStoredFile->addAlternative($storedFile);
     } else {
         $originalStoredFile = $storedFile;
         $originalFile = $file;
     }
     if (!$originalFile instanceof File) {
         // "File" class provides the API we need (like guessing MIME type)
         $originalFile = new File($originalFile->getPathname());
     }
     $isImage = substr($originalFile->getMimeType(), 0, strlen('image/')) == 'image/';
     if (!$isImage) {
         return self::RESULT_NOT_IMAGE_GIVEN;
     }
     $lookupKey = $originalFile->getPathname();
     if (!isset($this->thumbnailsProgress[$lookupKey])) {
         $this->thumbnailsProgress[$lookupKey] = $repoConfig['thumbnail_sizes'];
         // thumbnails that have yet to be generated
     }
     if ($isAlternative) {
         // Taking a config of previously generated thumbnail and updating entity to store it.
         // We couldn't update it right away when we generate a thumbnail because we haven't yet
         // had access to entity
         $this->thumbnailsGenerator->updateStoredFileAlternativeMeta($storedFile, $file->getThumbnailConfig());
     }
     // getting next thumbnail's config that needs to be generated
     $thumbnailConfig = array_shift($this->thumbnailsProgress[$lookupKey]);
     if (!$thumbnailConfig) {
         // making it possible to have thumbnails generated if the same original file is passed to file
         // repository
         unset($this->thumbnailsProgress[$lookupKey]);
         return self::RESULT_NO_MORE_THUMBNAILS;
     }
     $thumbnailPathname = $this->generateThumbnail($originalFile, $storedFile, $thumbnailConfig);
     /* @var AlternativeFileTrait $newFile */
     $newFile = null;
     if ($originalFile instanceof UploadedFile) {
         $newFile = new AlternativeUploadedFile($thumbnailPathname, $originalFile->getClientOriginalName(), $originalFile->getClientMimeType(), filesize($thumbnailPathname));
     } else {
         $newFile = new AlternativeFile($thumbnailPathname);
     }
     $newFile->setOriginalFile($originalFile);
     $newFile->setOriginalStoredFile($originalStoredFile);
     $newFile->setThumbnailConfig($thumbnailConfig);
     // recursively creating thumbnails
     $this->fileRepository->put($repository->getName(), $newFile);
     // ... we cannot modify returned storedFile here already because UoW is already flushed
     return self::RESULT_SCHEDULED;
 }