From 76c6e40a74766de3b2667a4c8a26ef0ce03635b7 Mon Sep 17 00:00:00 2001 From: Chrystian Huot Date: Thu, 4 Jul 2019 14:03:31 -0400 Subject: [PATCH] Add pruneDays to settings and logic to prune older calls --- examples/rdio-scanner.service | 2 +- examples/talkgroups.sh | 2 +- examples/upload.sh | 2 +- private/settings.json | 7 ++----- server/imports/api/upload.ts | 37 ++++++++++++++++++++++++++++++++--- 5 files changed, 39 insertions(+), 11 deletions(-) diff --git a/examples/rdio-scanner.service b/examples/rdio-scanner.service index 25e0133..40df350 100644 --- a/examples/rdio-scanner.service +++ b/examples/rdio-scanner.service @@ -18,7 +18,7 @@ Environment=PORT=3000 Environment=HTTP_FORWARDED_COUNT=1 Environment=ROOT_URL=http://radio Environment=MONGO_URL=mongodb://localhost:27017/rdio-scanner -Environment=METEOR_SETTINGS={"apiKeys":["30851354-741b-4b7e-a126-4b56cca99732","5ab8dc98-6274-4f6e-8b50-68eab246a1dc","b29eb8b9-9bcd-4e6e-bb4f-d244ada12736"]} +Environment=METEOR_SETTINGS={"apiKeys":["b29eb8b9-9bcd-4e6e-bb4f-d244ada12736"],"pruneDays":30} [Install] WantedBy=multi-user.target diff --git a/examples/talkgroups.sh b/examples/talkgroups.sh index b55506a..d5b4863 100755 --- a/examples/talkgroups.sh +++ b/examples/talkgroups.sh @@ -1,7 +1,7 @@ #!/bin/bash api="http://127.0.0.1:3000/talkgroups" -key="30851354-741b-4b7e-a126-4b56cca99732" +key="b29eb8b9-9bcd-4e6e-bb4f-d244ada12736" basename=$(basename $2) csv="$2" diff --git a/examples/upload.sh b/examples/upload.sh index 629293b..0d3cab8 100755 --- a/examples/upload.sh +++ b/examples/upload.sh @@ -1,7 +1,7 @@ #!/bin/bash api="http://127.0.0.1:3000/upload" -key="30851354-741b-4b7e-a126-4b56cca99732" +key="b29eb8b9-9bcd-4e6e-bb4f-d244ada12736" basename="${2%.*}" jsonfile="$basename.json" diff --git a/private/settings.json b/private/settings.json index 89e5842..ef2cbf0 100644 --- a/private/settings.json +++ b/private/settings.json @@ -1,7 +1,4 @@ { - "apiKeys": [ - "30851354-741b-4b7e-a126-4b56cca99732", - "5ab8dc98-6274-4f6e-8b50-68eab246a1dc", - "b29eb8b9-9bcd-4e6e-bb4f-d244ada12736" - ] + "apiKeys": ["b29eb8b9-9bcd-4e6e-bb4f-d244ada12736"], + "pruneDays": 30 } \ No newline at end of file diff --git a/server/imports/api/upload.ts b/server/imports/api/upload.ts index 11ad21c..9ccea02 100644 --- a/server/imports/api/upload.ts +++ b/server/imports/api/upload.ts @@ -40,10 +40,11 @@ if (Meteor.isServer) { const apiKeys = Meteor.settings.apiKeys; if (key && Array.isArray(apiKeys) && apiKeys.find((apiKey) => apiKey === key)) { - audio = 'data:audio/mpeg;base64,' + Buffer.from(audio, 'binary').toString('base64'); - try { - const call = new Call(Object.assign({}, JSON.parse(json), { audio, system })); + const call = new Call(Object.assign({}, JSON.parse(json), { + audio: urlEncode('audio/mpeg', audio), + system, + })); Calls.collection.insert(call, (error: Meteor.Error) => { res.writeHead(error ? 500 : 200); @@ -63,9 +64,39 @@ if (Meteor.isServer) { form.parse(req); + pruneCalls(); + } else { res.writeHead(404); res.end(); } }); } + +function base64Encode(value: any): string { + return Buffer.from(value, 'binary').toString('base64'); +} + +function urlEncode(mimeType: string, value: any): string { + return `data:${mimeType};base64,${base64Encode(value)}`; +} + +function getPruneDays(): number | null { + const pruneDays = Meteor.settings.pruneDays; + return typeof pruneDays === 'number' ? pruneDays : null; +} + +function pruneCalls(): void { + const pruneDays = getPruneDays(); + + if (pruneDays !== null) { + const currentDate = new Date(); + const dateLimit = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate() - pruneDays); + + Calls.collection.remove({ + createdAt: { + $lt: dateLimit, + }, + }); + } +}