예제 #1
0
파일: Storage.php 프로젝트: bkwld/upchuck
 /**
  * Create a unique directory and filename
  *
  * @param string $filename
  * @param League\Flysystem\Filesystem|void $disk
  * @return string New path and filename
  */
 public function makeNestedAndUniquePath($filename, $disk = null)
 {
     // If no disk defined, get it from the current mount mananger
     if (empty($disk)) {
         $disk = $this->manager->getFilesystem('disk');
     }
     // Remove unsafe characters from the filename
     // https://regex101.com/r/mJ3sI5/1
     $filename = preg_replace('#[^\\w-_\\.]#i', '_', $filename);
     // Create nested folders to store the file in
     $dir = '';
     for ($i = 0; $i < $this->depth; $i++) {
         $dir .= str_pad(mt_rand(0, $this->length - 1), strlen($this->length), '0', STR_PAD_LEFT) . '/';
     }
     // If this file doesn't already exist, return it
     $path = $dir . $filename;
     if (!$disk->has($path)) {
         return $path;
     }
     // Get a unique filename for the file and return it
     $file = pathinfo($filename, PATHINFO_FILENAME);
     $i = 1;
     $ext = pathinfo($filename, PATHINFO_EXTENSION);
     while ($disk->has($path = $dir . $file . '-' . $i . '.' . $ext)) {
         $i++;
     }
     return $path;
 }
 /**
  * Retrieve a file content from a virtual file system.
  * In case no filesystem has this file registered, null is returned.
  *
  * @param string $path
  *
  * @throws NotLoadableException
  *
  * @return Binary|null|string
  */
 protected function retrieveContentFileFromVfs($path)
 {
     $content = null;
     $mimeType = null;
     foreach ($this->filesystemAliases as $alias) {
         $fs = $this->mountManager->getFilesystem($alias);
         if ($fs->has($path)) {
             //TODO: we should use readStream, the problem is that
             // \Liip\ImagineBundle\Model\Binary expects the full content...
             $content = $fs->read($path);
             $mimeType = $fs->getMimetype($path);
         }
     }
     if (null === $content) {
         // the path is not stored on any vfs
         return null;
     }
     if (false === $content) {
         throw new NotLoadableException(sprintf('Unable to read the file "%s" from the filesystem.', $path));
     }
     if (false === $mimeType || null === $mimeType) {
         return $content;
     }
     return new Binary($content, $mimeType);
 }
예제 #3
0
 /**
  * {@inheritdoc}
  */
 public function store(\SplFileInfo $localFile, $destFsAlias, $deleteRawFile = false)
 {
     $filesystem = $this->mountManager->getFilesystem($destFsAlias);
     $file = $this->factory->createFromRawFile($localFile, $destFsAlias);
     $error = sprintf('Unable to move the file "%s" to the "%s" filesystem.', $localFile->getPathname(), $destFsAlias);
     if (false === ($resource = fopen($localFile->getPathname(), 'r'))) {
         throw new FileTransferException($error);
     }
     try {
         $options = [];
         $mimeType = $file->getMimeType();
         if (null !== $mimeType) {
             $options['ContentType'] = $mimeType;
         }
         $isFileWritten = $filesystem->writeStream($file->getKey(), $resource, $options);
     } catch (FileExistsException $e) {
         throw new FileTransferException($error, $e->getCode(), $e);
     }
     if (false === $isFileWritten) {
         throw new FileTransferException($error);
     }
     $this->saver->save($file);
     if (true === $deleteRawFile) {
         $this->deleteRawFile($localFile);
     }
     return $file;
 }
예제 #4
0
 /**
  * Mount FTP
  *
  * @param  string   $host
  * @param  string   $username
  * @param  string   $password
  * @param  string[] $opt
  * @return $this
  */
 public function mountFtp($host, $username = '', $password = '', array $opt = [])
 {
     if (isset($this->settings[$host]['host'])) {
         $this->fs = $this->mounts->getFilesystem($host);
         return $this;
     }
     $opts = array_merge($opt, ['host' => $host, 'username' => $username, 'password' => $password]);
     $this->fs = new Filesystem(new Adapter\Ftp($opts));
     return $this;
 }
