Ejemplo n.º 1
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;
 }
 /**
  * {@inheritdoc}
  */
 public function getImageUrl($fileType, $filter)
 {
     $fileKey = sprintf('%s_default_image', $fileType);
     if (!$this->cacheManager->isStored($fileKey, $filter)) {
         $binary = $this->getImageBinary($fileType);
         $this->cacheManager->store($this->filterManager->applyFilter($binary, $filter), $fileKey, $filter);
     }
     return $this->cacheManager->resolve($fileKey, $filter);
 }
Ejemplo n.º 3
0
 /**
  * @param string $path
  * @param string $filter
  * @param Image  $image
  *
  * @return string
  */
 public function getFormattedImage($path, $filter, Image $image = null)
 {
     if (!$this->cacheManager->isStored($path, $filter)) {
         $config = $this->filterManager->getFilterConfiguration()->get($filter);
         if (isset($config['filters']['focused_crop'])) {
             $config['filters']['focused_crop']['object'] = $image;
         }
         $binary = $this->dataManager->find($filter, $path);
         $binary = $this->filterManager->applyFilter($binary, $filter, $config);
         $this->cacheManager->store($binary, $path, $filter);
     }
     return $this->cacheManager->resolve($path, $filter);
 }
Ejemplo n.º 4
0
 /**
  * @param string $imageData
  * @param string $filter
  * @throws \Exception
  * @return bool|string
  */
 public function handle($imageData, $filter)
 {
     try {
         $binary = $this->createBinary($imageData);
         $pictureName = time() . '.' . $binary->getFormat();
         $this->imagineCache->store($this->filterManager->applyFilter($binary, $filter), $pictureName, $filter);
         $this->imagineCache->resolve($pictureName, $filter);
         return $this->getUploadDir() . $filter . '/' . $pictureName;
     } catch (\Exception $e) {
         $this->error = $e->getMessage();
     }
     return false;
 }
 /**
  * This action applies a given filter to a given image, optionally saves the image and outputs it to the browser at the same time.
  *
  * @param Request $request
  * @param string $path
  * @param string $filter
  *
  * @return Response
  */
 public function filterAction(Request $request, $path, $filter)
 {
     $targetPath = $this->cacheManager->resolve($request, $path, $filter);
     if ($targetPath instanceof Response) {
         return $targetPath;
     }
     $image = $this->dataManager->find($filter, $path);
     $response = $this->filterManager->get($request, $filter, $image, $path);
     if ($targetPath) {
         $response = $this->cacheManager->store($response, $targetPath, $filter);
     }
     return $response;
 }
Ejemplo n.º 6
0
 /**
  * This action applies a given filter to a given image, optionally saves the image and outputs it to the browser at the same time.
  *
  * @param Request $request
  * @param string  $hash
  * @param string  $path
  * @param string  $filter
  *
  * @throws \RuntimeException
  * @throws BadRequestHttpException
  *
  * @return RedirectResponse
  */
 public function filterRuntimeAction(Request $request, $hash, $path, $filter)
 {
     try {
         $filters = $request->query->get('filters', array());
         if (!is_array($filters)) {
             throw new NotFoundHttpException(sprintf('Filters must be an array. Value was "%s"', $filters));
         }
         if (true !== $this->signer->check($hash, $path, $filters)) {
             throw new BadRequestHttpException(sprintf('Signed url does not pass the sign check for path "%s" and filter "%s" and runtime config %s', $path, $filter, json_encode($filters)));
         }
         try {
             $binary = $this->dataManager->find($filter, $path);
         } catch (NotLoadableException $e) {
             if ($defaultImageUrl = $this->dataManager->getDefaultImageUrl($filter)) {
                 return new RedirectResponse($defaultImageUrl);
             }
             throw new NotFoundHttpException(sprintf('Source image could not be found for path "%s" and filter "%s"', $path, $filter), $e);
         }
         $rcPath = $this->cacheManager->getRuntimePath($path, $filters);
         $this->cacheManager->store($this->filterManager->applyFilter($binary, $filter, array('filters' => $filters)), $rcPath, $filter);
         return new RedirectResponse($this->cacheManager->resolve($rcPath, $filter), 301);
     } catch (NonExistingFilterException $e) {
         $message = sprintf('Could not locate filter "%s" for path "%s". Message was "%s"', $filter, $hash . '/' . $path, $e->getMessage());
         if (null !== $this->logger) {
             $this->logger->debug($message);
         }
         throw new NotFoundHttpException($message, $e);
     } catch (RuntimeException $e) {
         throw new \RuntimeException(sprintf('Unable to create image for path "%s" and filter "%s". Message was "%s"', $hash . '/' . $path, $filter, $e->getMessage()), 0, $e);
     }
 }
 /**
  * Check if this could mean an image document was modified (check resource,
  * file and image).
  *
  * @param LifecycleEventArgs $args
  */
 private function invalidateCache(LifecycleEventArgs $args)
 {
     $object = $args->getObject();
     // If we hear about the Resource which is nested in a File, we get the
     // parent. instanceof can handle the case where Resource is not a known
     // class - the condition will just be false in that case.
     if ($object instanceof Resource) {
         $object = $object->getParent();
     }
     if (!$object instanceof ImageInterface) {
         return;
     }
     if (null === $this->request) {
         // do not fail on CLI
         return;
     }
     foreach ($this->filters as $filter) {
         $path = $this->manager->resolve($this->mediaManager->getUrlSafePath($object), $filter);
         if ($path instanceof RedirectResponse) {
             $path = $path->getTargetUrl();
         }
         // TODO: this might not be needed https://github.com/liip/LiipImagineBundle/issues/162
         if (false !== strpos($path, $filter)) {
             $path = substr($path, strpos($path, $filter) + strlen($filter));
         }
         $this->manager->remove($path, $filter);
     }
 }
 public function testFallbackToDefaultResolver()
 {
     $response = new Response('', 200);
     $request = new Request();
     $resolver = $this->getMockResolver();
     $resolver->expects($this->once())->method('resolve')->with($request, 'cats.jpeg', 'thumbnail')->will($this->returnValue('/thumbs/cats.jpeg'));
     $resolver->expects($this->once())->method('store')->with($response, '/thumbs/cats.jpeg', 'thumbnail')->will($this->returnValue($response));
     $resolver->expects($this->once())->method('remove')->with('/thumbs/cats.jpeg', 'thumbnail')->will($this->returnValue(true));
     $config = $this->getMockFilterConfiguration();
     $config->expects($this->exactly(3))->method('get')->with('thumbnail')->will($this->returnValue(array('size' => array(180, 180), 'mode' => 'outbound', 'cache' => null)));
     $cacheManager = new CacheManager($config, $this->getMockRouter(), $this->fixturesDir . '/assets', 'default');
     $cacheManager->addResolver('default', $resolver);
     // Resolve fallback to default resolver
     $this->assertEquals('/thumbs/cats.jpeg', $cacheManager->resolve($request, 'cats.jpeg', 'thumbnail'));
     // Store fallback to default resolver
     $this->assertEquals($response, $cacheManager->store($response, '/thumbs/cats.jpeg', 'thumbnail'));
     // Remove fallback to default resolver
     $this->assertTrue($cacheManager->remove('/thumbs/cats.jpeg', 'thumbnail'));
 }
 /**
  * This action applies a given filter to a given image, optionally saves the image and outputs it to the browser at the same time.
  *
  * @param Request $request
  * @param string  $hash
  * @param string  $path
  * @param string  $filter
  *
  * @throws \RuntimeException
  * @throws BadRequestHttpException
  *
  * @return RedirectResponse
  */
 public function filterRuntimeAction(Request $request, $hash, $path, $filter)
 {
     try {
         $filters = $request->query->get('filters', array());
         if (true !== $this->signer->check($hash, $path, $filters)) {
             throw new BadRequestHttpException(sprintf('Signed url does not pass the sign check for path "%s" and filter "%s" and runtime config %s', $path, $filter, json_encode($filters)));
         }
         try {
             $binary = $this->dataManager->find($filter, $path);
         } catch (NotLoadableException $e) {
             throw new NotFoundHttpException(sprintf('Source image could not be found for path "%s" and filter "%s"', $path, $filter), $e);
         }
         $cachePrefix = 'rc/' . $hash;
         $this->cacheManager->store($this->filterManager->applyFilter($binary, $filter, array('filters' => $filters)), $cachePrefix . '/' . $path, $filter);
         return new RedirectResponse($this->cacheManager->resolve($cachePrefix . '/' . $path, $filter), 301);
     } catch (RuntimeException $e) {
         throw new \RuntimeException(sprintf('Unable to create image for path "%s" and filter "%s". Message was "%s"', $hash . '/' . $path, $filter, $e->getMessage()), 0, $e);
     }
 }
Ejemplo n.º 10
0
    public function testShouldReturnUrlChangedInPostResolveEvent()
    {
        $dispatcher = $this->createEventDispatcherMock();
        $dispatcher
            ->expects($this->at(1))
            ->method('dispatch')
            ->with(ImagineEvents::POST_RESOLVE, $this->isInstanceOf('Liip\ImagineBundle\Events\CacheResolveEvent'))
            ->will($this->returnCallback(function ($name, $event) {
                $event->setUrl('changed_url');
            }))
        ;

        $cacheManager = new CacheManager(
            $this->createFilterConfigurationMock(),
            $this->createRouterMock(),
            new Signer('secret'),
            $dispatcher
        );
        $cacheManager->addResolver('default', $this->createResolverMock());

        $url = $cacheManager->resolve('cats.jpg', 'thumbnail');

        $this->assertEquals('changed_url', $url);
    }
Ejemplo n.º 11
0
 public function getFilteredPath(File $file, $filter)
 {
     return $this->cacheManager->isStored('uploaded/' . $file->getId() . '.' . $file->getExtention(), $filter) ? $this->cacheManager->resolve('uploaded/' . $file->getId() . '.' . $file->getExtention(), $filter) : $this->router->generate('digi_file_filter', array('file' => $file->getId() . '.' . $file->getExtention(), 'filter' => $filter), true);
 }
Ejemplo n.º 12
0
 /**
  * {@inheritdoc}
  */
 public function resolve($path, $filter, $resolver = null)
 {
     $filterConf = $this->filterConfig->get($filter);
     $path = $this->changeFileExtension($path, $filterConf['format']);
     return parent::resolve($path, $filter, $resolver);
 }