protected function getMapping($obj, $fieldName, $className = null)
 {
     $mapping = $this->factory->fromField($obj, $fieldName, $className);
     if ($mapping === null) {
         throw new MappingNotFoundException(sprintf('Mapping not found for field "%s"', $fieldName));
     }
     return $mapping;
 }
Beispiel #2
0
 public function remove($obj, $mapping)
 {
     $mapping = $this->factory->fromName($obj, $mapping);
     $this->dispatch(Events::PRE_REMOVE, new Event($obj, $mapping));
     $this->storage->remove($obj, $mapping);
     $mapping->setFileName($obj, null);
     $this->dispatch(Events::POST_REMOVE, new Event($obj, $mapping));
 }
 /**
  * {@inheritDoc}
  */
 public function resolvePath($obj, $field)
 {
     $mapping = $this->factory->fromField($obj, $field);
     if (null === $mapping) {
         throw new \InvalidArgumentException(sprintf('Unable to find uploadable field named: "%s"', $field));
     }
     return sprintf('%s/%s', $mapping->getUploadDir(), $mapping->getFileNameProperty()->getValue($obj));
 }
 /**
  * Dans le cas d'un document nouvellement enregistré, je vais renommer le nom du fichier uploadé si besoin
  * @param  LifecycleEventArgs $eventArgs [description]
  * @return [type]                        [description]
  */
 public function postPersist(LifecycleEventArgs $eventArgs)
 {
     $document = $eventArgs->getDocument();
     $dm = $eventArgs->getDocumentManager();
     $is_uploadable = $this->metadata_reader->isUploadable(ClassUtils::getClass($document));
     if ($is_uploadable) {
         // Récupération des champs uploadable
         $fields = $this->metadata_reader->getUploadableFields(ClassUtils::getClass($document));
         // Pour chacun de ces champs, je récupère le mapping associé pour vérifier le namer et le nom du champ
         foreach ($fields as $field) {
             $mapping = $this->mapping_factory->fromField($document, $field['propertyName']);
             if ($mapping->getNamer() instanceof \Redking\Bundle\UploadBundle\Naming\ObjectNamer) {
                 $filename = $mapping->getFileName($document);
                 $file = $mapping->getFile($document);
                 // Si il y a bien un fichier, je vérifie son nom
                 if (!is_null($filename) && $file instanceof File) {
                     $filename_normalized = $mapping->getNamer()->getNormalizedName($document, $filename);
                     // Si les deux noms ne correspondent pas, je renomme et réassigne
                     if (strcmp($filename, $filename_normalized) !== 0) {
                         $base_directory = $mapping->hasDirectoryNamer() ? $mapping->getDirectoryNamer()->directoryName($document, $mapping) . '/' : '';
                         $adapter = $this->filesystem_map->get($mapping->getUploadDestination())->getAdapter();
                         $adapter->rename($base_directory . $filename, $base_directory . $filename_normalized);
                         if ($adapter->exists($base_directory . $filename)) {
                             $adapter->delete($base_directory . $filename);
                         }
                         // On vérifie si il y a des fichiers resized à renommer
                         foreach ($this->resizes as $suffix => $resize_conf) {
                             $resize_filename = $base_directory . ResizedNamer::getName($filename, $suffix);
                             $resize_filename_normalized = $base_directory . ResizedNamer::getName($filename_normalized, $suffix);
                             if ($adapter->exists($resize_filename)) {
                                 $adapter->rename($resize_filename, $resize_filename_normalized);
                                 if ($adapter->exists($resize_filename)) {
                                     $adapter->delete($resize_filename);
                                 }
                             }
                         }
                         $mapping->setFileName($document, $filename_normalized);
                         // Ré-enregistrement
                         $class = $dm->getClassMetadata(get_class($document));
                         $dm->getUnitOfWork()->recomputeSingleDocumentChangeSet($class, $document);
                     }
                 }
             }
             // Traitement du répertoire basé sur l'id pour voir si le fichier est bien dedans
             $directory_namer = $mapping->getDirectoryNamer();
             if (!is_null($directory_namer) && $directory_namer instanceof BaseDirectoryIdNamer) {
                 $adapter = $this->filesystem_map->get($mapping->getUploadDestination())->getAdapter();
                 $filename = $mapping->getFileName($document);
                 $good_path = ltrim($directory_namer->directoryName($document, $mapping) . '/' . $filename, '/');
                 $bad_path = ltrim($directory_namer->directoryNameError($document, $mapping) . '/' . $filename, '/');
                 if (!$adapter->exists($good_path) && $adapter->exists($bad_path)) {
                     $success = $adapter->rename($bad_path, $good_path);
                 }
             }
         }
     }
 }
 /**
  * {@inheritDoc}
  */
 public function injectFiles($obj)
 {
     $mappings = $this->factory->fromObject($obj);
     foreach ($mappings as $mapping) {
         if ($mapping->getInjectOnLoad()) {
             $name = $mapping->getFileNameProperty()->getValue($obj);
             if (is_null($name)) {
                 continue;
             }
             $path = sprintf('%s/%s', $mapping->getUploadDir(), $name);
             $mapping->getProperty()->setValue($obj, new File($path, false));
         }
     }
 }
 /**
  * {@inheritDoc}
  */
 public function injectFiles($obj)
 {
     $mappings = $this->factory->fromObject($obj);
     foreach ($mappings as $mapping) {
         if ($mapping->getInjectOnLoad()) {
             $field = $mapping->getProperty()->getName();
             try {
                 $path = $this->storage->resolvePath($obj, $field);
             } catch (\InvalidArgumentException $e) {
                 continue;
             }
             $mapping->getProperty()->setValue($obj, new File($path, false));
         }
     }
 }
 public function testUploadDoesNotSetMetadataWhenUsingNonMetadataSupporterAdapter()
 {
     $obj = new DummyEntity();
     $mapping = $this->getMock('Vich\\UploaderBundle\\Mapping\\PropertyMapping');
     $adapter = $this->getMockBuilder('\\Gaufrette\\Adapter\\Apc')->disableOriginalConstructor()->getMock();
     $prop = $this->getMockBuilder('\\ReflectionProperty')->disableOriginalConstructor()->getMock();
     $prop->expects($this->any())->method('getValue')->with($obj)->will($this->returnValue('file.txt'));
     $prop->expects($this->any())->method('getName')->will($this->returnValue('nameProperty'));
     $file = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\File\\UploadedFile')->disableOriginalConstructor()->getMock();
     $file->expects($this->once())->method('getClientOriginalName')->will($this->returnValue('filename'));
     $file->expects($this->once())->method('getPathname')->will($this->returnValue(__DIR__ . '/../Fixtures/file.txt'));
     $mapping->expects($this->once())->method('getPropertyValue')->will($this->returnValue($file));
     $mapping->expects($this->once())->method('getUploadDir')->will($this->returnValue('filesystemKey'));
     $mapping->expects($this->once())->method('getFileNameProperty')->will($this->returnValue($prop));
     $mapping->expects($this->once())->method('getProperty')->will($this->returnValue($prop));
     $filesystem = $this->getMockBuilder('\\Gaufrette\\Filesystem')->disableOriginalConstructor()->setMethods(array('getAdapter', 'createStream', 'write'))->getMock();
     $imb = $this->getMockBuilder('\\Gaufrette\\Stream\\InMemoryBuffer')->disableOriginalConstructor()->setMethods(array('open', 'write', 'close'))->getMock();
     $imb->expects($this->once())->method('open')->will($this->returnValue(true));
     $imb->expects($this->once())->method('write')->will($this->returnValue(true));
     $imb->expects($this->once())->method('close')->will($this->returnValue(true));
     $filesystem->expects($this->once())->method('createStream')->will($this->returnValue($imb));
     $filesystemMap = $this->getMockBuilder('Knp\\Bundle\\GaufretteBundle\\FilesystemMap')->disableOriginalConstructor()->setMethods(array('get'))->getMock();
     $filesystemMap->expects($this->once())->method('get')->with('filesystemKey')->will($this->returnValue($filesystem));
     $this->factory->expects($this->once())->method('fromObject')->with($obj)->will($this->returnValue(array($mapping)));
     $adapter->expects($this->never())->method('setMetadata')->will($this->returnValue(null));
     $filesystem->expects($this->any())->method('getAdapter')->will($this->returnValue($adapter));
     $storage = new GaufretteStorage($this->factory, $filesystemMap);
     $storage->upload($obj);
 }
 /**
  * Test the resolve path method throws exception
  * when an invaid field name is specified.
  *
  * @expectedException \InvalidArgumentException
  */
 public function testResolvePathThrowsExceptionOnInvalidFieldName()
 {
     $obj = new DummyEntity();
     $this->factory->expects($this->once())->method('fromField')->with($obj, 'oops')->will($this->returnValue(null));
     $storage = new FileSystemStorage($this->factory);
     $storage->resolvePath($obj, 'oops');
 }
 /**
  * {@inheritDoc}
  */
 public function resolveUri($obj, $field)
 {
     $mapping = $this->factory->fromField($obj, $field);
     if (null === $mapping) {
         throw new \InvalidArgumentException(sprintf('Unable to find uploadable field named: "%s"', $field));
     }
     $name = $mapping->getFileNameProperty()->getValue($obj);
     if ($name === null) {
         throw new \InvalidArgumentException(sprintf('Unable to get filename property value: "%s"', $field));
     }
     $uriPrefix = $mapping->getUriPrefix();
     return $name ? $uriPrefix . '/' . $name : '';
 }
 /**
  *  Test file upload when filename contains directories
  * @dataProvider filenameWithDirectoriesDataProvider
  */
 public function testFilenameWithDirectoriesIsUploadedToCorrectDirectory($dir, $filename, $expectedDir, $expectedFileName)
 {
     $obj = new DummyEntity();
     $file = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\File\\UploadedFile')->disableOriginalConstructor()->getMock();
     $prop = $this->getMockBuilder('\\ReflectionProperty')->disableOriginalConstructor()->getMock();
     $name = new \stdClass();
     $namer = $this->getMockBuilder('Vich\\UploaderBundle\\Naming\\NamerInterface')->disableOriginalConstructor()->getMock();
     $namer->expects($this->once())->method('name')->with($obj, $name)->will($this->returnValue($filename));
     $prop->expects($this->any())->method('getName')->will($this->returnValue($name));
     $mapping = $this->getMock('Vich\\UploaderBundle\\Mapping\\PropertyMapping');
     $mapping->expects($this->once())->method('getNamer')->will($this->returnValue($namer));
     $mapping->expects($this->any())->method('getProperty')->will($this->returnValue($prop));
     $mapping->expects($this->once())->method('getFileNameProperty')->will($this->returnValue($prop));
     $mapping->expects($this->once())->method('getPropertyValue')->with($obj)->will($this->returnValue($file));
     $mapping->expects($this->once())->method('getDeleteOnUpdate')->will($this->returnValue(false));
     $mapping->expects($this->once())->method('hasNamer')->will($this->returnValue(true));
     $mapping->expects($this->once())->method('getUploadDir')->with($obj, $name)->will($this->returnValue($dir));
     $this->factory->expects($this->once())->method('fromObject')->with($obj)->will($this->returnValue(array($mapping)));
     $file->expects($this->once())->method('move')->with($expectedDir, $expectedFileName);
     $storage = new FileSystemStorage($this->factory);
     $storage->upload($obj);
 }
