forked from LaoFeng-mouse/flyingmouse-format
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
346 lines (320 loc) · 12.2 KB
/
Copy pathcli.js
File metadata and controls
346 lines (320 loc) · 12.2 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env node
"use strict";
const fs = require("node:fs");
const fsp = require("node:fs/promises");
const http = require("node:http");
const path = require("node:path");
const { randomUUID } = require("node:crypto");
const VALUE_OPTIONS = new Map([
["--to", "to"],
["--output", "output"],
["--output-dir", "outputDir"],
["--compression-level", "compressionLevel"],
["--video-codec", "videoCodec"],
["--pdf-action", "pdfAction"],
["--password", "password"]
]);
const HELP = `FlyingMouse Format CLI
Usage:
flyingmouse-format capabilities [--json]
flyingmouse-format targets <file-or-extension> [--json]
flyingmouse-format convert <files...> --to <format> [options]
flyingmouse-format images-to-pdf <images...> [--output <file>] [--json]
flyingmouse-format merge-pdfs <pdfs...> [--output <file>] [--json]
Options:
--output <file> Single-result output path
--output-dir <directory> Output directory for one or more results
--compression-level <0-9> ZIP compression level
--video-codec <h264|h265|av1>
--pdf-action <encrypt|decrypt>
--password <password> PDF password (never printed in JSON output)
--json Stable machine-readable output
-h, --help Show this help
Packaged app:
macOS: "FlyingMouse Format.app/Contents/MacOS/FlyingMouse Format" --cli ...
Windows: "FlyingMouse Format.exe" --cli ...
License: Non-Commercial — personal use only. Commercial resale or rebranding is prohibited.
`;
function parseCliArgs(argv) {
const args = [...argv];
const command = args.shift() || "help";
const files = [];
const options = { json: false, help: false };
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
if (arg === "--json") {
options.json = true;
} else if (arg === "--help" || arg === "-h") {
options.help = true;
} else if (VALUE_OPTIONS.has(arg)) {
const value = args[index + 1];
if (value == null || value.startsWith("--")) throw new Error(`${arg} requires a value.`);
options[VALUE_OPTIONS.get(arg)] = value;
index += 1;
} else if (arg.startsWith("--")) {
throw new Error(`Unknown option: ${arg}`);
} else {
files.push(arg);
}
}
return { command, files, options };
}
function uniqueDestination(filePath) {
if (!fs.existsSync(filePath)) return filePath;
const parsed = path.parse(filePath);
let counter = 1;
let candidate;
do {
candidate = path.join(parsed.dir, `${parsed.name} (${counter})${parsed.ext}`);
counter += 1;
} while (fs.existsSync(candidate));
return candidate;
}
function resolveOutputDestinations(results, options = {}) {
if (options.output && results.length !== 1) {
throw new Error("--output can only be used for one result; use --output-dir for multiple files.");
}
if (options.output && options.outputDir) throw new Error("Use either --output or --output-dir, not both.");
if (options.output) return [path.resolve(options.output)];
const directory = path.resolve(options.outputDir || process.cwd());
const reserved = new Set();
return results.map((result) => {
const initial = path.join(directory, path.basename(result.fileName));
let destination = uniqueDestination(initial);
if (reserved.has(destination)) {
const parsed = path.parse(destination);
let counter = 1;
do {
destination = uniqueDestination(path.join(parsed.dir, `${parsed.name} (${counter})${parsed.ext}`));
counter += 1;
} while (reserved.has(destination));
}
reserved.add(destination);
return destination;
});
}
function sanitizeJsonError(error, options = {}) {
const password = String(options.password || "");
let message = String(error?.message || error || "Unknown error");
if (password) message = message.split(password).join("[redacted]");
return {
ok: false,
error: message,
errorCode: error?.errorCode || "CLI_FAILED"
};
}
function requestJson(url, requestOptions = {}, body) {
return new Promise((resolve, reject) => {
const parsed = new URL(url);
const request = http.request({
hostname: parsed.hostname,
port: parsed.port,
path: `${parsed.pathname}${parsed.search}`,
method: requestOptions.method || "GET",
headers: requestOptions.headers || {}
}, (response) => {
const chunks = [];
response.on("data", (chunk) => chunks.push(chunk));
response.on("end", () => {
let payload;
try {
payload = JSON.parse(Buffer.concat(chunks).toString("utf8"));
} catch {
payload = { error: Buffer.concat(chunks).toString("utf8") || `HTTP ${response.statusCode}` };
}
if ((response.statusCode || 500) >= 400) {
const error = new Error(payload.messages?.zhCN || payload.error || `HTTP ${response.statusCode}`);
error.errorCode = payload.errorCode;
error.payload = payload;
reject(error);
return;
}
resolve(payload);
});
});
request.on("error", reject);
if (body) request.write(body);
request.end();
});
}
async function writeRequestChunk(request, chunk) {
if (request.write(chunk)) return;
await new Promise((resolve) => request.once("drain", resolve));
}
async function postMultipart(url, fields, files, fieldName) {
const boundary = `----flyingmouse-${randomUUID()}`;
const parts = [];
for (const [name, value] of Object.entries(fields)) {
if (value == null || value === "") continue;
parts.push(Buffer.from(`--${boundary}\r\nContent-Disposition: form-data; name="${name}"\r\n\r\n${String(value)}\r\n`));
}
const fileParts = [];
for (const filePath of files) {
const absolute = path.resolve(filePath);
const stat = await fsp.stat(absolute);
if (!stat.isFile()) throw new Error(`Not a file: ${filePath}`);
const safeName = path.basename(absolute).replace(/["\r\n]/g, "_");
const header = Buffer.from(`--${boundary}\r\nContent-Disposition: form-data; name="${fieldName}"; filename="${safeName}"\r\nContent-Type: application/octet-stream\r\n\r\n`);
fileParts.push({ absolute, header, size: stat.size });
}
const closing = Buffer.from(`--${boundary}--\r\n`);
const contentLength = parts.reduce((sum, item) => sum + item.length, 0)
+ fileParts.reduce((sum, item) => sum + item.header.length + item.size + 2, 0)
+ closing.length;
return new Promise((resolve, reject) => {
const parsed = new URL(url);
const request = http.request({
hostname: parsed.hostname,
port: parsed.port,
path: parsed.pathname,
method: "POST",
headers: {
"Content-Type": `multipart/form-data; boundary=${boundary}`,
"Content-Length": contentLength
}
}, (response) => {
const chunks = [];
response.on("data", (chunk) => chunks.push(chunk));
response.on("end", () => {
let payload;
try { payload = JSON.parse(Buffer.concat(chunks).toString("utf8")); }
catch { payload = { error: Buffer.concat(chunks).toString("utf8") }; }
if ((response.statusCode || 500) >= 400) {
const error = new Error(payload.messages?.zhCN || payload.error || `HTTP ${response.statusCode}`);
error.errorCode = payload.errorCode;
error.payload = payload;
reject(error);
} else resolve(payload);
});
});
request.on("error", reject);
(async () => {
try {
for (const part of parts) await writeRequestChunk(request, part);
for (const item of fileParts) {
await writeRequestChunk(request, item.header);
for await (const chunk of fs.createReadStream(item.absolute)) await writeRequestChunk(request, chunk);
await writeRequestChunk(request, Buffer.from("\r\n"));
}
request.end(closing);
} catch (error) {
request.destroy(error);
}
})();
});
}
async function downloadFile(url, destination) {
await fsp.mkdir(path.dirname(destination), { recursive: true });
return new Promise((resolve, reject) => {
const parsed = new URL(url);
const request = http.get(parsed, (response) => {
if (response.statusCode !== 200) {
response.resume();
reject(new Error(`Download failed with HTTP ${response.statusCode}.`));
return;
}
const output = fs.createWriteStream(destination, { flags: "wx" });
response.pipe(output);
output.on("finish", () => output.close(resolve));
output.on("error", reject);
});
request.on("error", reject);
});
}
function extensionFromInput(value) {
const base = path.basename(String(value || ""));
const ext = path.extname(base).replace(/^\./, "");
return ext || base.replace(/^\./, "");
}
function printResult(payload, json) {
if (json) {
process.stdout.write(`${JSON.stringify(payload)}\n`);
return;
}
if (Array.isArray(payload.outputs)) {
for (const output of payload.outputs) process.stdout.write(`${output.path}\n`);
} else {
process.stdout.write(`${JSON.stringify(payload, null, 2)}\n`);
}
}
async function executeCli(parsed, runtime) {
if (parsed.command === "help" || parsed.options.help) return { help: true };
const { startServer } = runtime || require("./server");
const started = await startServer(0);
try {
const baseUrl = started.url;
if (parsed.command === "capabilities") {
return await requestJson(`${baseUrl}/api/capabilities`);
}
if (parsed.command === "targets") {
if (parsed.files.length !== 1) throw new Error("targets requires one file name or extension.");
return await requestJson(`${baseUrl}/api/targets`, {
method: "POST",
headers: { "Content-Type": "application/json" }
}, JSON.stringify({ extension: extensionFromInput(parsed.files[0]) }));
}
if (!parsed.files.length) throw new Error(`${parsed.command} requires at least one input file.`);
let results;
if (parsed.command === "convert") {
if (!parsed.options.to) throw new Error("convert requires --to <format>.");
results = [];
for (const file of parsed.files) {
results.push(await postMultipart(`${baseUrl}/api/convert`, {
targetFormat: parsed.options.to,
compressionLevel: parsed.options.compressionLevel,
videoCodec: parsed.options.videoCodec,
pdfAction: parsed.options.pdfAction,
password: parsed.options.password
}, [file], "file"));
}
} else if (parsed.command === "images-to-pdf") {
results = [await postMultipart(`${baseUrl}/api/convert-images-to-pdf`, {}, parsed.files, "files")];
} else if (parsed.command === "merge-pdfs") {
results = [await postMultipart(`${baseUrl}/api/merge-pdfs`, {}, parsed.files, "files")];
} else {
throw new Error(`Unknown command: ${parsed.command}`);
}
const destinations = resolveOutputDestinations(results, parsed.options);
const outputs = [];
for (let index = 0; index < results.length; index += 1) {
await downloadFile(new URL(results[index].downloadUrl, baseUrl), destinations[index]);
outputs.push({
input: parsed.command === "convert" ? path.resolve(parsed.files[index]) : parsed.files.map((item) => path.resolve(item)),
path: destinations[index],
fileName: results[index].fileName,
mimeType: results[index].mimeType,
warnings: results[index].warnings || []
});
}
return { ok: true, command: parsed.command, outputs };
} finally {
await new Promise((resolve) => started.server.close(resolve));
}
}
async function runCli(argv = process.argv.slice(2), runtime) {
process.env.FLYINGMOUSE_LOG_STDERR = "1";
let parsed;
try {
parsed = parseCliArgs(argv);
const result = await executeCli(parsed, runtime);
if (result.help) process.stdout.write(HELP);
else printResult(result, parsed.options.json);
return 0;
} catch (error) {
const payload = sanitizeJsonError(error, parsed?.options || {});
if (parsed?.options?.json) process.stderr.write(`${JSON.stringify(payload)}\n`);
else process.stderr.write(`Error: ${payload.error}\nRun with --help for usage.\n`);
return 1;
}
}
if (require.main === module) {
runCli().then((code) => { process.exitCode = code; });
}
module.exports = {
HELP,
parseCliArgs,
resolveOutputDestinations,
sanitizeJsonError,
executeCli,
runCli
};