Skip to content

Commit

Permalink
[HttpFoundation] Avoid mime type guess with temp files in `BinaryFile…
Browse files Browse the repository at this point in the history
…Response`
  • Loading branch information
alexandre-daubois committed Dec 16, 2024
1 parent 8276584 commit b5567e7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion BinaryFileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,12 @@ public function prepare(Request $request): static
}

if (!$this->headers->has('Content-Type')) {
$this->headers->set('Content-Type', $this->file->getMimeType() ?: 'application/octet-stream');
$mimeType = null;
if (!$this->tempFileObject) {
$mimeType = $this->file->getMimeType();
}

$this->headers->set('Content-Type', $mimeType ?: 'application/octet-stream');
}

parent::prepare($request);
Expand Down
11 changes: 11 additions & 0 deletions Tests/BinaryFileResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,15 @@ public function testCreateFromTemporaryFile()
$string = ob_get_clean();
$this->assertSame('foo,bar', $string);
}

public function testCreateFromTemporaryFileWithoutMimeType()
{
$file = new \SplTempFileObject();
$file->fwrite('foo,bar');

$response = new BinaryFileResponse($file);
$response->prepare(new Request());

$this->assertSame('application/octet-stream', $response->headers->get('Content-Type'));
}
}

0 comments on commit b5567e7

Please sign in to comment.