public function testExecute_noConfigUpdate()
    {
        $repositoryConfig = array('filesystem' => 'dummy_tmp_fs');
        self::$fileRepository->createRepository('dummy_repo2', $repositoryConfig, 'Bla bla');
        $originalStoredFile1 = static::$fileRepository->put('dummy_repo2', new File(__DIR__ . '/../../Fixtures/backend.png'));
        $commandName = 'modera:file-repository:generate-thumbnails';
        $command = $this->application->find('modera:file-repository:generate-thumbnails');
        $commandTester = new CommandTester($command);
        $commandTester->execute(array('command' => $commandName, 'repository' => 'dummy_repo2', '--thumbnail' => ['100x100', '50x50'], '--update-config' => false));
        $output = $commandTester->getDisplay();
        $expectedOutput = <<<'OUTPUT'
 # Processing (14) backend.png
  * 100x100
  * 50x50

OUTPUT;
        $this->assertEquals($expectedOutput, $output);
        /* @var StoredFile[] $alternatives */
        $alternatives = self::$em->getRepository(StoredFile::class)->findBy(array('alternativeOf' => 14));
        $this->assertEquals(2, count($alternatives));
        $this->assertValidAlternative('backend.png', 100, 100, $alternatives[0]);
        $this->assertValidAlternative('backend.png', 50, 50, $alternatives[1]);
        /* @var Repository $repository */
        $repository = self::$em->getRepository(Repository::class)->find($originalStoredFile1->getRepository()->getId());
        $repoConfig = $repository->getConfig();
        $this->assertArrayNotHasKey('interceptors', $repoConfig);
        $this->assertArrayNotHasKey('thumbnail_sizes', $repoConfig);
    }
Exemple #2
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;
 }