From 62edca6dcbd1e407d3d0d78e2061a74a3d160df8 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Tue, 8 May 2018 09:22:20 -0700 Subject: [PATCH] add fileSize to the api, and use it to add remote fileSize reporting in includes/Event --- web/api/app/Controller/EventsController.php | 1 + web/api/app/Model/Event.php | 11 +++-- web/api/app/Plugin/Crud | 2 +- web/includes/Event.php | 44 +++++++++++++++++++ .../classic/views/report_event_audit.php | 2 +- 5 files changed, 55 insertions(+), 5 deletions(-) diff --git a/web/api/app/Controller/EventsController.php b/web/api/app/Controller/EventsController.php index 2dccf2414..fd0c4ef98 100644 --- a/web/api/app/Controller/EventsController.php +++ b/web/api/app/Controller/EventsController.php @@ -126,6 +126,7 @@ class EventsController extends AppController { $event['Event']['Prev'] = $event_neighbors['prev']['Event']['Id']; $event['Event']['fileExists'] = $this->Event->fileExists($event['Event']); + $event['Event']['fileSize'] = $this->Event->fileSize($event['Event']); # Also get the previous and next events for the same monitor $event_monitor_neighbors = $this->Event->find('neighbors', array( diff --git a/web/api/app/Model/Event.php b/web/api/app/Model/Event.php index 3eb78b9fe..a59393d9a 100644 --- a/web/api/app/Model/Event.php +++ b/web/api/app/Model/Event.php @@ -115,13 +115,13 @@ class Event extends AppModel { } // end function Relative_Path() - public function fileExists( $event ) { + public function fileExists($event) { //$data = $this->findById($id); //return $data['Event']['dataset_filename']; - $storage = $this->Storage->findById( $event['StorageId'] ); + $storage = $this->Storage->findById($event['StorageId']); if ( $event['DefaultVideo'] ) { - if ( file_exists( $storage['Storage']['Path'].'/'.$this->Relative_Path($event).'/'.$event['DefaultVideo'] ) ) { + if ( file_exists($storage['Storage']['Path'].'/'.$this->Relative_Path($event).'/'.$event['DefaultVideo']) ) { return 1; } else { Logger::Debug("FIle does not exist at " . $storage['Storage']['Path'].'/'.$this->Relative_Path($event).'/'.$event['DefaultVideo'] ); @@ -130,5 +130,10 @@ class Event extends AppModel { Logger::Debug("No DefaultVideo in Event" . $this->Event); return 0; } + } // end function fileExists($event) + + public function fileSize($event) { + $storage = $this->Storage->findById($event['StorageId']); + return filesize($storage['Storage']['Path'].'/'.$this->Relative_Path($event).'/'.$event['DefaultVideo']); } } diff --git a/web/api/app/Plugin/Crud b/web/api/app/Plugin/Crud index c3976f147..0bd63fb46 160000 --- a/web/api/app/Plugin/Crud +++ b/web/api/app/Plugin/Crud @@ -1 +1 @@ -Subproject commit c3976f1478c681b0bbc132ec3a3e82c3984eeed5 +Subproject commit 0bd63fb464957080ead342db58ca9e01532cf1ef diff --git a/web/includes/Event.php b/web/includes/Event.php index db55506fe..000f55e30 100644 --- a/web/includes/Event.php +++ b/web/includes/Event.php @@ -566,6 +566,50 @@ class Event { return false; } # end public function file_exists() + public function file_size() { + if ( file_exists($this->Path().'/'.$this->DefaultVideo()) ) { + return filesize($this->Path().'/'.$this->DefaultVideo()); + } + $Storage= $this->Storage(); + $Server = $Storage->ServerId() ? $Storage->Server() : $this->Monitor()->Server(); + if ( $Server->Id() != ZM_SERVER_ID ) { + + $url = $Server->Url() . '/zm/api/events/'.$this->{'Id'}.'.json'; + if ( ZM_OPT_USE_AUTH ) { + if ( ZM_AUTH_RELAY == 'hashed' ) { + $url .= '?auth='.generateAuthHash( ZM_AUTH_HASH_IPS ); + } elseif ( ZM_AUTH_RELAY == 'plain' ) { + $url = '?user='.$_SESSION['username']; + $url = '?pass='.$_SESSION['password']; + } elseif ( ZM_AUTH_RELAY == 'none' ) { + $url = '?user='.$_SESSION['username']; + } + } + Logger::Debug("sending command to $url"); + // use key 'http' even if you send the request to https://... + $options = array( + 'http' => array( + 'header' => "Content-type: application/x-www-form-urlencoded\r\n", + 'method' => 'GET', + 'content' => '' + ) + ); + $context = stream_context_create($options); + try { + $result = file_get_contents($url, false, $context); + if ($result === FALSE) { /* Handle error */ + Error("Error restarting zmc using $url"); + } + $event_data = json_decode($result,true); + Logger::Debug(print_r($event_data['event']['Event'],1)); + return $event_data['event']['Event']['fileSize']; + } catch ( Exception $e ) { + Error("Except $e thrown trying to get event data"); + } + } # end if not local + return 0; + } # end public function file_size() + } # end class ?> diff --git a/web/skins/classic/views/report_event_audit.php b/web/skins/classic/views/report_event_audit.php index cbf01b7b5..5a3b5e5b6 100644 --- a/web/skins/classic/views/report_event_audit.php +++ b/web/skins/classic/views/report_event_audit.php @@ -105,7 +105,7 @@ while( $event = $result->fetch(PDO::FETCH_ASSOC) ) { } # end if has previous events if ( ! $Event->file_exists() ) { $EventsByMonitor[$event['MonitorId']]['FileMissing'][] = $Event; - } else if ( ! filesize( $Event->Path().'/'.$Event->DefaultVideo() ) ) { + } else if ( ! $Event->file_size() ) { $EventsByMonitor[$event['MonitorId']]['ZeroSize'][] = $Event; } $EventsByMonitor[$event['MonitorId']]['Events'][] = $Event;