-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwmf2png.js
87 lines (79 loc) · 2.27 KB
/
wmf2png.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Node.js wrapper
//
// Example usage:
// const
// {readFileSync, writeFileSync} = require("fs"),
// wmf2png = require("./wmf2png");
//
// wmf2png(readFileSync("./SIGNATURE.wmf"), (error, output) => {
// if (error) throw error;
// writeFileSync("./sig.png", output);
// });
//
const
fs = require("fs"),
tmp = require("tmp"),
{ join } = require("path"),
{ pipeline } = require("vasync"),
{ execFile } = require("child_process");
function createTmpInputFile (context, next) {
tmp.tmpName((error, path) => {
if (error) {
return next(error);
}
context.tmpInputFile = path;
next();
});
}
function createTmpOutputFile (context, next) {
tmp.tmpName((error, path) => {
if (error) {
return next(error);
}
context.tmpOutputFile = path;
next();
});
}
function saveInputBufferToTmpFile (context, next) {
fs.writeFile(context.tmpInputFile, context.inputBuffer, next);
}
function execWmf2PngExe (context, next) {
execFile(join(__dirname, "wmf2png.exe"), [context.tmpInputFile, context.tmpOutputFile], next);
}
function readTmpOutputFileIntoBuffer (context, next) {
fs.readFile(context.tmpOutputFile, next);
}
function unlinkTmpInputFile (context, next) {
fs.unlink(context.tmpInputFile, next);
}
function unlinkTmpOutputFile (context, next) {
fs.unlink(context.tmpOutputFile, next);
}
module.exports = function wmf2png (input, callback) {
if (process.platform !== "win32") {
return callback(new Error("WMF conversion only works on the win32 platform"));
}
let result = pipeline({
arg: {
inputBuffer: input
},
funcs: [
createTmpInputFile,
createTmpOutputFile,
saveInputBufferToTmpFile,
execWmf2PngExe,
readTmpOutputFileIntoBuffer,
unlinkTmpInputFile,
unlinkTmpOutputFile
]
}, (error, results) => {
if (error) {
return callback(error);
}
results.operations.forEach((operation) => {
if (operation.funcname === "readTmpOutputFileIntoBuffer" && operation.status === "ok") {
callback(null, operation.result);
}
});
});
};