Ejemplo n.º 1
0
 /**
  * @param string $fileKey
  * @param string $fileKeyWithFormat
  * @param Format $format
  *
  * @return
  */
 protected function generateThumbnail($fileKey, $fileKeyWithFormat, Format $format)
 {
     // check if has original picture
     try {
         $has = $this->fileSystem->has($fileKey);
     } catch (\OutOfBoundsException $e) {
         $has = false;
     }
     if (!$has) {
         throw new Exception\ImageDoesNotExistException();
     }
     // create thumbnail
     try {
         $blobOriginal = $this->fileSystem->read($fileKey);
     } catch (FileNotFound $e) {
         throw new Exception\ImageDoesNotExistException();
     }
     $imagine = new Imagine();
     $image = $imagine->load($blobOriginal);
     $resizedImage = Manipulator::resize($image, $format);
     $extension = $this->getExtension($fileKey);
     $blobResizedImage = $resizedImage->get($extension, array('jpeg_quality' => 90, 'png_compression_level' => 9));
     $this->fileSystem->write($fileKeyWithFormat, $blobResizedImage, true);
     return $blobResizedImage;
 }
Ejemplo n.º 2
0
 /**
  * @param Request $request
  * @param string  $filename
  *
  * @throws NotFoundHttpException If media is not found
  *
  * @return Response
  */
 public function showAction(Request $request, $filename)
 {
     if (!$this->filesystem->has($filename)) {
         throw new NotFoundHttpException(sprintf('Media "%s" not found', $filename));
     }
     $response = new Response($content = $this->filesystem->read($filename));
     $mime = $this->filesystem->mimeType($filename);
     if (($filter = $request->query->get('filter')) && null !== $mime && 0 === strpos($mime, 'image')) {
         try {
             $cachePath = $this->cacheManager->resolve($request, $filename, $filter);
             if ($cachePath instanceof Response) {
                 $response = $cachePath;
             } else {
                 $image = $this->imagine->load($content);
                 $response = $this->filterManager->get($request, $filter, $image, $filename);
                 $response = $this->cacheManager->store($response, $cachePath, $filter);
             }
         } catch (\RuntimeException $e) {
             if (0 === strpos($e->getMessage(), 'Filter not defined')) {
                 throw new HttpException(404, sprintf('The filter "%s" cannot be found', $filter), $e);
             }
             throw $e;
         }
     }
     if ($mime) {
         $response->headers->set('Content-Type', $mime);
     }
     return $response;
 }
Ejemplo n.º 3
0
 /**
  * Update attachment entity before upload
  *
  * @param File $entity
  */
 public function preUpload(File $entity)
 {
     if ($entity->isEmptyFile()) {
         if ($this->filesystem->has($entity->getFilename())) {
             $this->filesystem->delete($entity->getFilename());
         }
         $entity->setFilename(null);
         $entity->setExtension(null);
         $entity->setOriginalFilename(null);
     }
     if ($entity->getFile() !== null && $entity->getFile()->isFile()) {
         $entity->setOwner($this->securityFacadeLink->getService()->getLoggedUser());
         $file = $entity->getFile();
         if ($entity->getFilename() !== null && $this->filesystem->has($entity->getFilename())) {
             $this->filesystem->delete($entity->getFilename());
         }
         $entity->setExtension($file->guessExtension());
         if ($file instanceof UploadedFile) {
             $entity->setOriginalFilename($file->getClientOriginalName());
             $entity->setMimeType($file->getClientMimeType());
             $entity->setFileSize($file->getClientSize());
         } else {
             $entity->setOriginalFilename($file->getFileName());
             $entity->setMimeType($file->getMimeType());
             $entity->setFileSize($file->getSize());
         }
         $entity->setFilename(uniqid() . '.' . $entity->getExtension());
         if ($this->filesystem->getAdapter() instanceof MetadataSupporter) {
             $this->filesystem->getAdapter()->setMetadata($entity->getFilename(), ['contentType' => $entity->getMimeType()]);
         }
     }
 }
Ejemplo n.º 4
0
 /**
  * delete the given file
  *
  * @param string $file
  */
 public function deleteFile($file)
 {
     if (!$this->filesystem->has($file)) {
         return;
     }
     $this->filesystem->delete($file);
     $this->cacheManager->remove($file);
 }