Beispiel #11
0
 /**
  *  note: extension point.
  */
 protected function getFilename($obj, $mappingName, $className = null)
 {
     $mapping = $this->factory->fromName($obj, $mappingName, $className);
     $filename = $mapping->getFileName($obj);
     return array($mapping, $filename);
 }
 public function testConfiguredNamerRetrievedFromContainer()
 {
     $obj = new DummyEntity();
     $class = new \ReflectionClass($obj);
     $mappings = array('dummy_file' => array('upload_dir' => '/tmp', 'delete_on_remove' => true, 'namer' => 'my.custom.namer', 'inject_on_load' => true));
     $uploadable = $this->getMockBuilder('Vich\\UploaderBundle\\Mapping\\Annotation\\Uploadable')->disableOriginalConstructor()->getMock();
     $fileField = $this->getMockBuilder('Vich\\UploaderBundle\\Mapping\\Annotation\\UploadableField')->disableOriginalConstructor()->getMock();
     $fileField->expects($this->any())->method('getMapping')->will($this->returnValue('dummy_file'));
     $fileField->expects($this->once())->method('getPropertyName')->will($this->returnValue('file'));
     $fileField->expects($this->once())->method('getFileNameProperty')->will($this->returnValue('fileName'));
     $namer = $this->getMock('Vich\\UploaderBundle\\Naming\\NamerInterface');
     $this->container->expects($this->once())->method('get')->with('my.custom.namer')->will($this->returnValue($namer));
     $this->adapter->expects($this->any())->method('getReflectionClass')->will($this->returnValue($class));
     $this->driver->expects($this->once())->method('readUploadable')->with($class)->will($this->returnValue($uploadable));
     $this->driver->expects($this->once())->method('readUploadableFields')->with($class)->will($this->returnValue(array($fileField)));
     $factory = new PropertyMappingFactory($this->container, $this->driver, $this->adapter, $mappings);
     $mappings = $factory->fromObject($obj);
     $this->assertEquals(1, count($mappings));
     if (count($mappings) > 0) {
         $mapping = $mappings[0];
         $this->assertEquals($namer, $mapping->getNamer());
         $this->assertTrue($mapping->hasNamer());
     }
 }
 public function testConfiguredNamerRetrievedFromContainer()
 {
     $obj = new DummyEntity();
     $mappings = array('dummy_file' => array('upload_destination' => 'images', 'delete_on_remove' => true, 'delete_on_update' => true, 'namer' => 'my.custom.namer', 'inject_on_load' => true, 'directory_namer' => null));
     $namer = $this->getMock('Vich\\UploaderBundle\\Naming\\NamerInterface');
     $this->container->expects($this->once())->method('get')->with('my.custom.namer')->will($this->returnValue($namer));
     $this->metadata->expects($this->once())->method('isUploadable')->with('Vich\\UploaderBundle\\Tests\\DummyEntity')->will($this->returnValue(true));
     $this->metadata->expects($this->once())->method('getUploadableFields')->with('Vich\\UploaderBundle\\Tests\\DummyEntity')->will($this->returnValue(array('file' => array('mapping' => 'dummy_file', 'propertyName' => 'file', 'fileNameProperty' => 'fileName'))));
     $factory = new PropertyMappingFactory($this->container, $this->metadata, $mappings);
     $mappings = $factory->fromObject($obj);
     $this->assertEquals(1, count($mappings));
     $mapping = $mappings['dummy_file'];
     $this->assertEquals($namer, $mapping->getNamer());
     $this->assertTrue($mapping->hasNamer());
 }
Beispiel #14
0
 /**
  * @dataProvider emptyFilenameProvider
  */
 public function testResolveUriWithEmptyFile($filename)
 {
     $this->mapping->expects($this->once())->method('getFileName')->will($this->returnValue($filename));
     $this->factory->expects($this->once())->method('fromName')->with($this->object, 'file_mapping')->will($this->returnValue($this->mapping));
     $this->assertNull($this->storage->resolvePath($this->object, 'file_mapping'));
 }