Files
kupshop/bundles/KupShop/ContentBundle/Controller/ImagesController.php
2025-08-02 16:30:27 +02:00

52 lines
1.8 KiB
PHP

<?php
namespace KupShop\ContentBundle\Controller;
use KupShop\AdminBundle\Util\SystemImageUtils;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Service\Attribute\Required;
class ImagesController extends AbstractController
{
#[Required]
public SystemImageUtils $systemImageUtils;
#[Route(
path: '/system-images/{filename}.{suffix}',
requirements: [
'filename' => '[a-zA-Z0-9\-]*',
'suffix' => 'png|svg|jpg|webmanifest',
]
)]
public function getSystemImages(string $filename, string $suffix): BinaryFileResponse|RedirectResponse
{
try {
return $this->systemImageUtils->getBinaryFileResponse($filename, $suffix);
} catch (FileNotFoundException $e) {
if (isDevelopment() && ($url = $this->systemImageUtils->getImageSrcRemote($filename, $suffix))) {
return new RedirectResponse($url);
}
throw $this->createNotFoundException("File /system-images/{$filename}.{$suffix} not found.");
}
}
#[Route(path: '/favicon.ico')]
public function getFaviconIco(): BinaryFileResponse|RedirectResponse
{
try {
return $this->systemImageUtils->getBinaryFileResponse('favicon', 'ico');
} catch (FileNotFoundException $e) {
if (isDevelopment() && ($url = $this->systemImageUtils->getImageSrcRemote('favicon', 'ico'))) {
return new RedirectResponse($url);
}
throw $this->createNotFoundException('File favicon.ico not found.');
}
}
}