Kerkdienst terugluisteren

This commit is contained in:
2017-09-01 22:29:32 +02:00
parent 83f72ae901
commit 81bf7d2b07
4 changed files with 107 additions and 20 deletions

View File

@@ -0,0 +1,101 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;
class KerkdienstController extends Controller
{
private static $BASE_SQL = <<<QUERY
SELECT `zondagdienst`.`datum` AS `date`, `zondagdienst`.`tijd` AS `time`, `contacten`.`naam` AS `name`
FROM `zondagdienst` LEFT JOIN `contacten` ON `zondagdienst`.`kerk` = `contacten`.`contacten_id`
WHERE `zondagdienst`.`datum` > DATE_SUB("2017-09-02", INTERVAL 7 DAY)
ORDER BY `zondagdienst`.`datum` ASC
LIMIT 2
QUERY;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Gegevens over laatste en huidige dienst
*/
public function get() {
$isAvailable = ((date('N') % 7) != 0) || (time() > strtotime('today noon'));
$lastNext = app('db')->select(self::$BASE_SQL);
return response()->json([
'available' => $isAvailable,
'lastNext' => $lastNext]);
}
/**
* Podcast van de laatste kerkdienst
*/
public function podcast() {
$file = '/var/audio/podcast/6fm-kerkdienst.mp3';
$size = filesize($file);
$headers = [
'Pragma' => 'public',
'Expires' => '-1',
'Cache-Control' => 'public,must-revalidate,post-check=0,pre-check=0',
'Content-Type' => 'audio/mpeg',
'Content-Length' => $size,
'Accept-Ranges' => 'bytes'
];
$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);
$headers['Content-Range'] = 'bytes ' . $range[0] . '-' . $range[1] . '/' . $size;
$headers['Content-Length'] = $range[2];
}
}
if($range === null) {
response()->abort(416); // Requested Range Not Satisfiable
return;
}
return new StreamedResponse(function() use($file, $range) {
$output = fopen('php://output', 'w');
$fp = fopen($file, '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');
if(($range[2] -= $bufSize) == 0) {
break;
}
}
}
fclose($fp);
fclose($output);
}, count($range) ? 206 : 200, $headers);
}
}

View File

@@ -185,36 +185,23 @@ QUERY;
$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];
//}
$headers['Content-Range'] = 'bytes ' . $range[0] . '-' . $range[1] . '/' . $size;
$headers['Content-Length'] = $range[2];
}
}
if($range === null) {
// $log = fopen('/tmp/rq.log', 'a');
// fwrite($log, "Aborting (416)!\n\n");
// 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]) {
@@ -228,15 +215,12 @@ QUERY;
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) {
@@ -245,7 +229,6 @@ QUERY;
}
fclose($output);
// fclose($log);
}, count($range) ? 206 : 200, $headers);
}

View File

@@ -36,5 +36,8 @@ $app->get('programma/details/{id:\d+}', 'ProgramController@details' );
$app->get('programma/schema/test/{from}/{to}', 'ProgramController@testSchedule' );
$app->get('programma/download/{year:20\d\d}/{month:\d\d?}/{day:\d\d?}/{hour:\d\d?}/{duration:\d\d?}', 'PodcastController@complete');
$app->get('kerkdienst', 'KerkdienstController@get');
$app->get('kerkdienst/download', 'KerkdienstController@podcast');
// live/onair
// live/stream

View File

@@ -27,7 +27,7 @@ class Podcast extends Model {
$this->auth = $this->key;
}
if($data->program) {
if(isset($data->program) && $data->program) {
if(is_object($data->program)) {
$this->program = new \Model\Program($data->program);
} else {