Skip to content

Commit

Permalink
Introduce new download mode that concats event videos into 1 file per…
Browse files Browse the repository at this point in the history
… monitor
  • Loading branch information
Isaac Connor committed Oct 4, 2023
1 parent f864a29 commit 44c5c3c
Show file tree
Hide file tree
Showing 3 changed files with 290 additions and 59 deletions.
36 changes: 24 additions & 12 deletions web/ajax/event.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,28 +123,40 @@
}
break;
case 'download' :
require_once(ZM_SKIN_PATH.'/includes/export_functions.php');
$exportVideo = 1;
$exportFormat = $_REQUEST['exportFormat'];
$exportStructure = 'flat';
$exportIds = !empty($_REQUEST['eids'])?$_REQUEST['eids']:$_REQUEST['id'];
require_once('includes/download_functions.php');
$exportFormat = isset($_REQUEST['exportFormat']) ? $_REQUEST['exportFormat'] : 'zip';
$exportFileName = isset($_REQUEST['exportFileName']) ? $_REQUEST['exportFileName'] : '';

if (!$exportFileName) $exportFileName = 'Export'.(isset($_REQUEST['connkey'])?$_REQUEST['connkey']:'');
$exportFileName = preg_replace('/[^\w\-\.\(\):]+/', '', $exportFileName);

$exportIds = !empty($_REQUEST['eids']) ? $_REQUEST['eids'] : (isset($_REQUEST['id']) ? [$_REQUEST['id']] : []);
ZM\Debug("Export IDS". print_r($exportIds, true));

$filter = isset($_REQUEST['filter']) ? ZM\Filter::parse($_REQUEST['filter']) : null;
if ($filter and !count($exportIds)) {
$eventsSql = 'SELECT E.Id FROM Events AS E WHERE ';
$eventsSql .= $filter->sql();
$results = dbQuery($eventsSql);
while ($event_row = dbFetchNext($results)) {
$exportIds[] = $event_row['Id'];
}
} else {
ZM\Debug("No filter");
}

if ( $exportFile = exportEvents(
$exportIds,
(isset($_REQUEST['connkey'])?$_REQUEST['connkey']:''),
false,#detail
false,#frames
false,#images
true, #$exportVideo,
false,#Misc
$exportFileName,
$exportFormat,
false#,#Compress
, $exportStructure
) ) {
ajaxResponse(array(
'exportFile'=>$exportFile,
'exportFormat'=>$exportFormat,
'connkey'=>(isset($_REQUEST['connkey'])?$_REQUEST['connkey']:'')
));

} else {
ajaxError('Export Failed');
}
Expand Down
135 changes: 88 additions & 47 deletions web/ajax/modals/download.php
Original file line number Diff line number Diff line change
@@ -1,66 +1,65 @@
<?php
require_once('includes/Filter.php');
// This is the HTML representing the Download event modal on the Events page and other pages

function getDLEventHTML($eid, $eids) {
$result = '';

if ( !empty($eid) ) {
$result .= '<input type="hidden" name="id" value="' .validInt($eid). '"/>'.PHP_EOL;

$Event = new ZM\Event($eid);
if ( !$Event->Id() ) {
ZM\Error('Invalid event id');
$result .= '<div class="error">Invalid event id</div>'.PHP_EOL;
} else {
$result .= 'Downloading event ' . $Event->Id . '. Resulting file should be approximately ' . human_filesize( $Event->DiskSpace() ).PHP_EOL;
}
} else if ( !empty($eids) ) {
$total_size = 0;
foreach ( $eids as $eid ) {
if ( !validInt($eid) ) {
ZM\Warning("Invalid event id in eids[] $eid");
continue;
}
$Event = new ZM\Event($eid);
$total_size += $Event->DiskSpace();
$result .= '<input type="hidden" name="eids[]" value="' .validInt($eid). '"/>'.PHP_EOL;
}
unset($eid);
$result .= 'Downloading ' . count($eids) . ' events. Resulting file should be approximately ' . human_filesize($total_size).PHP_EOL;
} else {
$result .= '<div class="warning">There are no events found. Resulting download will be empty.</div>';
}

return $result;
}

