From aa5e4cda57817e34fd25fc7b5cf6f97c2b1dc2be Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz>
Date: Wed, 25 Nov 2020 01:32:08 +0100
Subject: [PATCH] pridan endpoint pro rizeni nahravani

---
 lib/PiTube.pm                   |  2 ++
 lib/PiTube/Controller/Stream.pm | 53 +++++++++++++++++++++++++++++++--
 2 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/lib/PiTube.pm b/lib/PiTube.pm
index 643b2d2..69e797f 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 ff936c5..b9f7954 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__
-- 
GitLab