Skip to content

Commit b5567e7

Browse files
[HttpFoundation] Avoid mime type guess with temp files in BinaryFileResponse
1 parent 8276584 commit b5567e7

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

BinaryFileResponse.php

+6-1
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);

Tests/BinaryFileResponseTest.php

+11
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)