예제 #5
0
 /**
  * {@inheritdoc}
  */
 public function export($key, $localPathname, $storageAlias)
 {
     if (!is_dir(dirname($localPathname))) {
         throw new \LogicException(sprintf('The export directory "%s" does not exist.', dirname($localPathname)));
     }
     $storageFs = $this->mountManager->getFilesystem($storageAlias);
     $rawFile = $this->fileFetcher->fetch($storageFs, $key);
     $copied = $this->copyFile($rawFile->getPathname(), $localPathname);
     //TODO: files should also be copied in the archive folder to be able to generate the ZIP file on the fly
     $this->localFs->remove($rawFile->getPathname());
     return $copied;
 }
예제 #6
0
파일: Upchuck.php 프로젝트: bkwld/cloner
 /**
  * Duplicate a file given it's URL
  *
  * @param  string $url
  * @return string
  */
 public function duplicate($url)
 {
     // Make the destination path
     $current_path = $this->helpers->path($url);
     $filename = basename($current_path);
     $dst_disk = $this->disks ? $this->disks->getFilesystem('dst') : $this->disk;
     $new_path = $this->storage->makeNestedAndUniquePath($filename, $dst_disk);
     // Copy, supporting alternative destination disks
     if ($this->disks) {
         $this->disks->copy('src://' . $current_path, 'dst://' . $new_path);
     } else {
         $this->disk->copy($current_path, $new_path);
     }
     // Return the Upchuck URL
     return $this->helpers->url($new_path);
 }
 /**
  * @param ProductInterface   $fromProduct
  * @param ProductInterface   $toProduct
  * @param AttributeInterface $fromAttribute
  * @param AttributeInterface $toAttribute
  * @param string             $fromLocale
  * @param string             $toLocale
  * @param string             $fromScope
  * @param string             $toScope
  */
 protected function copySingleValue(ProductInterface $fromProduct, ProductInterface $toProduct, AttributeInterface $fromAttribute, AttributeInterface $toAttribute, $fromLocale, $toLocale, $fromScope, $toScope)
 {
     $fromValue = $fromProduct->getValue($fromAttribute->getCode(), $fromLocale, $fromScope);
     if (null !== $fromValue) {
         $toValue = $toProduct->getValue($toAttribute->getCode(), $toLocale, $toScope);
         if (null === $toValue) {
             $toValue = $this->productBuilder->addProductValue($toProduct, $toAttribute, $toLocale, $toScope);
         }
         $file = null;
         if (null !== $fromValue->getMedia()) {
             $filesystem = $this->mountManager->getFilesystem(FileStorage::CATALOG_STORAGE_ALIAS);
             $rawFile = $this->rawFileFetcher->fetch($filesystem, $fromValue->getMedia()->getKey());
             $file = $this->rawFileStorer->store($rawFile, FileStorage::CATALOG_STORAGE_ALIAS, false);
             $file->setOriginalFilename($fromValue->getMedia()->getOriginalFilename());
         }
         $toValue->setMedia($file);
     }
 }
예제 #8
0
 /**
  * {@inheritdoc}
  */
 public function store(\SplFileInfo $localFile, $destFsAlias)
 {
     $filesystem = $this->mountManager->getFilesystem($destFsAlias);
     $storageData = $this->pathGenerator->generate($localFile);
     $file = $this->factory->create($localFile, $storageData, $destFsAlias);
     $error = sprintf('Unable to move the file "%s" to the "%s" filesystem.', $localFile->getPathname(), $destFsAlias);
     if (false === ($resource = fopen($localFile->getPathname(), 'r'))) {
         throw new FileTransferException($error);
     }
     try {
         $isFileWritten = $filesystem->writeStream($file->getKey(), $resource);
     } catch (FileExistsException $e) {
         throw new FileTransferException($error, $e->getCode(), $e);
     }
     if (false === $isFileWritten) {
         throw new FileTransferException($error);
     }
     $this->saver->save($file, ['flush_only_object' => true]);
     $this->deleteRawFile($localFile);
     return $file;
 }
