Skip to content

Commit

Permalink
Merge pull request #32 from ADmad/glide-2-official
Browse files Browse the repository at this point in the history
Upgrade to Glide 2 (Flysystem 2).
  • Loading branch information
ADmad committed Jun 27, 2021
2 parents 3de5b2e + 0811a91 commit ce35455
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 41 deletions.
22 changes: 7 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,18 @@ jobs:
composer-opts: '--prefer-lowest'

steps:
- uses: actions/checkout@v1
with:
fetch-depth: 1
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extension-csv: mbstring, intl
extensions: mbstring, intl
coverage: pcov

- name: Composer install
run: |
if [[ ${{ matrix.php-version }} == '8.0' ]]; then
composer update --ignore-platform-reqs
else
composer update ${{ matrix.composer-opts }}
fi
composer update ${{ matrix.composer-opts }}
- name: Run PHPUnit
run: |
Expand All @@ -51,20 +45,18 @@ jobs:
runs-on: ubuntu-18.04

steps:
- uses: actions/checkout@v1
with:
fetch-depth: 1
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
extension-csv: mbstring, intl
extensions: mbstring, intl
coverage: none
tools: cs2pr, psalm:^4.1
tools: cs2pr, psalm:^4.8

- name: Composer Install
run: composer require cakephp/cakephp-codesniffer:^4.1
run: composer require cakephp/cakephp-codesniffer:^4.5

- name: Run phpcs
run: vendor/bin/phpcs --report=checkstyle -q --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/ | cs2pr
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ In your `config/routes.php` setup the `GlideMiddleware` for required scope which
intercepts requests and serves images generated by Glide. For e.g.:

```php
Router::scope('/images', function ($routes) {
$routes->scope('/images', function ($routes) {
$routes->registerMiddleware('glide', new \ADmad\Glide\Middleware\GlideMiddleware([
// Run this middleware only for URLs starting with specified string. Default null.
// Setting this option is required only if you want to setup the middleware
Expand All @@ -55,7 +55,7 @@ Router::scope('/images', function ($routes) {
'base_url' => '/images/',

// Response class for serving images. If unset (default) an instance of
// \ADmad\Glide\Responses\PsrResponseFactory() will be used.
// \ADmad\Glide\Response\PsrResponseFactory() will be used.
// http://glide.thephpleague.com/1.0/config/responses/
'response' => null,
],
Expand Down Expand Up @@ -121,7 +121,7 @@ images. You can load the helper in your `AppView::initialize()` as shown in
example below:

```php
public function initialize()
public function initialize(): void
{
// All option values should match the corresponding options for `GlideFilter`.
$this->loadHelper('ADmad/Glide.Glide', [
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"require": {
"cakephp/cakephp": "^4.0.2",
"league/glide": "^1.6"
"league/glide": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5 || ^9.3"
Expand Down
16 changes: 9 additions & 7 deletions src/Middleware/GlideMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

use ADmad\Glide\Exception\ResponseException;
use ADmad\Glide\Exception\SignatureException;
use ADmad\Glide\Responses\PsrResponseFactory;
use ADmad\Glide\Response\PsrResponseFactory;
use Cake\Core\Configure;
use Cake\Core\InstanceConfigTrait;
use Cake\Event\EventDispatcherInterface;
use Cake\Event\EventDispatcherTrait;
Expand Down Expand Up @@ -180,7 +181,7 @@ protected function _checkModified(ServerRequestInterface $request, Server $serve
try {
/** @var int|string|false $modifiedTime */
$modifiedTime = $server->getSource()
->getTimestamp($server->getSourcePath($this->_path));
->lastModified($server->getSourcePath($this->_path));
} catch (Exception $exception) {
return $this->_handleException($request, $exception);
}
Expand Down Expand Up @@ -254,13 +255,10 @@ protected function _passThrough(ServerRequestInterface $request, Server $server)
$path = $server->getSourcePath($this->_path);

$resource = $source->readStream($path);
if ($resource === false) {
throw new ResponseException();
}
$stream = new Stream($resource);

$contentType = $source->getMimetype($path);
$contentLength = $source->getSize($path);
$contentType = $source->mimeType($path);
$contentLength = $source->fileSize($path);

/** @psalm-suppress PossiblyFalseArgument */
return (new Response())->withBody($stream)
Expand Down Expand Up @@ -345,6 +343,10 @@ protected function _handleException(ServerRequestInterface $request, $exception)
return $result;
}

if (Configure::read('debug')) {
throw $exception;
}

throw new ResponseException(null, null, $exception);
}
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,33 @@
<?php
declare(strict_types=1);

namespace ADmad\Glide\Responses;
namespace ADmad\Glide\Response;

use ADmad\Glide\Exception\ResponseException;
use Cake\Http\Response;
use Laminas\Diactoros\Stream;
use League\Flysystem\FilesystemInterface;
use League\Glide\Filesystem\FilesystemException;
use League\Flysystem\FilesystemOperator;
use League\Glide\Responses\ResponseFactoryInterface;

class PsrResponseFactory implements ResponseFactoryInterface
{
/**
* Create response.
*
* @param \League\Flysystem\FilesystemInterface $cache Cache file system.
* @param \League\Flysystem\FilesystemOperator $cache Cache file system.
* @param string $path Cached file path.
* @return \Psr\Http\Message\ResponseInterface Response object.
*/
public function create(FilesystemInterface $cache, $path)
public function create(FilesystemOperator $cache, $path)
{
$resource = $cache->readStream($path);
if ($resource === false) {
throw new ResponseException();
}
$stream = new Stream($resource);

$contentType = $cache->getMimetype($path);
$contentLength = $cache->getSize($path);

if ($contentType === false) {
throw new FilesystemException('Unable to determine the image content type.');
}

if ($contentLength === false) {
throw new FilesystemException('Unable to determine the image content length.');
}
$contentType = $cache->mimeType($path);
$contentLength = $cache->fileSize($path);

return (new Response())->withBody($stream)
->withHeader('Content-Type', $contentType)
Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/Middleware/GlideMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
use ADmad\Glide\Exception\ResponseException;
use ADmad\Glide\Exception\SignatureException;
use ADmad\Glide\Middleware\GlideMiddleware;
use Cake\Core\Configure;
use Cake\Event\EventManager;
use Cake\Http\Response;
use Cake\Http\ServerRequestFactory;
use Cake\TestSuite\TestCase;
use Cake\Utility\Security;
use League\Flysystem\UnableToRetrieveMetadata;
use League\Glide\ServerFactory;
use League\Glide\Signatures\Signature;
use TestApp\Http\TestRequestHandler;
Expand Down Expand Up @@ -180,15 +182,28 @@ public function testSignatureException()
$middleware->process($request, $this->handler);
}

public function test3rdPartyException()
{
$middleware = new GlideMiddleware($this->config);
$request = ServerRequestFactory::fromGlobals([
'REQUEST_URI' => '/images/non-existent.jpg',
]);

$this->expectException(UnableToRetrieveMetadata::class);
$middleware->process($request, $this->handler);
}

public function testResponseException()
{
$middleware = new GlideMiddleware($this->config);
$request = ServerRequestFactory::fromGlobals([
'REQUEST_URI' => '/images/non-existent.jpg',
]);

Configure::write('debug', false);
$this->expectException(ResponseException::class);
$middleware->process($request, $this->handler);
Configure::write('debug', true);
}

public function testExceptionEventListener()
Expand Down

0 comments on commit ce35455

Please sign in to comment.