From 40f90f68f52f34fb85c3d416ae30cf9969957523 Mon Sep 17 00:00:00 2001 From: Jordane VASPARD Date: Tue, 29 Oct 2024 22:18:37 +0100 Subject: [PATCH] Fix support for \SplTempFileObject in BinaryFileResponse --- BinaryFileResponse.php | 6 ++++-- Tests/BinaryFileResponseTest.php | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/BinaryFileResponse.php b/BinaryFileResponse.php index 5bc8b0e16..a2b160f8a 100644 --- a/BinaryFileResponse.php +++ b/BinaryFileResponse.php @@ -126,7 +126,7 @@ public function setChunkSize(int $chunkSize): static */ public function setAutoLastModified(): static { - $this->setLastModified(\DateTimeImmutable::createFromFormat('U', $this->file->getMTime())); + $this->setLastModified(\DateTimeImmutable::createFromFormat('U', $this->tempFileObject ? time() : $this->file->getMTime())); return $this; } @@ -197,7 +197,9 @@ public function prepare(Request $request): static $this->offset = 0; $this->maxlen = -1; - if (false === $fileSize = $this->file->getSize()) { + if ($this->tempFileObject) { + $fileSize = $this->tempFileObject->fstat()['size']; + } elseif (false === $fileSize = $this->file->getSize()) { return $this; } $this->headers->remove('Transfer-Encoding'); diff --git a/Tests/BinaryFileResponseTest.php b/Tests/BinaryFileResponseTest.php index b58276cf7..77bc32e8c 100644 --- a/Tests/BinaryFileResponseTest.php +++ b/Tests/BinaryFileResponseTest.php @@ -451,6 +451,9 @@ public function testCreateFromTemporaryFile() $this->assertEquals('attachment; filename=temp', $response->headers->get('Content-Disposition')); ob_start(); + $response->setAutoLastModified(); + $response->prepare(new Request()); + $this->assertSame('7', $response->headers->get('Content-Length')); $response->sendContent(); $string = ob_get_clean(); $this->assertSame('foo,bar', $string);