Fail gracefully when Imagine can not produce an thumbnail.

We now just fall back to the original image instead of throwing an exception. (partly) Fixes issue #89. Maybe related to issue #136
This commit is contained in:
Jan Böhmer 2022-07-21 00:31:34 +02:00
parent d57377a143
commit 2ee42b7621
2 changed files with 16 additions and 3 deletions

3
docs/docker/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
db/
public_media/
uploads/

View file

@ -45,6 +45,7 @@ namespace App\Services\Attachments;
use App\Entity\Attachments\Attachment; use App\Entity\Attachments\Attachment;
use InvalidArgumentException; use InvalidArgumentException;
use Liip\ImagineBundle\Service\FilterService; use Liip\ImagineBundle\Service\FilterService;
use Psr\Log\LoggerInterface;
use RuntimeException; use RuntimeException;
use function strlen; use function strlen;
use Symfony\Component\Asset\Packages; use Symfony\Component\Asset\Packages;
@ -59,15 +60,18 @@ class AttachmentURLGenerator
protected $attachmentHelper; protected $attachmentHelper;
protected $filterService; protected $filterService;
protected $logger;
public function __construct(Packages $assets, AttachmentPathResolver $pathResolver, public function __construct(Packages $assets, AttachmentPathResolver $pathResolver,
UrlGeneratorInterface $urlGenerator, AttachmentManager $attachmentHelper, UrlGeneratorInterface $urlGenerator, AttachmentManager $attachmentHelper,
FilterService $filterService) FilterService $filterService, LoggerInterface $logger)
{ {
$this->assets = $assets; $this->assets = $assets;
$this->pathResolver = $pathResolver; $this->pathResolver = $pathResolver;
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->attachmentHelper = $attachmentHelper; $this->attachmentHelper = $attachmentHelper;
$this->filterService = $filterService; $this->filterService = $filterService;
$this->logger = $logger;
//Determine a normalized path to the public folder (assets are relative to this folder) //Determine a normalized path to the public folder (assets are relative to this folder)
$this->public_path = $this->pathResolver->parameterToAbsolutePath('public'); $this->public_path = $this->pathResolver->parameterToAbsolutePath('public');
@ -168,8 +172,14 @@ class AttachmentURLGenerator
return $this->assets->getUrl($asset_path); return $this->assets->getUrl($asset_path);
} }
//Otherwise we can serve the relative path via Asset component try {
return $this->filterService->getUrlOfFilteredImage($asset_path, $filter_name); //Otherwise we can serve the relative path via Asset component
return $this->filterService->getUrlOfFilteredImage($asset_path, $filter_name);
} catch (\Imagine\Exception\RuntimeException $e) {
//If the filter fails, we can not serve the thumbnail and fall back to the original image and log an warning
$this->logger->warning('Could not open thumbnail for attachment with ID ' . $attachment->getID() . ': ' . $e->getMessage());
return $this->assets->getUrl($asset_path);
}
} }
/** /**