/
index.js
215 lines (202 loc) · 6.55 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* eslint-disable security/detect-child-process */
const fileType = require("file-type");
const fs = require("fs");
const path = require("upath");
const semver = require("semver");
const { execFile } = require("child_process");
const util = require("util");
const execFileAsync = util.promisify(execFile);
/**
* @author Frazer Smith
* @description Check each option provided is valid, of the correct type, and can be used by specified
* version of binary.
* @param {object} acceptedOptions - Object containing options that a binary accepts.
* @param {object} options - Object containing options to pass to binary.
* @param {string=} version - Semantic version of binary.
* @returns {Promise<Array|Error>} Promise of array of CLI arguments on resolve, or Error object on rejection.
*/
function parseOptions(acceptedOptions, options, version) {
return new Promise((resolve, reject) => {
const args = [];
const invalidArgs = [];
Object.keys(options).forEach((key) => {
if (Object.prototype.hasOwnProperty.call(acceptedOptions, key)) {
// eslint-disable-next-line valid-typeof
if (typeof options[key] === acceptedOptions[key].type) {
args.push(acceptedOptions[key].arg);
if (typeof options[key] !== "boolean") {
args.push(options[key]);
}
} else {
invalidArgs.push(
`Invalid value type provided for option '${key}', expected ${
acceptedOptions[key].type
} but received ${typeof options[key]}`
);
}
if (
acceptedOptions[key].minVersion &&
version &&
semver.lt(version, acceptedOptions[key].minVersion)
) {
invalidArgs.push(
`Invalid option provided for the current version of the binary used. '${key}' was introduced in v${acceptedOptions[key].minVersion}, but received v${version}`
);
}
if (
acceptedOptions[key].maxVersion &&
version &&
semver.gt(version, acceptedOptions[key].maxVersion)
) {
invalidArgs.push(
`Invalid option provided for the current version of the binary used. '${key}' is only present up to v${acceptedOptions[key].maxVersion}, but received v${version}`
);
}
} else {
invalidArgs.push(`Invalid option provided '${key}'`);
}
});
if (invalidArgs.length === 0) {
resolve(args);
} else {
reject(new Error(invalidArgs.join("; ")));
}
});
}
class UnRTF {
/**
* @param {string=} binPath - Path of UnRTF binary, i.e. `/usr/bin`
*/
constructor(binPath) {
if (binPath) {
this.unrtfPath = path.normalizeTrim(binPath);
} else if (process.platform === "win32") {
this.unrtfPath = path.joinSafe(
__dirname,
"lib",
"win32",
"unrtf-0.19.3",
"bin"
);
} else {
throw new Error(
`${process.platform} UnRTF binaries are not provided, please pass the installation directory as a parameter to the UnRTF instance.`
);
}
}
/**
* @author Frazer Smith
* @description Converts an RTF file to HTML/LaTeX/RTF/TXT.
* UnRTF will use the directory of the original file to store embedded pictures.
* @param {string} file - Filepath of the RTF file to read.
* @param {object=} options - Object containing options to pass to binary.
* @param {boolean=} options.noPictures - Disable the automatic storing of embedded
* pictures to the directory of the original file.
* @param {boolean=} options.noRemap - Disable charset conversion (only works for 8-bit charsets)
* (UnRTF v0.20.5 or later only).
* @param {boolean=} options.outputHtml - Generate HTML output.
* @param {boolean=} options.outputLatex - Generate LaTeX output.
* @param {boolean=} options.outputPs - Generate PostScript (PS) output (UnRTF v0.19.4 or earlier only).
* @param {boolean=} options.outputRtf - Generate RTF output. (UnRTF v0.21.3 or later only).
* @param {boolean=} options.outputText - Generate ASCII text output.
* @param {boolean=} options.outputVt - Generate text output with VT100 escape codes.
* @param {boolean=} options.outputWpml - Generate WPML output (UnRTF v0.19.4 or earlier only).
* @param {boolean=} options.printVersionInfo - Print copyright and version info.
* @param {boolean=} options.quiet - Do not print any leading comments in output (UnRTF v0.21.3 or later only).
* @returns {Promise<string|Error>} Promise of stdout string on resolve, or Error object on rejection.
*/
async convert(file, options = {}) {
const acceptedOptions = {
noPictures: {
arg: "--nopict",
type: "boolean",
minVersion: "0.0.1",
},
noRemap: {
arg: "--noremap",
type: "boolean",
minVersion: "0.20.5",
},
outputHtml: {
arg: "--html",
type: "boolean",
minVersion: "0.0.1",
},
outputLatex: {
arg: "--latex",
type: "boolean",
minVersion: "0.0.1",
},
outputPs: {
arg: "--ps",
type: "boolean",
minVersion: "0.0.1",
maxVersion: "0.19.4",
},
outputRtf: { arg: "--rtf", type: "boolean", minVersion: "0.21.3" },
outputText: { arg: "--text", type: "boolean", minVersion: "0.0.1" },
outputVt: { arg: "--vt", type: "boolean", minVersion: "0.0.1" },
outputWpml: {
arg: "--wpml",
type: "boolean",
minVersion: "0.0.1",
maxVersion: "0.19.4",
},
printVersionInfo: {
arg: "--version",
type: "boolean",
minVersion: "0.0.1",
},
quiet: { arg: "--quiet", type: "boolean", minVersion: "0.21.3" },
};
try {
/**
* UnRTF will attempt to convert empty strings/files, and non-RTF files
* so catch them here
*/
if (
file === undefined ||
// eslint-disable-next-line security/detect-non-literal-fs-filename
fs.existsSync(path.normalizeTrim(file)) === false
) {
throw new Error("File missing");
}
const results = await fileType.fromFile(path.normalizeTrim(file));
if (
results === undefined ||
results.mime === undefined ||
results.mime !== "application/rtf"
) {
throw new Error(
"File is not the correct media type, expected 'application/rtf'"
);
}
const { stderr } = await execFileAsync(
path.joinSafe(this.unrtfPath, "unrtf"),
["--version"]
);
/**
* UnRTF outputs the version into stderr:
* v0.19.3 returns "0.19.3\r\n"
* v0.21.0 returns "0.21.10\nsearch path is: /usr/share/unrtf/\n"
*/
const versionInfo = /^(\d{1,2}\.\d{1,2}\.\d{1,2})/i.exec(stderr)[1];
const args = await parseOptions(
acceptedOptions,
options,
versionInfo
);
args.push(path.normalizeTrim(file));
const { stdout } = await execFileAsync(
path.joinSafe(this.unrtfPath, "unrtf"),
args
);
return Promise.resolve(stdout);
} catch (err) {
return Promise.reject(err);
}
}
}
module.exports = {
UnRTF,
};