Skip to content

Commit

Permalink
other files
Browse files Browse the repository at this point in the history
  • Loading branch information
benji07 committed Mar 17, 2022
1 parent f57c9a3 commit 1425959
Show file tree
Hide file tree
Showing 28 changed files with 60 additions and 1 deletion.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
26 changes: 26 additions & 0 deletions src/Bridge/Glide/Bundle/DecoratingApiServerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace App\Bridge\Glide\Bundle;

use League\Glide\ServerFactory;

class DecoratingApiServerFactory extends ServerFactory
{
public function getApi()
{
// @phpstan-ignore-next-line
return new SkippingMimeTypesApi(parent::getApi(), $this->config['skipped_types'] ?? []);
}

public static function create(array $config = [])
{
$server = parent::create($config);
$decoratedFactory = (new self($config));

$server->setApi($decoratedFactory->getApi());

return $server;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ services:
League\Glide\Responses\SymfonyResponseFactory: ~

League\Glide\Server:
factory: [League\Glide\ServerFactory, create]
factory: [App\Bridge\Glide\Bundle\DecoratingApiServerFactory, create]
arguments:
- ~ # $config set by the extension

Expand Down
33 changes: 33 additions & 0 deletions src/Bridge/Glide/Bundle/SkippingMimeTypesApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace App\Bridge\Glide\Bundle;

use League\Glide\Api\ApiInterface;
use Symfony\Component\Mime\MimeTypes;
use Symfony\Component\Mime\MimeTypesInterface;

/**
* An API implementation allowing to skip Glide resize on some specific MIME types
* (usually unsupported by Glide, or removing GIF animations for instance),
* but still allowing to pass through Glide's server and move the images to a properly public location.
*/
class SkippingMimeTypesApi implements ApiInterface
{
public function __construct(
private ApiInterface $decorated,
private array $skippedTypes = ['image/gif'],
private MimeTypesInterface $types = new MimeTypes()
) {
}

public function run($source, array $params): string
{
if (\in_array($this->types->guessMimeType($source), $this->skippedTypes, true)) {
return (string) file_get_contents($source);
}

return $this->decorated->run($source, $params);
}
}

0 comments on commit 1425959

Please sign in to comment.