Skip to content

Fix BinaryFileResponse with range to psr response conversion #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Factory/PsrHttpFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ public function createResponse(Response $symfonyResponse)
{
$response = $this->responseFactory->createResponse($symfonyResponse->getStatusCode(), Response::$statusTexts[$symfonyResponse->getStatusCode()] ?? '');

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

Expand Down
17 changes: 17 additions & 0 deletions Tests/Factory/AbstractHttpMessageFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,23 @@ public function testCreateResponseFromBinaryFile()
$this->assertEquals('Binary', $psrResponse->getBody()->__toString());
}

public function testCreateResponseFromBinaryFileWithRange()
{
$path = tempnam($this->tmpDir, uniqid());
file_put_contents($path, 'Binary');

$request = new Request();
$request->headers->set('Range', 'bytes=1-4');

$response = new BinaryFileResponse($path, 200, ['Content-Type' => 'plain/text']);
$response->prepare($request);

$psrResponse = $this->factory->createResponse($response);

$this->assertEquals('inar', $psrResponse->getBody()->__toString());
$this->assertSame('bytes 1-4/6', $psrResponse->getHeaderLine('Content-Range'));
}

public function testUploadErrNoFile()
{
$file = new UploadedFile('', '', null, UPLOAD_ERR_NO_FILE, true);
Expand Down