Terugluisteren werkt
This commit is contained in:
@@ -63,8 +63,8 @@ QUERY;
|
|||||||
|
|
||||||
private function getPodcastList(Request $request, $filter, $params)
|
private function getPodcastList(Request $request, $filter, $params)
|
||||||
{
|
{
|
||||||
$count = (int)$request->get('aantal', 15);
|
$count = (int)$request->get('aantal', 15);
|
||||||
$page = (int)$request->get('pagina', 1);
|
$page = (int)$request->get('pagina', 1);
|
||||||
if($count <= 0 || $page <= 0) {
|
if($count <= 0 || $page <= 0) {
|
||||||
return abort(400);
|
return abort(400);
|
||||||
}
|
}
|
||||||
@@ -147,28 +147,91 @@ QUERY;
|
|||||||
return response()->abort(404);
|
return response()->abort(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$size += filesize($file);
|
$size += ($filesize = filesize($file));
|
||||||
$files[] = $file;
|
$files[] = [$file, $filesize];
|
||||||
$currentHour->add(\DateInterval::createFromDateString('1 hour'));
|
$currentHour->add(\DateInterval::createFromDateString('1 hour'));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new StreamedResponse(function() use($files) {
|
$headers = [
|
||||||
// Open output stream
|
'Pragma' => 'public',
|
||||||
$output = fopen('php://output', 'w');
|
'Expires' => '-1',
|
||||||
foreach($files as $file) {
|
'Cache-Control' => 'public,must-revalidate,post-check=0,pre-check=0',
|
||||||
$fp = fopen($file, 'rb');
|
'Content-Type' => 'audio/mpeg',
|
||||||
while ($buf = fread($fp, 1024)) {
|
'Content-Length' => $size,
|
||||||
fwrite($output, $buf);
|
'Accept-Ranges' => 'bytes'
|
||||||
}
|
];
|
||||||
fclose($fp);
|
|
||||||
|
$range = [];
|
||||||
|
if(isset($_SERVER['HTTP_RANGE'])) {
|
||||||
|
list($unit, $rangestr) = explode('=', $_SERVER['HTTP_RANGE'], 2);
|
||||||
|
if($unit == "bytes") {
|
||||||
|
$range = explode('-', $rangestr);
|
||||||
|
$range[1] = (empty($range[1])) ? ($size - 1) : min(abs(intval($range[1])), ($size - 1));
|
||||||
|
$range[0] = (empty($range[0]) || $range[1] < abs(intval($range[0]))) ? 0 : max(abs(intval($range[0])), 0);
|
||||||
|
$range[2] = ($range[1] - $range[0] + 1);
|
||||||
|
|
||||||
|
//if($range[0] > 0 || $range[1] < ($size - 1)) {
|
||||||
|
$headers['Content-Range'] = 'bytes ' . $range[0] . '-' . $range[1] . '/' . $size;
|
||||||
|
$headers['Content-Length'] = $range[2];
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
fclose($output);
|
}
|
||||||
}, 200, [
|
|
||||||
'Content-Type' => 'audio/mpg',
|
if($range === null) {
|
||||||
'Content-Disposition' => sprintf(
|
// $log = fopen('/tmp/rq.log', 'a');
|
||||||
'attachment; filename="Radio_6FM_Uitzending_%02d-%02d-%04d_%02du00.MP3"',
|
// fwrite($log, "Aborting (416)!\n\n");
|
||||||
$day, $month, $year, $hour)
|
// fclose($log);
|
||||||
]);
|
response()->abort(416); // Requested Range Not Satisfiable
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new StreamedResponse(function() use($files, $range) {
|
||||||
|
// $log = fopen('/tmp/rq.log', 'a');
|
||||||
|
// if(!count($range)) {
|
||||||
|
// fwrite($log, "Writing everything.\n");
|
||||||
|
// } else {
|
||||||
|
// fwrite($log, "Writing {$range[2]} bytes ({$range[0]} - {$range[1]})\n");
|
||||||
|
//}
|
||||||
|
$output = fopen('php://output', 'w');
|
||||||
|
foreach($files as $file) {
|
||||||
|
if(count($range) && $range[0] >= $file[1]) {
|
||||||
|
// We have a range and can skip the whole file
|
||||||
|
// fwrite($log, "Skipping all of {$file[0]}.\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// else fwrite($log, "File {$file[0]} is {$file[1]} bytes (starting from {$range[0]}).\n");
|
||||||
|
|
||||||
|
$fp = fopen($file[0], 'rb');
|
||||||
|
if(count($range) && $range[0]) {
|
||||||
|
fseek($fp, $range[0]);
|
||||||
|
$range[0] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
$maxToRead = count($range) ? min(1024, $range[2]) : 1024;
|
||||||
|
while ($buf = fread($fp, $maxToRead)) {
|
||||||
|
fwrite($output, $buf);
|
||||||
|
if(count($range)) {
|
||||||
|
$maxToRead = min(1024, $range[2]);
|
||||||
|
$bufSize = mb_strlen($buf, '8bit');
|
||||||
|
// $bytes += $bufSize;
|
||||||
|
if(($range[2] -= $bufSize) == 0) {
|
||||||
|
// fwrite($log, "Stopping reading from {$file[0]} because end of range reached.\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fwrite($log, "Wrote $bytes bytes.\n");
|
||||||
|
fclose($fp);
|
||||||
|
|
||||||
|
if(count($range) && $range[2] == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose($output);
|
||||||
|
// fclose($log);
|
||||||
|
}, count($range) ? 206 : 200, $headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Provide a streaming file with support for scrubbing
|
// Provide a streaming file with support for scrubbing
|
||||||
|
|||||||
Reference in New Issue
Block a user