Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak response creation #26

Merged
merged 5 commits into from
Oct 21, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 10 additions & 18 deletions Controller/ImagineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
class ImagineController
{
/**
* @var Liip\ImagineBundle\Imagine\DataLoader\LoaderInterface
* @var LoaderInterface
*/
private $dataLoader;

/**
* @var Liip\ImagineBundle\Imagine\Filter\FilterManager
* @var FilterManager
*/
private $filterManager;

Expand All @@ -26,16 +26,17 @@ class ImagineController
private $webRoot;

/**
* @var Liip\ImagineBundle\Imagine\CachePathResolver
* @var CachePathResolver
*/
private $cachePathResolver;

/**
* Constructor
*
* @param Liip\ImagineBundle\Imagine\DataLoader\LoaderInterface $dataLoader
* @param Liip\ImagineBundle\Imagine\Filter\FilterManager $filterManager
* @param Liip\ImagineBundle\Imagine\CachePathResolver $cachePathResolver
* @param LoaderInterface $dataLoader
* @param FilterManager $filterManager
* @param string $webRoot
* @param CachePathResolver $cachePathResolver
*/
public function __construct(LoaderInterface $dataLoader, FilterManager $filterManager, $webRoot, CachePathResolver $cachePathResolver = null)
{
Expand Down Expand Up @@ -69,21 +70,12 @@ public function filterAction(Request $request, $path, $filter)
}

$image = $this->dataLoader->find($path);
$targetFormat = pathinfo($path, PATHINFO_EXTENSION);
$image = $this->filterManager->get($filter, $image, $targetFormat);
$response = $this->filterManager->get($request, $filter, $image, $path);

if ($targetPath) {
$this->cachePathResolver->store($targetPath, $image);
$statusCode = 201;
} else {
$statusCode = 200;
$response = $this->cachePathResolver->store($response, $targetPath);
}

$contentType = $request->getMimeType($targetFormat);
if (empty($contentType)) {
$contentType = 'image/'.$targetFormat;
}

return new Response($image, $statusCode, array('Content-Type' => $contentType));
return $response;
}
}
46 changes: 30 additions & 16 deletions Imagine/CachePathResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

namespace Liip\ImagineBundle\Imagine;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpKernel\Util\Filesystem;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Request,
Symfony\Component\HttpFoundation\Response,
Symfony\Component\HttpFoundation\RedirectResponse,
Symfony\Component\Routing\RouterInterface,
Symfony\Component\HttpKernel\Util\Filesystem,
Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class CachePathResolver
{
/**
* @var Symfony\Component\Routing\RouterInterface
* @var RouterInterface
*/
private $router;

/**
* @var Symfony\Component\HttpKernel\Util\Filesystem
* @var Filesystem
*/
private $filesystem;

Expand All @@ -28,10 +29,10 @@ class CachePathResolver
/**
* Constructs cache path resolver with a given web root and cache prefix
*
* @param Symfony\Component\HttpFoundation\Request $request
* @param Symfony\Component\Routing\RouterInterface $router
* @param Symfony\Component\HttpKernel\Util\Filesystem $filesystem
* @param string $webRoot
* @param Request $request
* @param RouterInterface $router
* @param Filesystem $filesystem
* @param string $webRoot
*/
public function __construct(RouterInterface $router, Filesystem $filesystem, $webRoot)
{
Expand All @@ -46,6 +47,8 @@ public function __construct(RouterInterface $router, Filesystem $filesystem, $we
* @param string $path
* @param string $filter
* @param boolean $absolute
*
* @return string
*/
public function getBrowserPath($targetPath, $filter, $absolute = false)
{
Expand All @@ -70,6 +73,8 @@ public function getBrowserPath($targetPath, $filter, $absolute = false)
* @param Request $request
* @param string $path
* @param string $filter
*
* @return string
*/
public function resolve(Request $request, $targetPath, $filter)
{
Expand All @@ -91,15 +96,20 @@ public function resolve(Request $request, $targetPath, $filter)
// if the file has already been cached, we're probably not rewriting
// correctly, hence make a 301 to proper location, so browser remembers
if (file_exists($targetPath)) {
return new Response('', 301, array(
'location' => $request->getBasePath().$browserPath
));
return new RedirectResponse($request->getBasePath().$targetPath);
}

return $targetPath;
}

public function store($targetPath, $image)
/**
* @throws \RuntimeException
* @param Response $response
* @param string $targetPath
*
* @return Response
*/
public function store(Response $response, $targetPath)
{
$dir = pathinfo($targetPath, PATHINFO_DIRNAME);

Expand All @@ -109,6 +119,10 @@ public function store($targetPath, $image)
));
}

file_put_contents($targetPath, $image);
file_put_contents($targetPath, $response->getContent());

$response->setStatusCode(201);

return $response;
}
}
58 changes: 49 additions & 9 deletions Imagine/Filter/FilterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,48 @@

namespace Liip\ImagineBundle\Imagine\Filter;

use Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface;
use Symfony\Component\HttpFoundation\Request,
Symfony\Component\HttpFoundation\Response;

use Imagine\Exception\InvalidArgumentException;
use Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface;

class FilterManager
{
/**
* @var array
*/
private $filters;

/**
* @var array
*/
private $loaders;
private $services;

/**
* @param array $filters
*/
public function __construct(array $filters = array())
{
$this->filters = $filters;
$this->loaders = array();
$this->services = array();
}

/**
* @param $name
* @param Loader\LoaderInterface $loader
*
* @return void
*/
public function addLoader($name, LoaderInterface $loader)
{
$this->loaders[$name] = $loader;
}

/**
* @param $filter
*
* @return array
*/
public function getFilterConfig($filter)
{
if (empty($this->filters[$filter])) {
Expand All @@ -33,10 +53,18 @@ public function getFilterConfig($filter)
return $this->filters[$filter];
}

public function get($filter, $image, $format = 'png')
/**
* @param Request $request
* @param $filter
* @param $image
* @param string $localPath
*
* @return Response
*/
public function get(Request $request, $filter, $image, $localPath)
{
if (!isset($this->filters[$filter])) {
throw new InvalidArgumentException(sprintf(
throw new \InvalidArgumentException(sprintf(
'Could not find image filter "%s"', $filter
));
}
Expand All @@ -45,17 +73,29 @@ public function get($filter, $image, $format = 'png')

foreach ($config['filters'] as $filter => $options) {
if (!isset($this->loaders[$filter])) {
throw new InvalidArgumentException(sprintf(
throw new \InvalidArgumentException(sprintf(
'Could not find loader for "%s" filter type', $filter
));
}
$image = $this->loaders[$filter]->load($image, $options);
}

if (empty($config['format'])) {
$format = pathinfo($localPath, PATHINFO_EXTENSION);
$format = $format ?: 'png';
} else {
$format = $config['format'];
}

$quality = empty($config['quality']) ? 100 : $config['quality'];
$format = empty($config['format']) ? $format : $config['format'];

$image = $image->get($format, array('quality' => $quality));

return $image;
$contentType = $request->getMimeType($format);
if (empty($contentType)) {
$contentType = 'image/'.$format;
}

return new Response($image, 200, array('Content-Type' => $contentType));
}
}