/**
  * This method marks an entity to be uploaded as soon as the "flush" method of your object manager is called.
  * After calling this method, the file info you passed is set for this entity in the listener. This is all it takes
  * to upload a file for an entity in the Uploadable extension.
  *
  * @param object $entity   - The entity you are marking to "Upload" as soon as you call "flush".
  * @param mixed  $fileInfo - The file info object or array. In Symfony 2, this will be typically an UploadedFile instance.
  */
 public function markEntityToUpload($entity, $fileInfo)
 {
     if (is_object($fileInfo) && $fileInfo instanceof UploadedFile) {
         $fileInfoClass = $this->fileInfoClass;
         $fileInfo = new $fileInfoClass($fileInfo);
     }
     $this->listener->addEntityFileInfo($entity, $fileInfo);
 }
Пример #2
0
 /**
  * File info can be also directly Nette FileUpload object.
  *
  * @param mixed $entity
  * @param FileUpload|FileInfoInterface $file
  * @throws Nette\InvalidStateException
  */
 public function addEntityFileInfo($entity, $file)
 {
     if ($file instanceof FileUpload) {
         $file = $this->fileInfoFromFileUpload($file);
     } else {
         if (!$file instanceof FileInfoInterface) {
             throw new Nette\InvalidStateException("Unexpected type of \$file. Expected \\Nette\\Http\\FileUpload or \\Gedmo\\Uploadable\\FileInfo\\FileInfoInterface.");
         }
     }
     parent::addEntityFileInfo($entity, $file);
 }
 public function test_useGeneratedFilenameWhenAppendingNumbers()
 {
     // We set the default path on the listener
     $this->listener->setDefaultPath($this->destinationTestDir);
     $file = new FileWithAlphanumericName();
     $fileInfo = $this->generateUploadedFile('file', $this->testFileWithSpaces, $this->testFilenameWithSpaces);
     $this->listener->addEntityFileInfo($file, $fileInfo);
     $this->em->persist($file);
     $this->em->flush();
     $filePath = $file->getPath() . '/' . str_replace(' ', '-', $fileInfo['name']);
     $this->assertPathEquals($filePath, $file->getFilePath());
     $file = new FileWithAlphanumericName();
     $this->listener->addEntityFileInfo($file, $fileInfo);
     $this->em->persist($file);
     $this->em->flush();
     $filePath = $file->getPath() . '/' . str_replace(' ', '-', str_replace('.txt', '-2.txt', $fileInfo['name']));
     $this->assertPathEquals($filePath, $file->getFilePath());
 }