/
getCaffBytes.js
56 lines (54 loc) · 2.37 KB
/
getCaffBytes.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const fs = require('fs');
const crypto = require('crypto');
const caffParser = require('../build/Release/caffparser');
const streamBuffers = require('stream-buffers');
function decryptCaff(encoded) {
let combined = Buffer.from(encoded, 'hex');
const ENC_KEY = Buffer.from(process.env.AES_KEY, 'hex');
const IV = Buffer.alloc(16);
combined.copy(IV, 0, 0, 16);
const encryptedData = combined.slice(16).toString('binary');
let decipher = crypto.createDecipheriv('aes-256-cbc', ENC_KEY, IV);
let decrypted = decipher.update(encryptedData, 'binary', 'utf8') + decipher.final('utf8');
return decrypted;
}
module.exports = function () {
return async function(req, res, next) {
res.set('Access-Control-Allow-Origin', '*');
if (res.locals.caff) {
let caffFileName = './caffs/' + res.locals.caff._id + "_encrypted.caff";
fs.readFile(caffFileName, 'utf8', (err, encryptedData) => {
if (err) {
console.log(err);
return res.status(400).end();
}
const caff = Buffer.from(decryptCaff(encryptedData), 'binary');
const caffTempFileName = './tmp/' + Date.now() + res.locals.user._id + "_" + res.locals.caff.id + "_tmp.caff";
fs.writeFile(caffTempFileName, caff, "utf8", (err) => {
if (err) {
console.log(err);
return res.status(400),end();
}
readStream = fs.createReadStream(caffTempFileName);
readStream.on('open', () => {
res.writeHead(200, {
"Content-Type": "application/octet",
"Content-Disposition": "attachment; filename=" + res.locals.caff._id + ".caff"
});
readStream.pipe(res);
});
readStream.on('error', (err) => {
console.log(err);
fs.unlinkSync(caffTempFileName);
return res.status(400).end();
});
readStream.on('end', () => {
fs.unlinkSync(caffTempFileName);
});
});
});
} else {
return res.status(400).end();
}
};
};