Ejemplo n.º 5
0
 /**
  * @param mixed $data
  * @param int $mode
  * @return int
  */
 public function write($data, $mode = 0)
 {
     if ($mode & FILE_APPEND) {
         $data = $this->read() . $data;
     }
     if (!$this->gaufrette->has($this->filePath)) {
         $this->gaufrette->createFile($this->filePath);
     }
     return $this->gaufrette->write($this->filePath, $data, true);
 }
Ejemplo n.º 6
0
 /**
  * {@inheritdoc}
  */
 public function upload(ImageInterface $image)
 {
     if (!$image->hasFile()) {
         return;
     }
     if (null !== $image->getPath()) {
         $this->remove($image->getPath());
     }
     do {
         $hash = md5(uniqid(mt_rand(), true));
         $path = $this->expandPath($hash . '.' . $image->getFile()->guessExtension());
     } while ($this->filesystem->has($path));
     $image->setPath($path);
     $this->filesystem->write($image->getPath(), file_get_contents($image->getFile()->getPathname()));
 }
Ejemplo n.º 7
0
 function let(Filesystem $filesystem, ImageInterface $image)
 {
     $filesystem->has(Argument::any())->willReturn(false);
     $file = new File(__FILE__, 'img.jpg');
     $image->getFile()->willReturn($file);
     $this->beConstructedWith($filesystem);
 }
 /**
  * @test
  * @group functional
  */
 public function shouldDeleteFile()
 {
     $this->filesystem->write('foo', 'Some content');
     $this->assertTrue($this->filesystem->has('foo'));
     $this->filesystem->delete('foo');
     $this->assertFalse($this->filesystem->has('foo'));
 }
Ejemplo n.º 9
0
 /**
  * Predicate to know if file exists physically
  *
  * @param ProductMediaInterface $media
  *
  * @return bool
  */
 protected function fileExists(ProductMediaInterface $media)
 {
     if (null === $media->getFilename()) {
         return false;
     }
     return $this->filesystem->has($media->getFilename());
 }
Ejemplo n.º 10
0
 private function read(Filesystem $fs)
 {
     $profileFilename = Application::PROFILE_FILENAME;
     if ($fs->has($profileFilename)) {
         $this->processProfileContent($fs->read($profileFilename));
     }
 }
 /**
  * @test
  * @group functional
  */
 public function shouldWorkWithHiddenFiles()
 {
     $this->filesystem->write('.foo', 'hidden');
     $this->assertTrue($this->filesystem->has('.foo'));
     $this->assertContains('.foo', $this->filesystem->keys());
     $this->filesystem->delete('.foo');
     $this->assertFalse($this->filesystem->has('.foo'));
 }
Ejemplo n.º 12
0
 /**
  * {@inheritdoc}
  */
 public function upload(MediaInterface $media, $name = null, $test = false)
 {
     if (!$media->hasMedia()) {
         return;
     }
     if (null !== $media->getName()) {
         $this->remove($media->getName());
     }
     do {
         $hash = md5(uniqid(mt_rand(), true));
         if ($test === false) {
             $name = $hash . '.' . $media->getMedia()->guessExtension();
         }
     } while ($this->filesystem->has($name));
     $media->setName($name);
     if ($test === false) {
         $this->filesystem->write($media->getName(), file_get_contents($media->getMedia()->getPathname()));
     }
 }
