Navigation Menu

Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Jul 7, 2020
1 parent 5f43410 commit a3e0212
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
23 changes: 9 additions & 14 deletions src/Middleware/GlideMiddleware.php
Expand Up @@ -56,13 +56,6 @@ class GlideMiddleware implements MiddlewareInterface, EventDispatcherInterface
*/
protected $_path = '';

/**
* Glide params for generating thumbnails.
*
* @var array
*/
protected $_params = [];

/**
* Constructor.
*
Expand All @@ -89,15 +82,14 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
{
$uri = $request->getUri();
$this->_path = urldecode($uri->getPath());
parse_str($uri->getQuery(), $this->_params);

$config = $this->getConfig();

if ($config['path'] && strpos($this->_path, $config['path']) !== 0) {
return $handler->handle($request);
}

$this->_checkSignature();
$this->_checkSignature($request);

$server = $this->_getServer($config['server']);

Expand Down Expand Up @@ -150,10 +142,11 @@ protected function _getServer($config): Server
/**
* Check signature token if secure URLs are enabled.
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request.
* @throws \ADmad\Glide\Exception\SignatureException
* @return void
*/
protected function _checkSignature()
protected function _checkSignature(ServerRequestInterface $request)
{
if (!$this->getConfig('security.secureUrls')) {
return;
Expand All @@ -163,7 +156,7 @@ protected function _checkSignature()
try {
SignatureFactory::create($signKey)->validateRequest(
$this->_path,
$this->_params
$request->getQueryParams()
);
} catch (Exception $exception) {
throw new SignatureException($exception->getMessage(), null, $exception);
Expand Down Expand Up @@ -215,9 +208,11 @@ protected function _checkModified(ServerRequestInterface $request, Server $serve
*/
protected function _getResponse(ServerRequestInterface $request, Server $server): ?ResponseInterface
{
$queryParams = $request->getQueryParams();

if (
(empty($this->_params)
|| (count($this->_params) === 1 && isset($this->_params['s']))
(empty($queryParams)
|| (count($queryParams) === 1 && isset($queryParams['s']))
)
&& $this->getConfig('originalPassThrough')
) {
Expand All @@ -237,7 +232,7 @@ protected function _getResponse(ServerRequestInterface $request, Server $server)
}

try {
$response = $server->getImageResponse($this->_path, $this->_params);
$response = $server->getImageResponse($this->_path, $request->getQueryParams());
} catch (Exception $exception) {
return $this->_handleException($request, $exception);
}
Expand Down
10 changes: 5 additions & 5 deletions tests/TestCase/Middleware/GlideMiddlewareTest.php
Expand Up @@ -114,13 +114,13 @@ public function testSecureUrl()
$signature = new Signature(Security::getSalt());
$sig = $signature->generateSignature('/images/cake logo.png', ['w' => 100]);

$request = ServerRequestFactory::fromGlobals([
'REQUEST_URI' => '/images/cake%20logo.png',
'QUERY_STRING' => 'w=100&s=' . $sig,
]);
$request = ServerRequestFactory::fromGlobals(
['REQUEST_URI' => '/images/cake%20logo.png'],
['w' => 100, 's' => $sig]
);

$middleware = new GlideMiddleware($this->config);
$response = $middleware->process($request, $this->handler);
$middleware->process($request, $this->handler);

$this->assertTrue(is_dir(TMP . 'cache/cake logo.png'));
}
Expand Down

0 comments on commit a3e0212

Please sign in to comment.