-
Notifications
You must be signed in to change notification settings - Fork 0
/
temptracker.js
42 lines (35 loc) · 910 Bytes
/
temptracker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict';
const fs = require('fs');
const util = require('util');
const path = require('path');
const uuid = require('uuid/v4');
const unlink = util.promisify(fs.unlink);
class TempTracker {
constructor(tempDir) {
this.files = [];
this.tempDir = tempDir;
this.id = uuid();
}
add(file) {
this.files.push(file);
}
create(ext) {
let tempFile = `${this.tempDir}/${this.id}.${ext}`;
tempFile = path.normalize(tempFile);
this.add(tempFile);
return tempFile;
}
async cleanup() {
let tasks = this.files.map(async f => {
try {
await unlink(f);
} catch (ex) {
if (ex.code != 'ENOENT') {
console.error(ex);
}
}
});
await Promise.all(tasks);
}
}
module.exports.TempTracker = TempTracker;