Ejemplo n.º 13
0
 function it_uploads_without_removing(MediaInterface $media, Filesystem $filesystem, UploadedFile $file)
 {
     $media->hasMedia()->shouldBeCalled()->willReturn(true);
     $media->getName()->shouldBeCalled()->willReturn(null);
     $media->getMedia()->shouldBeCalled()->willReturn($file);
     $file->guessExtension()->shouldBeCalled()->willReturn('md');
     $filesystem->has(Argument::containingString('.md'))->shouldBeCalled()->willReturn(false);
     $media->setName(Argument::any())->shouldBeCalled()->willReturn($media);
     $media->getMedia()->shouldBeCalled()->willReturn($file);
     $file->getPathname()->shouldBeCalled()->willReturn(__DIR__ . '/../../../../../README.md');
     $filesystem->write(Argument::any(), file_get_contents(__DIR__ . '/../../../../../README.md'))->shouldBeCalled()->willReturn(Argument::type('int'));
     $this->upload($media);
 }
 public function cleanup(UserProfile $prop)
 {
     if ($prop->getProperty()->getFieldType() === ProfileProperty::TYPE_FILE) {
         /*$fileName = str_replace($this->getBaseFileUrl() . '/', '', $prop->getPropertyValue());
           if ( is_file($this->getBaseFilePath() . '/' . $fileName) ) {
               echo 'maybe removing ' . $this->getBaseFilePath() . '/' . $fileName;
               @unlink($this->getBaseFilePath() . '/' . $fileName);
           }*/
         $fileKey = AbstractUploader::extractKey($prop->getPropertyValue(), $this->awsBucket);
         if ($fileKey && $this->filesystem->has($fileKey)) {
             $this->filesystem->delete($fileKey);
         }
     }
 }
Ejemplo n.º 15
0
 public function has($id)
 {
     return $this->filesystem->has($id);
 }
Ejemplo n.º 16
0
 /**
  * @param \Gaufrette\Filesystem $filesystem
  */
 function it_check_if_file_with_key_exists_in_filesystem($filesystem)
 {
     $filesystem->has('filename')->willReturn(true);
     $this->exists()->shouldReturn(true);
     $filesystem->has('filename')->willReturn(false);
     $this->exists()->shouldReturn(false);
 }
Ejemplo n.º 17
0
 /**
  * @return string
  */
 public function getJson()
 {
     return $this->filesystem->has($this->configFile) ? $this->filesystem->get($this->configFile, true)->getContent() : '{}';
 }
Ejemplo n.º 18
0
 /**
  * Get Virtual Files Action
  *
  * @param string $file
  *
  * @throws NotFoundHttpException
  * @return Response
  */
 public function fileAction($file = null)
 {
     if (null == $file) {
         return $this->redirect($this->generateUrl('vfs_files', array('file' => '/')));
     }
     $cacheDirectory = $this->getParameter('adapter_cache_dir');
     $local = new LocalAdapter($cacheDirectory, true);
     $localadapter = new LocalAdapter($this->getParameter('adapter_files'));
     $adapter = new CacheAdapter($localadapter, $local, 3600);
     $fsAvatar = new Filesystem($adapter);
     if ($this->endswith($file, '/')) {
         if ($fsAvatar->has($file)) {
             $this->gvars['title'] = $this->translate('indexof', array('%dir%' => $this->generateUrl('vfs_files', array('file' => $file))));
             $this->gvars['pagetitle'] = $this->translate('indexof_raw', array('%dir%' => $this->generateUrl('vfs_files', array('file' => $file))));
             $path = substr($file, 1);
             $fs = $fsAvatar->listKeys($path);
             $listfiles = array();
             foreach ($fs['dirs'] as $key) {
                 $fulldir = $key . '/';
                 $dirname = $key;
                 if (substr($key, 0, strlen($path)) == $path) {
                     $dirname = substr($dirname, strlen($path));
                 }
                 if (!strstr($dirname, '/')) {
                     $listfiles[$fulldir] = $dirname;
                 }
             }
             foreach ($fs['keys'] as $key) {
                 $fullfile = $key;
                 $filename = $key;
                 if (substr($key, 0, strlen($path)) == $path) {
                     $filename = substr($filename, strlen($path));
                 }
                 if (!strstr($filename, '/')) {
                     $listfiles[$fullfile] = $filename;
                 }
             }
             $this->gvars['fs'] = $listfiles;
             return $this->renderResponse('AcfResBundle:Vfs:list_files.html.twig', $this->gvars);
         } else {
             throw new NotFoundHttpException();
         }
     }
     if ($fsAvatar->has($file)) {
         if ($fsAvatar->getAdapter()->isDirectory($file)) {
             $file .= '/';
             return $this->redirect($this->generateUrl('vfs_files', array('file' => $file)));
         }
         $reqFile = $fsAvatar->get($file);
         $response = new Response();
         $response->headers->set('Content-Type', 'binary');
         $response->setContent($reqFile->getContent());
         return $response;
     } else {
         throw new NotFoundHttpException();
     }
 }
