Use network path instead of absolute URL for attachment thumbnails.

This should fix issue #237
This commit is contained in:
Jan Böhmer 2023-03-31 23:30:37 +02:00
parent 8295ed716b
commit 52bdde40a1

View file

@ -24,9 +24,13 @@ namespace App\Services\Attachments;
use App\Entity\Attachments\Attachment; use App\Entity\Attachments\Attachment;
use InvalidArgumentException; use InvalidArgumentException;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Liip\ImagineBundle\Service\FilterService; use Liip\ImagineBundle\Service\FilterService;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use RuntimeException; use RuntimeException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\UrlHelper;
use Symfony\Component\Routing\Generator\UrlGenerator;
use function strlen; use function strlen;
use Symfony\Component\Asset\Packages; use Symfony\Component\Asset\Packages;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@ -38,19 +42,19 @@ class AttachmentURLGenerator
protected AttachmentPathResolver $pathResolver; protected AttachmentPathResolver $pathResolver;
protected UrlGeneratorInterface $urlGenerator; protected UrlGeneratorInterface $urlGenerator;
protected AttachmentManager $attachmentHelper; protected AttachmentManager $attachmentHelper;
protected FilterService $filterService; protected CacheManager $thumbnailManager;
protected LoggerInterface $logger; protected LoggerInterface $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, LoggerInterface $logger) CacheManager $thumbnailManager, 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->thumbnailManager = $thumbnailManager;
$this->logger = $logger; $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)
@ -145,7 +149,7 @@ class AttachmentURLGenerator
return $this->urlGenerator->generate('attachment_view', ['id' => $attachment->getID()]); return $this->urlGenerator->generate('attachment_view', ['id' => $attachment->getID()]);
} }
//For builtin ressources it is not useful to create a thumbnail //For builtin resources it is not useful to create a thumbnail
//because the footprints images are small and highly optimized already. //because the footprints images are small and highly optimized already.
if (('thumbnail_md' === $filter_name && $attachment->isBuiltIn()) if (('thumbnail_md' === $filter_name && $attachment->isBuiltIn())
//GD can not work with SVG, so serve it directly... //GD can not work with SVG, so serve it directly...
@ -154,8 +158,10 @@ class AttachmentURLGenerator
} }
try { try {
//Otherwise we can serve the relative path via Asset component //We try to get network path here (so no schema), but this param might just get ignored by the cache manager
return $this->filterService->getUrlOfFilteredImage($asset_path, $filter_name); $tmp = $this->thumbnailManager->getBrowserPath($asset_path, $filter_name, [], null, UrlGeneratorInterface::NETWORK_PATH);
//So we remove the schema manually
return preg_replace('/^https?:/', '', $tmp);
} catch (\Imagine\Exception\RuntimeException $e) { } 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 //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()); $this->logger->warning('Could not open thumbnail for attachment with ID ' . $attachment->getID() . ': ' . $e->getMessage());