From a3e02123eb83cd6c5a47d642bf950a6467bf4994 Mon Sep 17 00:00:00 2001 From: ADmad Date: Tue, 7 Jul 2020 12:44:18 +0530 Subject: [PATCH] Optimize code --- src/Middleware/GlideMiddleware.php | 23 ++++++++----------- .../Middleware/GlideMiddlewareTest.php | 10 ++++---- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/Middleware/GlideMiddleware.php b/src/Middleware/GlideMiddleware.php index 7edda82..ed4bdd5 100644 --- a/src/Middleware/GlideMiddleware.php +++ b/src/Middleware/GlideMiddleware.php @@ -56,13 +56,6 @@ class GlideMiddleware implements MiddlewareInterface, EventDispatcherInterface */ protected $_path = ''; - /** - * Glide params for generating thumbnails. - * - * @var array - */ - protected $_params = []; - /** * Constructor. * @@ -89,7 +82,6 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface { $uri = $request->getUri(); $this->_path = urldecode($uri->getPath()); - parse_str($uri->getQuery(), $this->_params); $config = $this->getConfig(); @@ -97,7 +89,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface return $handler->handle($request); } - $this->_checkSignature(); + $this->_checkSignature($request); $server = $this->_getServer($config['server']); @@ -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; @@ -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); @@ -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') ) { @@ -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); } diff --git a/tests/TestCase/Middleware/GlideMiddlewareTest.php b/tests/TestCase/Middleware/GlideMiddlewareTest.php index f6714c9..7f2a357 100644 --- a/tests/TestCase/Middleware/GlideMiddlewareTest.php +++ b/tests/TestCase/Middleware/GlideMiddlewareTest.php @@ -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')); }