Ejemplo n.º 19
0
 /**
  * Predicate to know if file exists physically
  *
  * @param AbstractProductMedia $media
  *
  * @return boolean
  */
 protected function fileExists(AbstractProductMedia $media)
 {
     return $this->filesystem->has($media->getFilename());
 }
Ejemplo n.º 20
0
 /**
  * Whether there is a file or directory at the given path.
  * 
  * @param string $path Filesystem-relative path to check for existence.
  * 
  * @return boolean
  */
 private function exists($path)
 {
     return $this->gaufrette->has($this->getGaufrettePath($path));
 }
Ejemplo n.º 21
0
 /**
  * Check with the filesystem if the image exists and update the image key cache.
  *
  * @param Image $image
  *
  * @return bool
  */
 protected function validateTag(Image $image)
 {
     $exists = $this->filesystem->has($image->getKey());
     $this->setImageExists($image, $exists);
     return $exists;
 }
Ejemplo n.º 22
0
 /**
  * @param Filesystem $localFilesyste
  * @param Filesystem $remoteFilesyste
  *
  * @throws \RuntimeException
  */
 private function validateFilesytems(Filesystem $localFilesyste, Filesystem $remoteFilesyste)
 {
     if (!$remoteFilesyste->has('shopware.php')) {
         throw new \RuntimeException("shopware.php not found in remote filesystem");
     }
     if (!$localFilesyste->has('shopware.php')) {
         throw new \RuntimeException("shopware.php not found in local filesystem");
     }
     if ($localFilesyste->checksum('shopware.php') != $remoteFilesyste->checksum('shopware.php')) {
         throw new \RuntimeException("Filesytems does not seem to match");
     }
 }
Ejemplo n.º 23
0
 /** @inheritDoc */
 public function isFile($path)
 {
     return !$this->isDirectory($path) && $this->gaufrette->has($this->getGaufrettePath($path));
 }
Ejemplo n.º 24
0
 /**
  * @inheritdoc
  */
 public function has($path)
 {
     return $this->filesystem->has($path);
 }
 /**
  * Algorithm to transform names from name.txt to name_i.txt and name_i.txt into name_{i++}.txt
  * when given key already exists and can't be reused.
  *
  * @param \FSi\DoctrineExtensions\Uploadable\Keymaker\KeymakerInterface $keymaker
  * @param object $object
  * @param string $property
  * @param mixed $id
  * @param string $fileName
  * @param integer $keyLength
  * @param string $keyPattern
  * @param \FSi\DoctrineExtensions\Uploadable\Filesystem $filesystem
  * @throws \FSi\DoctrineExtensions\Uploadable\Exception\RuntimeException
  * @return string
  */
 private function generateNewKey(KeymakerInterface $keymaker, $object, $property, $id, $fileName, $keyLength, $keyPattern, Filesystem $filesystem)
 {
     while ($filesystem->has($newKey = $keymaker->createKey($object, $property, $id, $fileName, $keyPattern))) {
         if ($match = preg_match('/(.*)_(\\d+)(\\.[^\\.]*)?$/', $fileName, $matches)) {
             $fileName = sprintf('%s_%s%s', $matches[1], strval($matches[2] + 1), isset($matches[3]) ? $matches[3] : '');
         } else {
             $fileParts = explode('.', $fileName);
             if (count($fileParts) > 1) {
                 $fileParts[count($fileParts) - 1] .= '_1';
                 $fileName = implode('.', $fileParts);
             } else {
                 $fileName .= '_1';
             }
         }
     }
     if (mb_strlen($newKey) > $keyLength) {
         throw new RuntimeException(sprintf('Generated key exceeded limit of %d characters (had %d characters).', $keyLength, mb_strlen($newKey)));
     }
     return $newKey;
 }
Ejemplo n.º 26
0
 public function getETag($id)
 {
     if ($this->filesystem->has($id)) {
         return $this->filesystem->read($id . '.etag');
     }
 }
Ejemplo n.º 27
0
 /**
  * {@inheritdoc}
  */
 public function childExists($name)
 {
     return $this->filesystem->has($this->prefix . $name);
 }