From a5c761fc01f4d524a45b139d4539d6d85eb4d505 Mon Sep 17 00:00:00 2001 From: David Jakowenko Date: Wed, 25 Aug 2021 00:56:42 -0400 Subject: [PATCH] fix: clean /tmp files after processing and remove all on restarts #76 --- api/src/constants/system.js | 6 +++++- api/src/routes/index.js | 3 ++- api/src/util/mqtt.util.js | 6 +++--- api/src/util/process.util.js | 4 +++- api/src/util/storage.util.js | 5 +++++ 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/api/src/constants/system.js b/api/src/constants/system.js index b144846d..9b796d8b 100644 --- a/api/src/constants/system.js +++ b/api/src/constants/system.js @@ -1,6 +1,10 @@ module.exports.core = { server: { port: 3000 }, - storage: { path: './.storage', config: { path: './.storage/config' } }, + storage: { + path: './.storage', + config: { path: './.storage/config' }, + tmp: { path: '/tmp/double-take' }, + }, }; module.exports.dev = { diff --git a/api/src/routes/index.js b/api/src/routes/index.js index 60895386..cf5b6279 100644 --- a/api/src/routes/index.js +++ b/api/src/routes/index.js @@ -1,5 +1,6 @@ const express = require('express'); const { respond, HTTPError } = require('../util/respond.util'); +const { STORAGE } = require('../constants'); const { NOT_FOUND } = require('../constants/http-status'); const router = express.Router(); @@ -14,7 +15,7 @@ router.use('/train', require('./train.routes')); router.use('/storage', require('./storage.routes')); router.use('/proxy', require('./proxy.routes')); -router.use('/tmp', express.static(`/tmp`)); +router.use(STORAGE.TMP.PATH, express.static(STORAGE.TMP.PATH)); router.all('*', (req, res) => respond(HTTPError(NOT_FOUND, `${req.originalUrl} not found`), res)); module.exports = router; diff --git a/api/src/util/mqtt.util.js b/api/src/util/mqtt.util.js index 1fbc06d3..d6f398bd 100644 --- a/api/src/util/mqtt.util.js +++ b/api/src/util/mqtt.util.js @@ -4,7 +4,7 @@ const mqtt = require('mqtt'); const fs = require('fs'); const { contains } = require('./helpers.util'); const { jwt } = require('./auth.util'); -const { AUTH, SERVER, MQTT, FRIGATE, CAMERAS } = require('../constants'); +const { AUTH, SERVER, MQTT, FRIGATE, CAMERAS, STORAGE } = require('../constants'); let PREVIOUS_MQTT_LENGTHS = []; let JUST_SUBSCRIBED = false; @@ -38,13 +38,13 @@ const processMessage = ({ topic, message }) => { } PREVIOUS_MQTT_LENGTHS.unshift(buffer.length); - fs.writeFileSync(`/tmp/${filename}`, buffer); + fs.writeFileSync(`${STORAGE.TMP.PATH}/${filename}`, buffer); await axios({ method: 'get', url: `http://0.0.0.0:${SERVER.PORT}/api/recognize`, headers: AUTH ? { authorization: jwt.sign({ route: 'recognize' }) } : null, params: { - url: `http://0.0.0.0:${SERVER.PORT}/api/tmp/${filename}`, + url: `http://0.0.0.0:${SERVER.PORT}/api/${STORAGE.TMP.PATH}/${filename}`, type: 'mqtt', camera, }, diff --git a/api/src/util/process.util.js b/api/src/util/process.util.js index 74990ee5..c9f8feb2 100644 --- a/api/src/util/process.util.js +++ b/api/src/util/process.util.js @@ -20,7 +20,7 @@ module.exports.polling = async (event, { retries, id, type, url, breakMatch, MAT for (let i = 0; i < retries; i++) { if (breakMatch === true && MATCH_IDS.includes(id)) break; - const tmp = `/tmp/${id}-${type}-${uuidv4()}.jpg`; + const tmp = `${STORAGE.TMP.PATH}/${id}-${type}-${uuidv4()}.jpg`; const filename = `${uuidv4()}.jpg`; const stream = await this.stream(url); @@ -55,6 +55,8 @@ module.exports.polling = async (event, { retries, id, type, url, breakMatch, MAT allResults.push(...results); + filesystem.delete(tmp); + if (foundMatch) { MATCH_IDS.push(id); if (breakMatch === true) break; diff --git a/api/src/util/storage.util.js b/api/src/util/storage.util.js index d8f44662..b64b7e45 100644 --- a/api/src/util/storage.util.js +++ b/api/src/util/storage.util.js @@ -49,6 +49,11 @@ module.exports.purge = async () => { }; module.exports.setup = () => { + if (fs.existsSync(STORAGE.TMP.PATH)) { + fs.rmdirSync(STORAGE.TMP.PATH, { recursive: true }); + } + fs.mkdirSync(STORAGE.TMP.PATH, { recursive: true }); + if (!fs.existsSync(`${STORAGE.PATH}/matches`)) { fs.mkdirSync(`${STORAGE.PATH}/matches`, { recursive: true }); }