if ( !canView('Events') ) {
if (!canView('Events')) {
$view = 'error';
return;
}

$eid = isset($_REQUEST['eid']) ? $_REQUEST['eid'] : '';
$eids = isset($_REQUEST['eids']) ? $_REQUEST['eids'] : array();
$generated = isset($_REQUEST['generated']) ? $_REQUEST['generated'] : '';
$exportFileName = 'zmDownload';

$filter = null;
if (isset($_REQUEST['filter'])) {
$filter = ZM\Filter::parse($_REQUEST['filter']);
#} else if (isset($_SESSION['montageReviewFilter'])) {
#$filter = $_SESSION['montageReviewFilter'];
}

if ($filter) {
if (isset($_REQUEST['MonitorId'])) {
$filter->addTerm(array('attr' => 'Monitor', 'op' => 'IN', 'val' => implode(',', $_REQUEST['MonitorId']), 'cnj' => 'and'));
}
if (isset($_REQUEST['GroupId'])) {
$monitor_ids = [];
foreach ($_REQUEST['GroupId'] as $group_id) {
$group = ZM\Group::find_one(['Id'=>$group_id]);
if ($group) {
$monitor_ids += $group->MonitorIds();
$exportFileName .= ' '.$group->Name();
}
}
$filter->addTerm(array('attr' => 'Monitor', 'op' => 'IN', 'val' => implode(',', $monitor_ids), 'cnj' => 'and'));
}
if (isset($_REQUEST['minTimeSecs'])) {
}
if (isset($_REQUEST['maxTimeSecs'])) {
}
if (isset($_REQUEST['minTime'])) {
$filter->addTerm(array('attr' => 'StartDateTime', 'op' => '>=', 'val' => $_REQUEST['minTime'], 'cnj' => 'and'));
$exportFileName .= ' '.$_REQUEST['minTime'];
}
if (isset($_REQUEST['maxTime'])) {
$filter->addTerm(array('attr' => 'StartDateTime', 'op' => '<=', 'val' => $_REQUEST['maxTime'], 'cnj' => 'and'));
$exportFileName .= ' '.$_REQUEST['maxTime'];
}
}
$total_size = 0;
if ( isset($_SESSION['montageReviewFilter']) and !$eids ) {
if ($filter and !$eids) {
# Handles montageReview filter
$eventsSql = 'SELECT E.Id, E.DiskSpace FROM Events AS E WHERE 1';
$eventsSql .= $_SESSION['montageReviewFilter']['sql'];
$eventsSql = 'SELECT E.Id, E.DiskSpace FROM Events AS E WHERE ';
$eventsSql .= $filter->sql();
$results = dbQuery($eventsSql);
while ( $event_row = dbFetchNext( $results ) ) {
while ($event_row = dbFetchNext($results)) {
array_push($eids, $event_row['Id']);
$total_size += $event_row['DiskSpace'];
}
if ( ! count($eids) ) {
if (!count($eids)) {
ZM\Error("No events found for download using $eventsSql");
}
#session_start();
#unset($_SESSION['montageReviewFilter']);
#session_write_close();
#} else {
#Debug("NO montageReviewFilter");
}

$exportFormat = '';
Expand All @@ -74,6 +73,7 @@ function getDLEventHTML($eid, $eids) {

$focusWindow = true;
$connkey = isset($_REQUEST['connkey']) ? validInt($_REQUEST['connkey']) : generateConnKey();
if ($exportFileName == 'zmDownload') $exportFileName .= '-'.$connkey;

?>
<div id="downloadModal" class="modal" tabindex="-1" role="dialog">
Expand All @@ -93,11 +93,52 @@ function getDLEventHTML($eid, $eids) {
?>
<input type="hidden" name="connkey" value="<?php echo $connkey; ?>"/>
<input type="hidden" name="exportVideo" value="1"/>
<input type="hidden" name="mergeevents" value="1"/>
<?php echo $filter ? $filter->hidden_fields() : '' ?>
<div>
<?php echo getDLEventHTML($eid, $eids) ?>
<?php
$result = '';

if (!empty($eid)) {
$result .= '<input type="hidden" name="id" value="' .validInt($eid). '"/>'.PHP_EOL;

$Event = new ZM\Event($eid);
if (!$Event->Id()) {
ZM\Error('Invalid event id');
$result .= '<div class="error">Invalid event id</div>'.PHP_EOL;
} else {
$result .= 'Downloading event ' . $Event->Id . '. Resulting file should be approximately ' . human_filesize( $Event->DiskSpace() ).PHP_EOL;
}
} else if (!empty($eids)) {
$total_size = 0;
foreach ($eids as $eid) {
if (!validInt($eid)) {
ZM\Warning("Invalid event id in eids[] $eid");
continue;
}
$Event = new ZM\Event($eid);
$total_size += $Event->DiskSpace();
if (!$filter)
$result .= '<input type="hidden" name="eids[]" value="' .validInt($eid). '"/>'.PHP_EOL;
}
unset($eid);
$result .= 'Downloading ' . count($eids) . ' events. Resulting file should be approximately ' . human_filesize($total_size).PHP_EOL;
if (count($eids) > 1000 and !$filter) {
$results .= '<span class="warning">Warning: Too many recordings specified. Download may fail. Please select fewer recordings</span>';
}
} else {
$result .= '<div class="warning">There are no events found. Resulting download will be empty.</div>';
}

echo $result;
?>
</div>
<div class="exportFileName">
<label><?php echo translate('Download File Name') ?></label>
<input type="text" name="exportFileName" value="<?php echo validHtmlStr($exportFileName) ?>" pattern="[A-Za-z0-9 \(\)\.\:\-]+"/>
</div>
<div class="font-weight-bold py-2">
<span class="pr-3"><?php echo translate('ExportFormat') ?></span>
<div class="exportFormat">
<label><?php echo translate('ExportFormat') ?></label>
<input type="radio" id="exportFormatTar" name="exportFormat" value="tar"/>
<label for="exportFormatTar"><?php echo translate('ExportFormatTar') ?></label>
<input type="radio" id="exportFormatZip" name="exportFormat" value="zip" checked="checked"/>
Expand Down
Loading

0 comments on commit 44c5c3c

Please sign in to comment.