Skip to content

Commit 46ba0d9

Browse files
committed
Merge branch '6.0' into 6.1
* 6.0: Update ExceptionInterface.php Revert removal of Stringable check Flush with flush() after ob_end_flush() [Validator] : Fix "PHP Warning: Undefined array key 1" in NotCompromisedPasswordValidator [Validator] Fix traverse option on Valid constraint when used as Attribute [HttpFoundation] Fix deleteFileAfterSend on client abortion Prevent that bad Ignore method annotations lead to incorrect results also fix the test fix deprecation fix sending request to paths containing multiple slashes Fix generated validation error message for wrong exception mapping status code
2 parents 1d17f20 + 24f5a50 commit 46ba0d9

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

BinaryFileResponse.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class BinaryFileResponse extends Response
3434
protected $offset = 0;
3535
protected $maxlen = -1;
3636
protected $deleteFileAfterSend = false;
37+
protected $chunkSize = 8 * 1024;
3738

3839
/**
3940
* @param \SplFileInfo|string $file The file to stream
@@ -101,6 +102,22 @@ public function getFile(): File
101102
return $this->file;
102103
}
103104

105+
/**
106+
* Sets the response stream chunk size.
107+
*
108+
* @return $this
109+
*/
110+
public function setChunkSize(int $chunkSize): self
111+
{
112+
if ($chunkSize < 1 || $chunkSize > \PHP_INT_MAX) {
113+
throw new \LogicException('The chunk size of a BinaryFileResponse cannot be less than 1 or greater than PHP_INT_MAX.');
114+
}
115+
116+
$this->chunkSize = $chunkSize;
117+
118+
return $this;
119+
}
120+
104121
/**
105122
* Automatically sets the Last-Modified header according the file modification date.
106123
*
@@ -282,7 +299,23 @@ public function sendContent(): static
282299
$out = fopen('php://output', 'w');
283300
$file = fopen($this->file->getPathname(), 'r');
284301

285-
stream_copy_to_stream($file, $out, $this->maxlen, $this->offset);
302+
ignore_user_abort(true);
303+
304+
if (0 !== $this->offset) {
305+
fseek($file, $this->offset);
306+
}
307+
308+
$length = $this->maxlen;
309+
while ($length && !feof($file)) {
310+
$read = ($length > $this->chunkSize) ? $this->chunkSize : $length;
311+
$length -= $read;
312+
313+
stream_copy_to_stream($file, $out, $read);
314+
315+
if (connection_aborted()) {
316+
break;
317+
}
318+
}
286319

287320
fclose($out);
288321
fclose($file);

Response.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,7 @@ public static function closeOutputBuffers(int $targetLevel, bool $flush): void
12631263
while ($level-- > $targetLevel && ($s = $status[$level]) && (!isset($s['del']) ? !isset($s['flags']) || ($s['flags'] & $flags) === $flags : $s['del'])) {
12641264
if ($flush) {
12651265
ob_end_flush();
1266+
flush();
12661267
} else {
12671268
ob_end_clean();
12681269
}

0 commit comments

Comments
 (0)