Skip to content

Commit b53e128

Browse files
committed
bug symfony#59228 [HttpFoundation] Avoid mime type guess with temp files in BinaryFileResponse (alexandre-daubois)
This PR was merged into the 7.1 branch. Discussion ---------- [HttpFoundation] Avoid mime type guess with temp files in `BinaryFileResponse` | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT When using `SplTempFileObject` with `BinaryFileResponse`, the MIME type guesser cannot be used on the `php://temp` stream. The mime type should be passed manually to the `Content-Type` header. Currently, if the content type header is not provided when passing a `SplTempFileObject` to `BinaryFileResponse`, the MIME type guesser throws an exception. Commits ------- 223dcd1 [HttpFoundation] Avoid mime type guess with temp files in `BinaryFileResponse`
2 parents 5a3d905 + 223dcd1 commit b53e128

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Symfony/Component/HttpFoundation/BinaryFileResponse.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,12 @@ public function prepare(Request $request): static
189189
}
190190

191191
if (!$this->headers->has('Content-Type')) {
192-
$this->headers->set('Content-Type', $this->file->getMimeType() ?: 'application/octet-stream');
192+
$mimeType = null;
193+
if (!$this->tempFileObject) {
194+
$mimeType = $this->file->getMimeType();
195+
}
196+
197+
$this->headers->set('Content-Type', $mimeType ?: 'application/octet-stream');
193198
}
194199

195200
parent::prepare($request);

src/Symfony/Component/HttpFoundation/Tests/BinaryFileResponseTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,4 +458,15 @@ public function testCreateFromTemporaryFile()
458458
$string = ob_get_clean();
459459
$this->assertSame('foo,bar', $string);
460460
}
461+
462+
public function testCreateFromTemporaryFileWithoutMimeType()
463+
{
464+
$file = new \SplTempFileObject();
465+
$file->fwrite('foo,bar');
466+
467+
$response = new BinaryFileResponse($file);
468+
$response->prepare(new Request());
469+
470+
$this->assertSame('application/octet-stream', $response->headers->get('Content-Type'));
471+
}
461472
}

0 commit comments

Comments
 (0)