151 lines
4.7 KiB
PHP
151 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', true);
|
|
|
|
class PodcastController extends Controller
|
|
{
|
|
// TODO: Include program
|
|
private static $BASE_SQL = <<<QUERY
|
|
SELECT `podcast`.`id`, `podcast`.`soundfilename`,
|
|
`podcast_meta`.`creationdt` AS `created`, `podcast_meta`.`title`, `podcast_meta`.`content`, `podcast_meta`.`program`,
|
|
`programs`.`longname` AS `program_name`, `programs`.`description` AS `program_description`
|
|
FROM `podcast`
|
|
INNER JOIN `podcast_meta` ON `podcast`.`id` = `podcast_meta`.`podcast`
|
|
LEFT JOIN `programs` ON `podcast_meta`.`program` = `programs`.`id`
|
|
WHERE `podcast_meta`.`active` = 1 AND `podcast_meta`.`title` <> '' AND `podcast_meta`.`content` <> ''
|
|
QUERY;
|
|
|
|
/**
|
|
* Create a new controller instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Lijst van alle podcasts
|
|
*/
|
|
public function overview(Request $request) {
|
|
return $this->getPodcastList($request, null, []);
|
|
}
|
|
|
|
/**
|
|
* Lijst van alle podcasts voor een specifiek programma
|
|
*/
|
|
public function program(Request $request, $program) {
|
|
if((int)$program <= 0) {
|
|
return abort(404);
|
|
}
|
|
|
|
return $this->getPodcastList($request, '`podcast_meta`.`program` = :program', ['program' => (int)$program]);
|
|
}
|
|
|
|
private function getPodcastList(Request $request, $filter, $params)
|
|
{
|
|
$count = (int)$request->get('aantal', 15);
|
|
$page = (int)$request->get('pagina', 1);
|
|
if($count <= 0 || $page <= 0) {
|
|
return abort(400);
|
|
}
|
|
|
|
$start = ($page - 1) * $count;
|
|
$podcasts = app('db')->select(self::$BASE_SQL
|
|
. ($filter ? ' AND (' . $filter . ')' : '')
|
|
. ' ORDER BY `podcast_meta`.`creationdt` DESC, `podcast_meta`.`id` DESC'
|
|
. ' LIMIT ' . (int)$start . ', ' . (int)$count,
|
|
$params);
|
|
|
|
$result = array();
|
|
foreach($podcasts as $podcast) {
|
|
$result[] = new \Model\Podcast($podcast);
|
|
}
|
|
|
|
return response()->json(['page' => $page, 'count' => $count, 'podcasts' => $result]);
|
|
}
|
|
|
|
/**
|
|
* Download specifieke podcast
|
|
*/
|
|
public function download(Request $request, $id) {
|
|
$queryResult = app('db')->select(self::$BASE_SQL
|
|
. ' AND `podcast`.`id` = :id '
|
|
. ' LIMIT 0, 1',
|
|
['id' => (int)$id]);
|
|
|
|
if(count($queryResult) == 0) { return abort(404); }
|
|
|
|
$podcast = new \Model\Podcast($queryResult[0]);
|
|
if(!$podcast->isValidAuth($request->get('auth'))) {
|
|
return abort(404);
|
|
}
|
|
|
|
return response()->download($podcast->getSoundfile(),
|
|
'6FM Gemist - ' . $podcast->title . '.mp3');
|
|
}
|
|
|
|
public function stream(Request $request, $id) {
|
|
$queryResult = app('db')->select(self::$BASE_SQL
|
|
. ' AND `podcast`.`id` = :id '
|
|
. ' LIMIT 0, 1',
|
|
['id' => $id]);
|
|
|
|
if(count($queryResult) == 0) { return abort(404); }
|
|
|
|
$podcast = new \Model\Podcast($queryResult[0]);
|
|
if(!$podcast->isValidAuth($request->get('auth'))) {
|
|
return abort(404);
|
|
}
|
|
|
|
$filename = $podcast->getSoundfile();
|
|
$file = fopen($filename, "rb");
|
|
$size = filesize($filename);
|
|
$content = fread($file, $size);
|
|
fclose($file);
|
|
|
|
self::streamFile($request, 'audio/mpeg', $content, $size);
|
|
}
|
|
|
|
// Provide a streaming file with support for scrubbing
|
|
// Source: https://gist.github.com/widnyana/cd2bdda07dc02e9fce71
|
|
private static function streamFile(Request $r, $contentType, $stream, $fullsize ) {
|
|
$size = $fullsize;
|
|
$response_code = 200;
|
|
$headers = array("Content-type" => $contentType);
|
|
|
|
// Check for request for part of the stream
|
|
$range = $r->header('Range');
|
|
if($range != null) {
|
|
$eqPos = strpos($range, "=");
|
|
$toPos = strpos($range, "-");
|
|
$unit = substr($range, 0, $eqPos);
|
|
$start = intval(substr($range, $eqPos+1, $toPos));
|
|
$success = fseek($stream, $start);
|
|
if($success == 0) {
|
|
$size = $fullsize - $start;
|
|
$response_code = 206;
|
|
$headers["Accept-Ranges"] = $unit;
|
|
$headers["Content-Range"] = $unit . " " . $start . "-" . ($fullsize-1) . "/" . $fullsize;
|
|
}
|
|
}
|
|
|
|
$headers["Content-Length"] = $size;
|
|
$response = new StreamedResponse(
|
|
function () use ($stream) {
|
|
$out = fopen('php://output', 'w');
|
|
fputs($out, $stream);
|
|
fclose($out);
|
|
}, 200, $headers);
|
|
$response->send();
|
|
}
|
|
|
|
}
|