Clean up debug. Clone the entire monitor object into oldMonitor instead of cherry-picking properties. Fixes use of new name when deleting old symlink. Always check for existence of monitor dir and symlink after done saving.

This commit is contained in:
Isaac Connor
2022-10-26 14:31:08 -04:00
parent 33455cbad2
commit 2fb631fa6e

View File

@@ -50,7 +50,6 @@ if ($action == 'save') {
# For convenience
$newMonitor = $_REQUEST['newMonitor'];
ZM\Debug("newMonitor: ". print_r($newMonitor, true));
if (!$newMonitor['ManufacturerId'] and ($newMonitor['Manufacturer'] != '')) {
# Need to add a new Manufacturer entry
@@ -123,9 +122,8 @@ if ($action == 'save') {
}
}
ZM\Debug("newMonitor: ". print_r($newMonitor, true));
$changes = $monitor->changes($newMonitor);
ZM\Debug("Changes: ". print_r($changes, true));
ZM\Debug('Changes: '. print_r($changes, true));
$restart = false;
if (count($changes)) {
@@ -140,37 +138,19 @@ if ($action == 'save') {
}
}
# These are used in updating zones
$oldW = $monitor->Width();
$oldH = $monitor->Height();
$oldMonitor = clone $monitor;
if ($monitor->save($changes)) {
// Groups will be added below
if ( isset($changes['Name']) or isset($changes['StorageId']) ) {
if (isset($changes['Name'])) {
$OldStorage = $monitor->Storage();
$saferOldName = basename($monitor->Name());
if (file_exists($OldStorage->Path().'/'.$saferOldName))
unlink($OldStorage->Path().'/'.$saferOldName);
# Leave old symlinks on old storage areas, as old events will still be there. Only delete the link if the name has changed
if (isset($changes['Name'])) {
$link_path = $oldMonitor->Storage()->Path().'/'.basename($oldMonitor->Name());
if (file_exists($link_path)) {
ZM\Debug("Deleting old link ".$link_path);
unlink($link_path);
} else {
ZM\Debug("Old link didn't exist at ".$link_path);
}
$NewStorage = new ZM\Storage($newMonitor['StorageId']);
if (!file_exists($NewStorage->Path().'/'.$mid)) {
if (!mkdir($NewStorage->Path().'/'.$mid, 0755)) {
ZM\Error('Unable to mkdir '.$NewStorage->Path().'/'.$mid);
}
}
$saferNewName = basename($newMonitor['Name']);
$link_path = $NewStorage->Path().'/'.$saferNewName;
// Use a relative path for the target so the link continues to work from backups or directory changes.
if (!@symlink($mid, $link_path)) {
if (!(file_exists($link_path) and is_link($link_path))) {
ZM\Warning('Unable to symlink ' . $NewStorage->Path().'/'.$mid . ' to ' . $NewStorage->Path().'/'.$saferNewName);
}
}
} // end if Name or Storage Area Change
}
if (isset($changes['Width']) || isset($changes['Height'])) {
$newW = $newMonitor['Width'];
@@ -178,7 +158,7 @@ if ($action == 'save') {
$zones = dbFetchAll('SELECT * FROM Zones WHERE MonitorId=?', NULL, array($mid));
if ( ($newW == $oldH) and ($newH == $oldW) ) {
if ( ($newW == $oldMonitor->Height()) and ($newH == $oldMonitor->Width()) ) {
foreach ( $zones as $zone ) {
$newZone = $zone;
# Rotation, no change to area etc just swap the coords
@@ -209,7 +189,7 @@ if ($action == 'save') {
} # end foreach zone
} else {
$newA = $newW * $newH;
$oldA = $oldW * $oldH;
$oldA = $oldmonitor->Width() * $oldMonitor->Height();
foreach ( $zones as $zone ) {
$newZone = $zone;
@@ -273,25 +253,28 @@ if ($action == 'save') {
intval(($zoneArea*75)/100),
intval(($zoneArea*2)/100)
));
$Storage = $monitor->Storage();
if (!@mkdir($Storage->Path().'/'.$mid, 0755)) {
ZM\Error('Unable to mkdir '.$Storage->Path().'/'.$mid);
} else {
$saferName = basename($newMonitor['Name']);
$link_path = $Storage->Path().'/'.$saferName;
if (!@symlink($mid, $link_path)) {
if (!(file_exists($link_path) and is_link($link_path))) {
ZM\Warning('Unable to symlink ' . $Storage->Path().'/'.$mid . ' to ' . $link_path);
}
}
}
} else {
ZM\Error('Error saving new Monitor.');
return;
}
}
$Storage = $monitor->Storage();
$mid_dir = $Storage->Path().'/'.$mid;
if (!file_exists($mid_dir)) {
if (!@mkdir($mid_dir, 0755)) {
ZM\Error('Unable to mkdir '.$Storage->Path().'/'.$mid);
}
}
$saferName = basename($newMonitor['Name']);
$link_path = $Storage->Path().'/'.$saferName;
if (!@symlink($mid, $link_path)) {
if (!(file_exists($link_path) and is_link($link_path))) {
ZM\Warning('Unable to symlink ' . $Storage->Path().'/'.$mid . ' to ' . $link_path);
}
}
if ( isset($changes['GroupIds']) ) {
dbQuery('DELETE FROM Groups_Monitors WHERE MonitorId=?', array($mid));
foreach ( $changes['GroupIds'] as $group_id ) {