mirror of
https://github.com/koel/koel
synced 2024-11-24 21:23:06 +00:00
Refactor php streamer
This commit is contained in:
parent
90ed58cb91
commit
2349a66a31
1 changed files with 32 additions and 40 deletions
|
@ -10,29 +10,12 @@ class PHPStreamer extends Streamer implements DirectStreamerInterface
|
||||||
*/
|
*/
|
||||||
public function stream(): void
|
public function stream(): void
|
||||||
{
|
{
|
||||||
// Get the 'Range' header if one was sent
|
$range = $this->getRange();
|
||||||
if (array_key_exists('HTTP_RANGE', $_SERVER)) {
|
$start = null;
|
||||||
// IIS/Some Apache versions
|
$end = null;
|
||||||
$range = $_SERVER['HTTP_RANGE'];
|
|
||||||
} elseif (function_exists('apache_request_headers') && $apache = apache_request_headers()) {
|
|
||||||
// Try Apache again
|
|
||||||
$headers = [];
|
|
||||||
|
|
||||||
foreach ($apache as $header => $val) {
|
|
||||||
$headers[strtolower($header)] = $val;
|
|
||||||
}
|
|
||||||
|
|
||||||
$range = array_key_exists('range', $headers) ? $headers['range'] : false;
|
|
||||||
} else {
|
|
||||||
// We can't get the header/there isn't one set
|
|
||||||
$range = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the data range requested (if any)
|
|
||||||
$fileSize = filesize($this->song->path);
|
$fileSize = filesize($this->song->path);
|
||||||
|
|
||||||
if ($range) {
|
if ($range) {
|
||||||
$partial = true;
|
|
||||||
list($param, $range) = explode('=', $range);
|
list($param, $range) = explode('=', $range);
|
||||||
|
|
||||||
// Bad request - range unit is not 'bytes'
|
// Bad request - range unit is not 'bytes'
|
||||||
|
@ -44,34 +27,25 @@ class PHPStreamer extends Streamer implements DirectStreamerInterface
|
||||||
// Bad request - 'bytes' parameter is not valid
|
// Bad request - 'bytes' parameter is not valid
|
||||||
abort_unless(count($range) === 2, 400);
|
abort_unless(count($range) === 2, 400);
|
||||||
|
|
||||||
if ($range[0] === '') {
|
|
||||||
// First number missing, return last $range[1] bytes
|
|
||||||
$start = 0;
|
|
||||||
$end = (int) $range[1];
|
|
||||||
} elseif ($range[1] === '') {
|
|
||||||
if ($range[0] === 0) {
|
|
||||||
$start = 0;
|
|
||||||
$end = $fileSize - 1;
|
|
||||||
$partial = false;
|
|
||||||
} else {
|
|
||||||
// Second number missing, return from byte $range[0] to end
|
|
||||||
$start = (int) $range[0];
|
$start = (int) $range[0];
|
||||||
|
|
||||||
|
if (!$range[0]) {
|
||||||
|
// First number missing, return last $range[1] bytes
|
||||||
|
$end = (int) $range[1];
|
||||||
|
} elseif (!$range[1]) {
|
||||||
$end = $fileSize - 1;
|
$end = $fileSize - 1;
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Both numbers present, return specific range
|
// Both numbers present, return specific range
|
||||||
$start = (int) $range[0];
|
|
||||||
$end = (int) $range[1];
|
$end = (int) $range[1];
|
||||||
|
|
||||||
if ($end >= $fileSize || (!$start && (!$end || $end === ($fileSize - 1)))) {
|
if ($end >= $fileSize) {
|
||||||
// Invalid range/whole file specified, return whole file
|
$end = $fileSize - 1;
|
||||||
$partial = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$partial = $start > 0 || $end < $fileSize - 1;
|
||||||
$length = $end - $start + 1;
|
$length = $end - $start + 1;
|
||||||
} else {
|
} else {
|
||||||
// No range requested
|
|
||||||
$length = filesize($this->song->path);
|
$length = filesize($this->song->path);
|
||||||
$partial = false;
|
$partial = false;
|
||||||
}
|
}
|
||||||
|
@ -104,10 +78,28 @@ class PHPStreamer extends Streamer implements DirectStreamerInterface
|
||||||
|
|
||||||
fclose($fp);
|
fclose($fp);
|
||||||
} else {
|
} else {
|
||||||
// ...otherwise just send the whole file
|
|
||||||
readfile($this->song->path);
|
readfile($this->song->path);
|
||||||
}
|
}
|
||||||
|
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getRange():? string
|
||||||
|
{
|
||||||
|
if (getenv('HTTP_RANGE')) {
|
||||||
|
// IIS/Some Apache versions
|
||||||
|
return (string) getenv('HTTP_RANGE');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (function_exists('apache_request_headers') && $apache = apache_request_headers()) {
|
||||||
|
// Try Apache again
|
||||||
|
foreach ($apache as $header => $val) {
|
||||||
|
if (strtolower($header) === 'range') {
|
||||||
|
return (string) $val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue