Skip to content

Commit

Permalink
Fix support for \SplTempFileObject in BinaryFileResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
elementaire committed Nov 4, 2024
1 parent 3d7bbf0 commit 40f90f6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 4 additions & 2 deletions BinaryFileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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');
Expand Down
3 changes: 3 additions & 0 deletions Tests/BinaryFileResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 40f90f6

Please sign in to comment.