package PiTube::Controller::Stream; use Mojo::Base 'Mojolicious::Controller'; use constant CONTENT_TYPE => { m3u8 => 'application/vnd.apple.mpegurl', ts => 'video/mp2t', }; sub list { my $c = shift; my $cond = { is_active => 't' }; my $streams = $c->schema->resultset('Stream_view')->search( $cond, { order_by => 'name' } ); $c->stash->{streams} = $streams; } sub player { my $c = shift; # stream my $stream = $c->schema->resultset('Stream_view')->find({ key => $c->stash->{key} }); if ( ! $stream ) { $c->render('stream/404'); return; } $c->stash->{stream} = $stream; if ( ! $stream->is_granted($c) ) { $c->render('stream/403'); return; } $c->render(); } sub hls { my $c = shift; if ( $c->req->url !~ /^\/hls\/([a-z0-9\-]+)(_\w+)?(\/\w+)?\.(m3u8|ts)$/i ) { $c->render( status => 404, text => ''); return; } my ($type, $file) = ($4, $c->req->url); if ( ! -f $file ) { $c->render( status => 404, text => '' ); return; } if ( $type eq 'ts' ) { # manifesty necheckujeme my $stream = $c->schema->resultset('Stream')->find({ key => $1 }); $c->render( status => 404, text => '' ), return if ! $stream; if ( ! $stream->is_granted($c) ) { $c->render( status => 403, text => ''); return; } } $c->res->headers->content_type(CONTENT_TYPE->{$type}); $c->res->headers->cache_control('max-age=0, no-cache'); $c->res->headers->access_control_allow_origin('*'); $c->reply->file($file); } 1;