Skip to content

Commit

Permalink
fix: clean /tmp files after processing and remove all on restarts #76
Browse files Browse the repository at this point in the history
  • Loading branch information
jakowenko committed Aug 25, 2021
1 parent 07dfd25 commit a5c761f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
6 changes: 5 additions & 1 deletion api/src/constants/system.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
3 changes: 2 additions & 1 deletion api/src/routes/index.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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;
6 changes: 3 additions & 3 deletions api/src/util/mqtt.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
},
Expand Down
4 changes: 3 additions & 1 deletion api/src/util/process.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions api/src/util/storage.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down

0 comments on commit a5c761f

Please sign in to comment.