From 5d5932d1f1573c9f3e1493d5b0f9ee758d1cf603 Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Fri, 25 Sep 2020 13:34:25 +0200 Subject: [PATCH] Fix BinaryFileResponse with range to psr response conversion Closes #84 --- Factory/PsrHttpFactory.php | 4 ++-- .../Factory/AbstractHttpMessageFactoryTest.php | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Factory/PsrHttpFactory.php b/Factory/PsrHttpFactory.php index 80ec527..fcb80d6 100644 --- a/Factory/PsrHttpFactory.php +++ b/Factory/PsrHttpFactory.php @@ -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); diff --git a/Tests/Factory/AbstractHttpMessageFactoryTest.php b/Tests/Factory/AbstractHttpMessageFactoryTest.php index 998edcc..d43f7fb 100644 --- a/Tests/Factory/AbstractHttpMessageFactoryTest.php +++ b/Tests/Factory/AbstractHttpMessageFactoryTest.php @@ -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);