Skip to content

Commit

Permalink
Merge pull request #338 from JefvdA/334-support-special-characters-in…
Browse files Browse the repository at this point in the history
…-filename

feat: add new optional fileNameFallBack argument
  • Loading branch information
alexpozzi committed Apr 19, 2024
2 parents fe25f99 + ccfce02 commit 24c61f0
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Snappy/Response/JpegResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

class JpegResponse extends SnappyResponse
{
public function __construct($content, $fileName = 'output.jpg', $contentType = 'image/jpg', $contentDisposition = 'inline', $status = 200, $headers = [])
public function __construct($content, $fileName = 'output.jpg', $contentType = 'image/jpg', $contentDisposition = 'inline', $status = 200, $headers = [], $fileNameFallBack = '')
{
parent::__construct($content, $fileName, $contentType, $contentDisposition, $status, $headers);
parent::__construct($content, $fileName, $contentType, $contentDisposition, $status, $headers, $fileNameFallBack);
}
}
4 changes: 2 additions & 2 deletions src/Snappy/Response/PdfResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

class PdfResponse extends SnappyResponse
{
public function __construct($content, $fileName = 'output.pdf', $contentType = 'application/pdf', $contentDisposition = 'attachment', $status = 200, $headers = [])
public function __construct($content, $fileName = 'output.pdf', $contentType = 'application/pdf', $contentDisposition = 'attachment', $status = 200, $headers = [], $fileNameFallBack = '')
{
parent::__construct($content, $fileName, $contentType, $contentDisposition, $status, $headers);
parent::__construct($content, $fileName, $contentType, $contentDisposition, $status, $headers, $fileNameFallBack);
}
}
4 changes: 2 additions & 2 deletions src/Snappy/Response/SnappyResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class SnappyResponse extends Base
{
public function __construct($content, $fileName, $contentType, $contentDisposition = 'attachment', $status = 200, $headers = [])
public function __construct($content, $fileName, $contentType, $contentDisposition = 'attachment', $status = 200, $headers = [], $fileNameFallBack = '')
{
$contentDispositionDirectives = [ResponseHeaderBag::DISPOSITION_INLINE, ResponseHeaderBag::DISPOSITION_ATTACHMENT];
if (!in_array($contentDisposition, $contentDispositionDirectives)) {
Expand All @@ -16,6 +16,6 @@ public function __construct($content, $fileName, $contentType, $contentDispositi

parent::__construct($content, $status, $headers);
$this->headers->add(['Content-Type' => $contentType]);
$this->headers->add(['Content-Disposition' => $this->headers->makeDisposition($contentDisposition, $fileName)]);
$this->headers->add(['Content-Disposition' => $this->headers->makeDisposition($contentDisposition, $fileName, $fileNameFallBack)]);
}
}
6 changes: 6 additions & 0 deletions tests/Snappy/Response/JpegResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ public function testSetDifferentFileName(): void

$this->assertSame($fileName, $matches[1]);
}

public function testSpecialCharacters(): void
{
$response = new JpegResponse('', 'Ä.jpg', 'image/jpg', fileNameFallBack: 'thefilenamefallback.jpg');
$this->assertSame('inline; filename=thefilenamefallback.jpg; filename*=utf-8\'\'%C3%84.jpg', str_replace('"', '', $response->headers->get('Content-Disposition')));
}
}
6 changes: 6 additions & 0 deletions tests/Snappy/Response/PdfResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ public function testSetDifferentFileName(): void

$this->assertSame($fileName, $matches[1]);
}

public function testSpecialCharacters(): void
{
$response = new PdfResponse('', 'Ä.jpg', 'image/jpg', fileNameFallBack: 'thefilenamefallback.jpg');
$this->assertSame('attachment; filename=thefilenamefallback.jpg; filename*=utf-8\'\'%C3%84.jpg', str_replace('"', '', $response->headers->get('Content-Disposition')));
}
}

0 comments on commit 24c61f0

Please sign in to comment.