Skip to content

Commit

Permalink
Use the slugger to generate fallback filename (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Jul 29, 2024
1 parent 77710b2 commit 3d61ddc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
arguments:
- '@Terminal42\ShortlinkBundle\Repository\ShortlinkRepository'
- '@Terminal42\ShortlinkBundle\ShortlinkGenerator'
- '@contao.slug'
tags:
- { name: controller.service_arguments }

Expand Down
8 changes: 6 additions & 2 deletions src/Controller/QrCodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Contao\Backend;
use Contao\BackendTemplate;
use Contao\CoreBundle\Exception\ResponseException;
use Contao\CoreBundle\Slug\Slug;
use Contao\Input;
use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -25,6 +26,7 @@ class QrCodeController
public function __construct(
private readonly ShortlinkRepository $repository,
private readonly ShortlinkGenerator $generator,
private readonly Slug $slug,
) {
}

Expand Down Expand Up @@ -62,7 +64,6 @@ public function __invoke(): Response

private function download(Shortlink $shortlink, string $format): never
{
$filename = ($shortlink->getName() ?: 'qrcode').'.'.$format;
$backend = match ($format) {
'svg' => new SvgImageBackEnd(),
'png' => new ImagickImageBackEnd(),
Expand All @@ -72,10 +73,13 @@ private function download(Shortlink $shortlink, string $format): never
$renderer = new ImageRenderer(new RendererStyle(400, 0), $backend);
$writer = new Writer($renderer);

$filename = ($shortlink->getName() ?: 'qrcode');

$response = new Response($writer->writeString($this->generateUrl($shortlink)));
$response->headers->set('Content-Disposition', HeaderUtils::makeDisposition(
HeaderUtils::DISPOSITION_ATTACHMENT,
$filename,
$filename.'.'.$format,
$this->slug->generate($filename).'.'.$format,
));

throw new ResponseException($response);
Expand Down

0 comments on commit 3d61ddc

Please sign in to comment.