Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpFoundation] Add support for \SplTempFileObject in BinaryFileResponse #49144

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
merged 1 commit into from
Feb 5, 2024
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
27 changes: 18 additions & 9 deletions src/Symfony/Component/HttpFoundation/BinaryFileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class BinaryFileResponse extends Response
protected static bool $trustXSendfileTypeHeader = false;

protected File $file;
protected ?\SplTempFileObject $tempFileObject = null;
protected int $offset = 0;
protected int $maxlen = -1;
protected bool $deleteFileAfterSend = false;
Expand Down Expand Up @@ -62,15 +63,18 @@ public function __construct(\SplFileInfo|string $file, int $status = 200, array
*/
public function setFile(\SplFileInfo|string $file, ?string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true): static
{
$isTemporaryFile = $file instanceof \SplTempFileObject;
$this->tempFileObject = $isTemporaryFile ? $file : null;

if (!$file instanceof File) {
if ($file instanceof \SplFileInfo) {
$file = new File($file->getPathname());
$file = new File($file->getPathname(), !$isTemporaryFile);
} else {
$file = new File((string) $file);
}
}

if (!$file->isReadable()) {
if (!$file->isReadable() && !$isTemporaryFile) {
throw new FileException('File must be readable.');
}

Expand All @@ -80,7 +84,7 @@ public function setFile(\SplFileInfo|string $file, ?string $contentDisposition =
$this->setAutoEtag();
}

if ($autoLastModified) {
if ($autoLastModified && !$isTemporaryFile) {
$this->setAutoLastModified();
}

Expand Down Expand Up @@ -298,19 +302,25 @@ public function sendContent(): static
}

$out = fopen('php://output', 'w');
$file = fopen($this->file->getPathname(), 'r');

if ($this->tempFileObject) {
$file = $this->tempFileObject;
$file->rewind();
} else {
$file = new \SplFileObject($this->file->getPathname(), 'r');
}

ignore_user_abort(true);

if (0 !== $this->offset) {
fseek($file, $this->offset);
$file->fseek($this->offset);
}

$length = $this->maxlen;
while ($length && !feof($file)) {
while ($length && !$file->eof()) {
$read = $length > $this->chunkSize || 0 > $length ? $this->chunkSize : $length;

if (false === $data = fread($file, $read)) {
if (false === $data = $file->fread($read)) {
break;
}
while ('' !== $data) {
Expand All @@ -326,9 +336,8 @@ public function sendContent(): static
}

fclose($out);
fclose($file);
} finally {
if ($this->deleteFileAfterSend && is_file($this->file->getPathname())) {
if (null === $this->tempFileObject && $this->deleteFileAfterSend && is_file($this->file->getPathname())) {
unlink($this->file->getPathname());
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Add `UploadedFile::getClientOriginalPath()`
* Add `QueryParameterRequestMatcher`
* Add `HeaderRequestMatcher`
* Add support for `\SplTempFileObject` in `BinaryFileResponse`

7.0
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,25 @@ public static function tearDownAfterClass(): void
@unlink($path);
}
}

public function testCreateFromTemporaryFile()
{
$file = new \SplTempFileObject();
$file->fwrite('foo,bar');

$response = new BinaryFileResponse($file, 201, [
'Content-Type' => 'text/csv',
]);

$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);

$this->assertSame(201, $response->getStatusCode());
$this->assertSame('text/csv', $response->headers->get('Content-Type'));
$this->assertEquals('attachment; filename=temp', $response->headers->get('Content-Disposition'));

ob_start();
$response->sendContent();
$string = ob_get_clean();
$this->assertSame('foo,bar', $string);
}
}