예제 #9
0
 /**
  * @param string $filename
  *
  * @throws NotFoundHttpException
  *
  * @return StreamedFileResponse
  */
 public function downloadAction($filename)
 {
     $filename = urldecode($filename);
     foreach ($this->filesystemAliases as $alias) {
         $fs = $this->mountManager->getFilesystem($alias);
         if ($fs->has($filename)) {
             $stream = $fs->readStream($filename);
             return new StreamedFileResponse($stream);
         }
     }
     throw $this->createNotFoundException(sprintf('File with key "%s" could not be found.', $filename));
 }
 /**
  * @param string $filename
  *
  * @throws NotFoundHttpException
  *
  * @return StreamedFileResponse
  */
 public function downloadAction($filename)
 {
     $filename = urldecode($filename);
     foreach ($this->filesystemAliases as $alias) {
         $fs = $this->mountManager->getFilesystem($alias);
         if ($fs->has($filename)) {
             $stream = $fs->readStream($filename);
             $headers = [];
             if (null !== ($fileInfo = $this->fileInfoRepository->findOneByIdentifier($filename))) {
                 $headers['Content-Disposition'] = sprintf('attachment; filename="%s"', $fileInfo->getOriginalFilename());
             }
             return new StreamedFileResponse($stream, 200, $headers);
         }
     }
     throw $this->createNotFoundException(sprintf('File with key "%s" could not be found.', $filename));
 }
 function let(MountManager $mountManager, FilesystemInterface $filesystem)
 {
     $mountManager->getFilesystem(FileStorage::CATALOG_STORAGE_ALIAS)->willReturn($filesystem);
     $this->beConstructedWith($mountManager, [FileStorage::CATALOG_STORAGE_ALIAS]);
 }
 /**
  * Get the filesystem with the corresponding name.
  *
  * @param string $name
  *
  * @throws \LogicException
  *
  * @return FilesystemInterface
  */
 public function getFilesystem($name)
 {
     return $this->mountManager->getFilesystem($name);
 }
예제 #13
0
파일: Manager.php 프로젝트: aleksabp/bolt
 public function getFilesystem($prefix = null)
 {
     $prefix = isset($this->filesystems[$prefix]) ? $prefix : static::DEFAULT_PREFIX;
     return parent::getFilesystem($prefix);
 }
예제 #14
0
 /**
  * @expectedException  LogicException
  */
 public function testUndefinedFilesystem()
 {
     $manager = new MountManager();
     $manager->getFilesystem('prefix');
 }
 function it_copies_when_a_product_value_has_a_media_but_not_the_target_value($builder, $attrValidatorHelper, FileInfoInterface $fromMedia, FileInfoInterface $toMedia, \SplFileInfo $rawFile, FileInfoInterface $fileInfo, FileInfoInterface $fromMedia, FileInfoInterface $toMedia, AttributeInterface $fromAttribute, AttributeInterface $toAttribute, ProductInterface $product, ProductValueInterface $fromProductValue, FileStorerInterface $fileStorer, ProductValueInterface $toProductValue, FileFetcherInterface $fileFetcher, MountManager $mountManager, FilesystemInterface $fileSystem)
 {
     $fromLocale = 'fr_FR';
     $toLocale = 'fr_FR';
     $toScope = 'mobile';
     $fromScope = 'mobile';
     $fromAttribute->getCode()->willReturn('fromAttributeCode');
     $toAttribute->getCode()->willReturn('toAttributeCode');
     $attrValidatorHelper->validateLocale(Argument::cetera())->shouldBeCalled();
     $attrValidatorHelper->validateScope(Argument::cetera())->shouldBeCalled();
     $fromProductValue->getMedia()->willReturn($fromMedia);
     $fromMedia->getOriginalFilename()->willReturn('akeneo.jpg');
     $fromMedia->getKey()->willReturn('key');
     $mountManager->getFilesystem(FileStorage::CATALOG_STORAGE_ALIAS)->willReturn($fileSystem);
     $fileFetcher->fetch($fileSystem, 'key')->willReturn($rawFile);
     $fileStorer->store($rawFile, FileStorage::CATALOG_STORAGE_ALIAS, false)->willReturn($fileInfo);
     $product->getValue('fromAttributeCode', $fromLocale, $fromScope)->willReturn($fromProductValue);
     $product->getValue('toAttributeCode', $toLocale, $toScope)->willReturn(null);
     $builder->addProductValue($product, $toAttribute, $toLocale, $toScope)->willReturn($toProductValue);
     $toProductValue->getMedia()->willReturn($toMedia);
     $toProductValue->setMedia($fileInfo)->shouldBeCalled();
     $this->copyAttributeData($product, $product, $fromAttribute, $toAttribute, ['from_locale' => $fromLocale, 'to_locale' => $toLocale, 'from_scope' => $fromScope, 'to_scope' => $toScope]);
 }