From ccfce02a3e4d73525c8667bfad54e48a791ce844 Mon Sep 17 00:00:00 2001 From: JefvdA Date: Tue, 19 Mar 2024 16:09:13 +0100 Subject: [PATCH] feat: add new optional fileNameFallBack argument refs: #334 --- src/Snappy/Response/JpegResponse.php | 4 ++-- src/Snappy/Response/PdfResponse.php | 4 ++-- src/Snappy/Response/SnappyResponse.php | 4 ++-- tests/Snappy/Response/JpegResponseTest.php | 6 ++++++ tests/Snappy/Response/PdfResponseTest.php | 6 ++++++ 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/Snappy/Response/JpegResponse.php b/src/Snappy/Response/JpegResponse.php index 87bea61..2d3d5b9 100644 --- a/src/Snappy/Response/JpegResponse.php +++ b/src/Snappy/Response/JpegResponse.php @@ -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); } } diff --git a/src/Snappy/Response/PdfResponse.php b/src/Snappy/Response/PdfResponse.php index 7889d2d..b225d70 100644 --- a/src/Snappy/Response/PdfResponse.php +++ b/src/Snappy/Response/PdfResponse.php @@ -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); } } diff --git a/src/Snappy/Response/SnappyResponse.php b/src/Snappy/Response/SnappyResponse.php index 1b3f106..07e9863 100644 --- a/src/Snappy/Response/SnappyResponse.php +++ b/src/Snappy/Response/SnappyResponse.php @@ -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)) { @@ -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)]); } } diff --git a/tests/Snappy/Response/JpegResponseTest.php b/tests/Snappy/Response/JpegResponseTest.php index 9a0cb82..53bd18b 100644 --- a/tests/Snappy/Response/JpegResponseTest.php +++ b/tests/Snappy/Response/JpegResponseTest.php @@ -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'))); + } } diff --git a/tests/Snappy/Response/PdfResponseTest.php b/tests/Snappy/Response/PdfResponseTest.php index 1fc93b5..1a98813 100644 --- a/tests/Snappy/Response/PdfResponseTest.php +++ b/tests/Snappy/Response/PdfResponseTest.php @@ -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'))); + } }