diff --git a/lib/PiTube.pm b/lib/PiTube.pm index 643b2d2b17b1bb6ff49e3f3f3ccde8b92f82e52c..69e797fd02a1eea2b9551410f08adea6bf70c704 100644 --- a/lib/PiTube.pm +++ b/lib/PiTube.pm @@ -73,6 +73,8 @@ sub startup { $r->get('/api/streams')->to('Stream#list'); $r->get('/streams/:id')->to('Stream#info'); + $r->put('/streams/:id/recording')->to('Stream#recording'); + # $r->get('/streams/add')->over( is => 'administrator' )->to('Stream#add'); # $r->get('/streams/:id/form')->over( is => 'administrator' )->to('Stream#form'); # $r->post('/streams')->over( is => 'administrator' )->to('Stream#create'); diff --git a/lib/PiTube/Controller/Stream.pm b/lib/PiTube/Controller/Stream.pm index ff936c5a69d20865a9e707e6fae77313e201dc4d..b9f7954dcf80bf0b65c02a13613901b39390b238 100644 --- a/lib/PiTube/Controller/Stream.pm +++ b/lib/PiTube/Controller/Stream.pm @@ -1,11 +1,14 @@ package PiTube::Controller::Stream; use Mojo::Base 'Mojolicious::Controller'; +use Mojo::UserAgent; use constant CONTENT_TYPE => { m3u8 => 'application/vnd.apple.mpegurl', ts => 'video/mp2t', }; +use constant CONTROL_URL => '%s/control/record/%s?app=stream&name=%s&rec=%s'; + sub list { my $c = shift; @@ -39,7 +42,7 @@ sub list { $stream->get_columns(), is_live => $stream->is_live, is_writeable => $stream->is_writeable( $rights ), - is_recordable => $stream->is_recordable( $c->session->{user}{username} ), + is_recordable => $stream->is_recordable( $c->session->{user}{id} ), ); delete $stream{publish_user_name} if ! $stream->is_live; @@ -112,12 +115,58 @@ sub info { $stream->get_columns(), is_live => $stream->is_live, is_writeable => $stream->is_writeable( $rights ), - is_recordable => $stream->is_recordable( $c->session->{user}{username} ), + is_recordable => $stream->is_recordable( $c->session->{user}{id} ), }; $c->render(); } +sub recording { + my $c = shift; + + # akce + if ( ! $c->param('action') || $c->param('action') !~ /(stop|start)/) { + $c->render( status => 400, text => ''); + return; + } + + # stream + my $stream = $c->schema->resultset('Stream')->find({ + id => $c->stash->{id} + }); + + if ( ! $stream ) { + $c->render( status => 404, text => ''); + return; + } + + if ( $stream->publish_user_id != $c->session->{user}{id}) { + $c->render( status => 403, text => ''); + return; + } + + my $ua = Mojo::UserAgent->new; + + my $res = $ua->get(sprintf(CONTROL_URL, + $c->config->{nginx}{base_url}, + $c->param('action'), + $stream->key, + 'all' + ))->result; + + my $recording = ( $c->param('action') eq 'start' ) ? 1 : 0; + + if ($res->is_success) { + # TRANSAKCE + $stream->update({ + recording => $recording + }); + # create/update records + } + + $c->render( json => { recording => $stream->recording } ); +} + 1; __END__