Skip to content

Commit 5d5932d

Browse files
committed
Fix BinaryFileResponse with range to psr response conversion
Closes #84
1 parent e44f249 commit 5d5932d

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

Factory/PsrHttpFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ public function createResponse(Response $symfonyResponse)
127127
{
128128
$response = $this->responseFactory->createResponse($symfonyResponse->getStatusCode(), Response::$statusTexts[$symfonyResponse->getStatusCode()] ?? '');
129129

130-
if ($symfonyResponse instanceof BinaryFileResponse) {
130+
if ($symfonyResponse instanceof BinaryFileResponse && !$symfonyResponse->headers->has('Content-Range')) {
131131
$stream = $this->streamFactory->createStreamFromFile(
132132
$symfonyResponse->getFile()->getPathname()
133133
);
134134
} else {
135135
$stream = $this->streamFactory->createStreamFromFile('php://temp', 'wb+');
136-
if ($symfonyResponse instanceof StreamedResponse) {
136+
if ($symfonyResponse instanceof StreamedResponse || $symfonyResponse instanceof BinaryFileResponse) {
137137
ob_start(function ($buffer) use ($stream) {
138138
$stream->write($buffer);
139139

Tests/Factory/AbstractHttpMessageFactoryTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,23 @@ public function testCreateResponseFromBinaryFile()
182182
$this->assertEquals('Binary', $psrResponse->getBody()->__toString());
183183
}
184184

185+
public function testCreateResponseFromBinaryFileWithRange()
186+
{
187+
$path = tempnam($this->tmpDir, uniqid());
188+
file_put_contents($path, 'Binary');
189+
190+
$request = new Request();
191+
$request->headers->set('Range', 'bytes=1-4');
192+
193+
$response = new BinaryFileResponse($path, 200, ['Content-Type' => 'plain/text']);
194+
$response->prepare($request);
195+
196+
$psrResponse = $this->factory->createResponse($response);
197+
198+
$this->assertEquals('inar', $psrResponse->getBody()->__toString());
199+
$this->assertSame('bytes 1-4/6', $psrResponse->getHeaderLine('Content-Range'));
200+
}
201+
185202
public function testUploadErrNoFile()
186203
{
187204
$file = new UploadedFile('', '', null, UPLOAD_ERR_NO_FILE, true);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy