public function upload(UploadedFile $file, PropertyMapping $propertyMapping, $filter_name = NULL) { if ($filter_name === NULL) { $filter_name = $propertyMapping->getMappingName(); } // vars $provider = $this->getProvider($propertyMapping); $filesystem = $provider->getFilesystem(); $relative_dir = $provider->getRelativeDir(); //$propertyMapping->getUriPrefix(); $file_name = $file->getClientOriginalName(); // if namer set $mapping_config = $this->vichGetMappingConfig($propertyMapping->getMappingName()); if ($mapping_config) { $namer = $mapping_config['namer']; // illegal offset warning (backwards compatibility) if (is_array($namer) && isset($namer['service'])) { $namer = $namer['service']; } if ($this->getContainer()->has($namer)) { $namer = $this->getContainer()->get($namer); if (method_exists($namer, 'getRandomFileName')) { $file_name = $namer->getRandomFileName($file_name, $propertyMapping); } } } // upload $uploaded = $filesystem->write($relative_dir . '/' . $file_name, file_get_contents($file->getPathname())); //$uploaded = $file->move($upload_dir, $file_name); if ($uploaded) { // apply filter return $this->doApplyFilter($file_name, $propertyMapping); } return false; }
/** * {@inheritDoc} */ public function injectFile($obj, PropertyMapping $mapping) { $path = $this->storage->resolvePath($obj, $mapping->getFilePropertyName()); if ($path !== null) { $mapping->setFile($obj, new File($path, false)); } }
/** * Creates a directory name for the file being uploaded. * * @param object $entity The object the upload is attached to. * @param PropertyMapping $mapping The mapping to use to manipulate the given object. * * @return string The directory name. */ public function directoryName($entity, PropertyMapping $mapping) { $namer = $mapping->getNamer(); if (method_exists($namer, 'getDirectory')) { return $namer->getDirectory(); } return null; }
protected function createFileList($entities, PropertyMapping $propertyMapping) { $array = array(); foreach ($entities as $entity) { $array[] = array('uuid' => uniqid(), 'name' => $this->get('vichimagine.file')->fileName($entity, $propertyMapping->getMappingName()), 'thumbnailUrl' => $this->get('vichimagine.file')->asset($entity, $propertyMapping->getMappingName())); } return $array; }
/** * Test that the configured mappings are accessed * correctly. */ public function testConfiguredMappingAccess() { $prop = new PropertyMapping(); $prop->setMapping(array('delete_on_remove' => true, 'delete_on_update' => true, 'upload_destination' => '/tmp', 'inject_on_load' => true)); $this->assertEquals($prop->getUploadDir(), '/tmp'); $this->assertTrue($prop->getDeleteOnRemove()); $this->assertTrue($prop->getInjectOnLoad()); }
public function name($entity, PropertyMapping $mapping) { $this->mapping = $mapping; /* @var UploadedFile $file */ $file = $mapping->getFile($entity); $file_name = $file->getClientOriginalName(); return $this->getRandomFileName($file_name); }
/** * {@inheritDoc} */ protected function doResolvePath(PropertyMapping $mapping, $dir, $name, $relative = false) { $path = !empty($dir) ? $dir . DIRECTORY_SEPARATOR . $name : $name; if ($relative) { return $path; } return $mapping->getUploadDestination() . DIRECTORY_SEPARATOR . $path; }
/** * {@inheritDoc} */ public function name($object, PropertyMapping $mapping) { $file = $mapping->getFile($object); $name = uniqid(); if ($extension = $this->getExtension($file)) { $name = sprintf('%s.%s', $name, $extension); } return $name; }
/** * {@inheritdoc} */ public function name($object, PropertyMapping $mapping) { $file = $mapping->getFile($object); /* @var $file UploadedFile */ $name = sha1(microtime(true) . mt_rand(0, 999)); if ($extension = $file->guessExtension()) { $name = sprintf('%s.%s', $name, $extension); } return $name; }
/** * {@inheritDoc} */ public function remove($obj, PropertyMapping $mapping) { $name = $mapping->getFileName($obj); // the non-strict comparison is done on purpose: we want to skip // null and empty filenames if (null == $name) { return; } $this->doRemove($mapping, $mapping->getUploadDir($obj), $name); }
/** * @inheritDoc */ public function name($object, PropertyMapping $mapping) { $file = $mapping->getFile($object); if ($extension = $this->getExtension($file)) { $originalFileName = str_replace(".{$extension}", "", $file->getClientOriginalName()); $newFileName = trim(preg_replace('/-+/', "-", preg_replace('/[^a-z0-9]/', "-", strtolower($originalFileName))), '-'); return sprintf('%s.%s', $newFileName, $extension); } return null; }
/** * @param object $entity * @param PropertyMapping $mapping * * @return string */ public function name($entity, PropertyMapping $mapping) { $extension = $this->getExtension($mapping->getFile($entity)); if ($mapping->getUriPrefix()) { $namePieces[] = $mapping->getUriPrefix(); } $namePieces[] = sprintf('%s.%s', str_replace('.', '', uniqid('', true)), $extension); $name = implode('/', $namePieces); return $name; }
/** * {@inheritDoc} */ public function name($object, PropertyMapping $mapping) { $file = $mapping->getFile($object); $name = $file->getClientOriginalName(); if ($this->transliterate) { $name = $this->transliterate($name); } /** @var $file UploadedFile */ return uniqid() . '_' . $name; }
public function testDirectoryNamerIsCalled() { $object = new DummyEntity(); $prop = new PropertyMapping('file', 'fileName'); $prop->setMapping(array('upload_destination' => '/tmp')); $namer = $this->getMock('Vich\\UploaderBundle\\Naming\\DirectoryNamerInterface'); $namer->expects($this->once())->method('directoryName')->with($object, $prop)->will($this->returnValue('/other-dir')); $prop->setDirectoryNamer($namer); $this->assertEquals('/other-dir/', $prop->getUploadDir($object)); $this->assertEquals('/tmp', $prop->getUploadDestination()); }
/** * Construit le nom du fichier à partir de l'objet * @param [type] $object [description] * @param PropertyMapping $mapping [description] * @return [type] [description] */ public function name($object, PropertyMapping $mapping) { $file = $mapping->getFile($object); $extension = $this->getExtension($file); $name = uniqid(); if (is_null($object->getId())) { $name = parent::name($object, $mapping); } elseif (!is_null($extension)) { $name = $object->getId() . '.' . $extension; } return $name; }
/** * {@inheritdoc} */ public function name($object, PropertyMapping $mapping) { /** @var UploadedFile $file */ $file = $mapping->getFile($object); if (is_null($file)) { return false; } $extension = $file->guessExtension(); if ($extension == 'jpeg') { $extension = 'jpg'; } return sprintf('%s.%s', md5(uniqid()), $extension); }
/** * Construit le nom du fichier à partir de l'objet * @param [type] $object [description] * @param PropertyMapping $mapping [description] * @return [type] [description] */ public function name($object, PropertyMapping $mapping) { $file = $mapping->getFile($object); $extension = $this->getExtension($file); if (class_exists('\\Doctrine\\ODM\\MongoDB\\Id\\UuidGenerator')) { $generator = new \Doctrine\ODM\MongoDB\Id\UuidGenerator(); $name = $generator->generateV5($generator->generateV4(), php_uname('n')); } else { $name = uniqid(); } if ($extension = $this->getExtension($file)) { $name = sprintf('%s.%s', $name, $extension); } return $name; }
/** * @inheritDoc */ public function name($object, PropertyMapping $mapping) { if (empty($this->propertyPath)) { throw new \LogicException('The property to use can not be determined. Did you call the configure() method?'); } $file = $mapping->getFile($object); try { $name = $this->getPropertyValue($object, $this->propertyPath); } catch (NoSuchPropertyException $e) { throw new NameGenerationException(sprintf('File name could not be generated: property %s does not exist.', $this->propertyPath), $e->getCode(), $e); } if (empty($name)) { throw new NameGenerationException(sprintf('File name could not be generated: property %s is empty.', $this->propertyPath)); } if ($this->transliterate) { $name = $this->transliterate($name); } // append the file extension if there is one if ($extension = $this->getExtension($file)) { $name = sprintf('%s.%s', $name, $extension); } return $name; }
/** * Creates the property mapping from the read annotation and configured mapping. * * @param object $obj The object. * @param string $fieldName The field name. * @param array $mappingData The mapping data. * * @return PropertyMapping The property mapping. * @throws \InvalidArgumentException */ protected function createMapping($obj, $fieldName, array $mappingData) { if (!array_key_exists($mappingData['mapping'], $this->mappings)) { throw new \InvalidArgumentException(sprintf('No mapping named "%s" configured.', $mappingData['mapping'])); } $config = $this->mappings[$mappingData['mapping']]; $fileProperty = isset($mappingData['propertyName']) ? $mappingData['propertyName'] : $fieldName; $fileNameProperty = empty($mappingData['fileNameProperty']) ? $fileProperty . $this->defaultFilenameAttributeSuffix : $mappingData['fileNameProperty']; $mapping = new PropertyMapping($fileProperty, $fileNameProperty); $mapping->setMappingName($mappingData['mapping']); $mapping->setMapping($config); if ($config['namer']) { $mapping->setNamer($this->container->get($config['namer'])); } if ($config['directory_namer']) { $mapping->setDirectoryNamer($this->container->get($config['directory_namer'])); } return $mapping; }
/** * {@inheritDoc} */ protected function doResolvePath(PropertyMapping $mapping, $dir, $name) { return $mapping->getUploadDestination() . DIRECTORY_SEPARATOR . $dir . $name; }
/** * {@inheritDoc} */ public function name($object, PropertyMapping $mapping) { $file = $mapping->getFile($object); /** @var $file UploadedFile */ return uniqid() . '_' . $file->getClientOriginalName(); }
/** * Creates the property mapping from the read annotation and configured mapping. * * @param object $obj The object. * @param string $fieldName The field name. * @param array $mappingData The mapping data. * * @return PropertyMapping The property mapping. * @throws MappingNotFoundException */ protected function createMapping($obj, $fieldName, array $mappingData) { if (!array_key_exists($mappingData['mapping'], $this->mappings)) { throw MappingNotFoundException::createNotFoundForClassAndField($mappingData['mapping'], $this->getClassName($obj), $fieldName); } $config = $this->mappings[$mappingData['mapping']]; $fileProperty = isset($mappingData['propertyName']) ? $mappingData['propertyName'] : $fieldName; $fileNameProperty = empty($mappingData['fileNameProperty']) ? $fileProperty . $this->defaultFilenameAttributeSuffix : $mappingData['fileNameProperty']; $mapping = new PropertyMapping($fileProperty, $fileNameProperty); $mapping->setMappingName($mappingData['mapping']); $mapping->setMapping($config); if ($config['namer']['service']) { $namerConfig = $config['namer']; $namer = $this->container->get($namerConfig['service']); if (!empty($namerConfig['options']) && $namer instanceof ConfigurableInterface) { $namer->configure($namerConfig['options']); } else { if (!empty($namerConfig['options']) && !$namer instanceof ConfigurableInterface) { throw new \LogicException(sprintf('Namer %s can not receive options as it does not implement ConfigurableInterface.', $namerConfig['service'])); } } $mapping->setNamer($namer); } if ($config['directory_namer']) { $mapping->setDirectoryNamer($this->container->get($config['directory_namer'])); } return $mapping; }
public function name($object, \Vich\UploaderBundle\Mapping\PropertyMapping $mapping) { $file = $mapping->getFile($object); $extention = $file->guessExtension(); return uniqid() . '.' . $extention; }
protected function hasUploadedFile($obj, PropertyMapping $mapping) { $file = $mapping->getFile($obj); return $file !== null && $file instanceof UploadedFile; }
/** * @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')); }
/** * Creates a directory name for the file being uploaded. * * @param object $object The object the upload is attached to. * @param PropertyMapping $mapping The mapping to use to manipulate the given object. * * @return string The directory name. */ public function directoryName($object, PropertyMapping $mapping) { return $this->getDirectoryNameFromFilename($mapping->getFileName($object)); }
/** * Creates the property mapping from the read annotation and configured mapping. * * @param object $obj The object. * @param \Vich\UploaderBundle\Annotation\UploadableField $field The read annotation. * @return PropertyMapping The property mapping. * @throws \InvalidArgumentException */ protected function createMapping($obj, UploadableField $field) { $class = $this->adapter->getReflectionClass($obj); if (!array_key_exists($field->getMapping(), $this->mappings)) { throw new \InvalidArgumentException(sprintf('No mapping named "%s" configured.', $field->getMapping())); } $config = $this->mappings[$field->getMapping()]; $mapping = new PropertyMapping(); $mapping->setProperty($class->getProperty($field->getPropertyName())); $mapping->setFileNameProperty($class->getProperty($field->getFileNameProperty())); $mapping->setMappingName($field->getMapping()); $mapping->setMapping($config); if ($config['namer']) { $mapping->setNamer($this->container->get($config['namer'])); } return $mapping; }
/** * {@inheritdoc} */ public function directoryName($object, PropertyMapping $mapping) { $fileName = $mapping->getFileName($object); return substr($fileName, 0, 2); }
/** * Get filesystem adapter by key * * @param string $key * * @return \League\Flysystem\FilesystemInterface */ protected function getFilesystem(PropertyMapping $mapping) { return $this->mountManager->getFilesystem($mapping->getUploadDestination()); }
/** * Get filesystem adapter from the property mapping * * @param PropertyMapping $mapping * * @return \Gaufrette\Filesystem */ protected function getFilesystem(PropertyMapping $mapping) { return $this->filesystemMap->get($mapping->getUploadDestination()); }