From 13804c0ba9a10e92968c2a6d7aaa96b882766bed Mon Sep 17 00:00:00 2001 From: Moe Date: Wed, 28 Dec 2022 13:28:55 -0800 Subject: [PATCH] Update Monitor Delete function --- languages/en_CA.json | 4 ++- libs/monitor/utils.js | 80 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/languages/en_CA.json b/languages/en_CA.json index e28af401..7be21c75 100644 --- a/languages/en_CA.json +++ b/languages/en_CA.json @@ -11,6 +11,8 @@ "contactAdmin": "Contact the maintainer of your Shinobi installation.", "accountAdded": "Account Added", "accountAddedText": "Account has been added.", + "monitorDeleted": "Monitor Deleted", + "byUser": "by user", "accountDeleted": "Account Deleted", "accountDeletedText": "Account has been deleted.", "accountActionFailed": "Account Action Failed", @@ -1547,7 +1549,7 @@ "fieldTextStreamAcodecAac": "Used for MP4 video.", "fieldTextStreamAcodecAc3": "Used for MP4 video.", "fieldTextStreamAcodecCopy": "Used for MP4 video. Has very low CPU usage but some audio codecs need custom flags like -strict 2 for aac.", - "fieldTextHlsTime": "How long each video segment should be, in minutes. Each segment will be drawn by the client through an m3u8 file. Shorter segments take less space.", + "fieldTextHlsTime": "How long each video segment should be, in seconds. Each segment will be drawn by the client through an m3u8 file. Shorter segments take less space.", "fieldTextHlsListSize": "The number of segments maximum before deleting old segments automatically.", "fieldTextPresetStream": "Preset flag for certain video encoders. If you find your camera is crashing every few seconds : try leaving it blank.", "fieldTextStreamQuality": "Low number means higher quality. Higher number means less quality.", diff --git a/libs/monitor/utils.js b/libs/monitor/utils.js index 64704c59..6c3c61ff 100644 --- a/libs/monitor/utils.js +++ b/libs/monitor/utils.js @@ -2,6 +2,7 @@ const fs = require('fs'); const treekill = require('tree-kill'); const spawn = require('child_process').spawn; const events = require('events'); +const URL = require('url'); const Mp4Frag = require('mp4frag'); const streamViewerCountTimeouts = {} module.exports = (s,config,lang) => { @@ -570,7 +571,86 @@ module.exports = (s,config,lang) => { await deleteFromTable('Files') await deletePath(binDir) } + async function deleteMonitor(options){ + const response = { ok: true } + try{ + const user = options.user + const userId = user.uid + const groupKey = options.ke + const monitorId = options.id || options.mid + const deleteFiles = options.deleteFiles === undefined ? true : options.deleteFiles + s.userLog({ + ke: groupKey, + mid: monitorId + },{ + type: lang.monitorDeleted, + msg: `${lang.byUser} : ${userId}` + }); + s.camera('stop', { + ke: groupKey, + mid: monitorId, + delete: 1, + }); + s.tx({ + f: 'monitor_delete', + uid: userId, + mid: monitorId, + ke: groupKey + },`GRP_${groupKey}`); + await s.knexQueryPromise({ + action: "delete", + table: "Monitors", + where: { + ke: groupKey, + mid: monitorId, + } + }); + if(deleteFiles){ + await deleteMonitorData(groupKey,monitorId) + s.debugLog(`Deleted Monitor Data`,{ + ke: groupKey, + mid: monitorId, + }); + } + response.msg = `${lang.monitorDeleted} ${lang.byUser} : ${userId}` + }catch(err){ + response.ok = false + response.err = err + s.systemLog(err) + } + return response + } + function getUrlProtocol(urlString){ + let modifiedUrlString = `${urlString}`.split('://') + const originalProtocol = `${modifiedUrlString[0]}` + return originalProtocol + } + function modifyUrlProtocol(urlString,newProtocol){ + let modifiedUrlString = `${urlString}`.split('://') + const originalProtocol = `${modifiedUrlString[0]}` + modifiedUrlString[0] = newProtocol; + modifiedUrlString = modifiedUrlString.join('://') + return modifiedUrlString + } + function getUrlParts(urlString){ + const originalProtocol = getUrlProtocol(urlString) + const modifiedUrlString = modifyUrlProtocol(urlString,'http') + const url = URL.parse(modifiedUrlString) + const data = {} + Object.keys(url).forEach(function(key){ + const value = url[key]; + if(value && typeof value !== 'function')data[key] = url[key]; + }); + data.href = `${urlString}` + data.origin = modifyUrlProtocol(data.origin,originalProtocol) + data.protocol = `${originalProtocol}:` + return data + } return { + getUrlProtocol, + modifyUrlProtocol, + getUrlParts, + deleteMonitor, deleteMonitorData, cameraDestroy: cameraDestroy, createSnapshot: createSnapshot,