forked from apostrophecms/heic-to-jpeg-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (61 loc) · 1.83 KB
/
index.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
57
58
59
60
61
62
const fs = require('fs');
const cp = require('child_process');
module.exports = function(options) {
options = options || {};
const types = {
'image/heif': 1,
'image/heic': 1,
'image/heif-sequence': 1,
'image/heic-sequence': 1
};
return function(req, res, next) {
if (!req.files) {
return next();
}
const relevant = [];
Object.keys(req.files).forEach(function(name) {
var files = req.files[name];
if (!Array.isArray(files)) {
files = [ files ];
}
files.forEach(function(file) {
if (types[file.type] || file.name.toLowerCase().match(/\.(heic|heif)$/)) {
relevant.push(file);
}
});
});
const worker = cp.fork(`${__dirname}/worker.js`);
Promise.all(relevant.map(file => {
const newName = file.name.replace(/\.[^\.]+$/, '.jpg');
let newPath = file.path.replace(/\.[^\.]+$/, '.jpg');
if (!newPath.match(/\.jpg$/)) {
newPath += '.jpg';
}
const newType = 'image/jpeg';
return new Promise((resolve, reject) => {
const errorHandler = (error) => {
console.error(error);
reject(error);
};
worker.once('message', (_message) => {
file.name = newName;
// Avoid leaking many megabytes of disk space
fs.unlinkSync(file.path);
file.path = newPath;
file.type = newType;
// Avoid listener collision
worker.removeListener('error', errorHandler);
resolve(true);
});
worker.once('error', errorHandler);
worker.send({ inputPath: file.path, outputPath: newPath });
});
})).then(o => {
// Gracefully kill the child process
worker.send({ exit: true });
return next();
}).catch(e => {
res.status(500).send('error');
});
}
};