-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
275 lines (226 loc) · 9.02 KB
/
server.js
File metadata and controls
275 lines (226 loc) · 9.02 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
#!/usr/bin/env node
/**
* ptouch-print web UI server
* No npm dependencies — uses only Node.js built-ins.
*/
const http = require('http');
const fs = require('fs');
const path = require('path');
const { execFile } = require('child_process');
const os = require('os');
const crypto = require('crypto');
const PORT = process.env.PORT || 3000;
const BINARY = path.join(__dirname, 'bin', 'ptouch-print');
const TMP_DIR = os.tmpdir();
const HTML_FILE = path.join(__dirname, 'public', 'index.html');
// ── helpers ──────────────────────────────────────────────────────────────────
function tmpFile(ext) {
return path.join(TMP_DIR, `ptouch-${crypto.randomBytes(6).toString('hex')}${ext}`);
}
function runBinary(args) {
console.log('[run]', BINARY, args.join(' '));
return new Promise((resolve, reject) => {
execFile(BINARY, args, { timeout: 30_000 }, (err, stdout, stderr) => {
const output = (stdout + '\n' + stderr).trim();
if (err && err.code !== 0) {
console.error('[err]', output || err.message);
reject(new Error(output || err.message));
} else {
console.log('[out]', output || '(no output)');
resolve(output);
}
});
});
}
function parseMultipart(body, boundary) {
const parts = {};
const sep = Buffer.from('--' + boundary);
const end = Buffer.from('--' + boundary + '--');
let start = 0;
while (start < body.length) {
const boundaryIdx = indexOf(body, sep, start);
if (boundaryIdx === -1) break;
const slice = body.slice(boundaryIdx, boundaryIdx + end.length);
if (slice.equals(end)) break;
const headerStart = boundaryIdx + sep.length + 2; // skip \r\n
const headerEnd = indexOf(body, Buffer.from('\r\n\r\n'), headerStart);
if (headerEnd === -1) break;
const headerStr = body.slice(headerStart, headerEnd).toString();
const dataStart = headerEnd + 4;
const nextBoundary = indexOf(body, sep, dataStart);
const dataEnd = nextBoundary === -1 ? body.length : nextBoundary - 2; // strip trailing \r\n
const data = body.slice(dataStart, dataEnd);
const nameMatch = headerStr.match(/name="([^"]+)"/);
const filenameMatch = headerStr.match(/filename="([^"]+)"/);
if (nameMatch) {
const name = nameMatch[1];
parts[name] = filenameMatch ? { filename: filenameMatch[1], data } : data.toString();
}
start = nextBoundary === -1 ? body.length : nextBoundary;
}
return parts;
}
function indexOf(buf, search, offset = 0) {
for (let i = offset; i <= buf.length - search.length; i++) {
let found = true;
for (let j = 0; j < search.length; j++) {
if (buf[i + j] !== search[j]) { found = false; break; }
}
if (found) return i;
}
return -1;
}
function readBody(req) {
return new Promise((resolve, reject) => {
const chunks = [];
req.on('data', c => chunks.push(c));
req.on('end', () => resolve(Buffer.concat(chunks)));
req.on('error', reject);
});
}
function json(res, status, obj) {
const body = JSON.stringify(obj);
res.writeHead(status, { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) });
res.end(body);
}
// ── build args ────────────────────────────────────────────────────────────────
function buildArgs(payload, previewPath, uploadedFiles) {
const args = [];
if (payload.debug) args.push('--debug');
if (payload.font) args.push(`--font=${payload.font}`);
if (payload.fontsize) args.push(`--font-size=${payload.fontsize}`);
if (payload.fontmargin != null && payload.fontmargin !== '') args.push(`--font-margin=${payload.fontmargin}`);
if (payload.align) args.push(`--align=${payload.align}`);
if (payload.copies && payload.copies > 1) args.push(`--copies=${payload.copies}`);
if (payload.timeout != null && payload.timeout !== '') args.push(`--timeout=${payload.timeout}`);
if (payload.forceTapeWidth) args.push(`--force-tape-width=${payload.forceTapeWidth}`);
if (previewPath) args.push(`--write-png=${previewPath}`);
// Resolve valid labels first so we know which is last
const valid = [];
for (const label of (payload.labels || [])) {
if (label.type === 'text') {
let lines = label.lines || [];
let s = 0, e = lines.length;
while (s < e && lines[s].trim() === '') s++;
while (e > s && lines[e - 1].trim() === '') e--;
lines = lines.slice(s, e);
if (lines.length > 0) valid.push({ label, lines });
} else if (label.type === 'image') {
const file = uploadedFiles[label.id];
if (file) valid.push({ label, file });
}
}
const expanded = [];
for (const item of valid) {
const qty = Math.max(1, parseInt(item.label.qty) || 1);
for (let q = 0; q < qty; q++) expanded.push(item);
}
for (let i = 0; i < expanded.length; i++) {
const { label, lines, file } = expanded[i];
const isLast = i === expanded.length - 1;
if (label.pad != null && label.pad !== '') args.push(`--pad=${label.pad}`);
if (lines) {
args.push(`--text=${lines[0]}`);
for (let j = 1; j < lines.length; j++) args.push(`--newline=${lines[j]}`);
} else {
args.push(`--image=${file}`);
}
if (!isLast && !payload.cutBetween) args.push('--chain');
if (isLast && !payload.finalCut) args.push('--chain');
}
return args;
}
// ── routes ────────────────────────────────────────────────────────────────────
async function handleInfo(req, res) {
try {
const output = await runBinary(['--info']);
json(res, 200, { output });
} catch (e) {
json(res, 500, { error: e.message });
}
}
async function handleListSupported(req, res) {
try {
const output = await runBinary(['--list-supported']);
json(res, 200, { output });
} catch (e) {
json(res, 500, { error: e.message });
}
}
async function handlePrint(req, res) {
const body = await readBody(req);
const ct = req.headers['content-type'] || '';
const boundaryMatch = ct.match(/boundary=(.+)$/);
let payload;
const uploadedFiles = {}; // label id -> tmp file path
const tmpFiles = [];
try {
if (boundaryMatch) {
const parts = parseMultipart(body, boundaryMatch[1]);
payload = JSON.parse(parts.payload);
for (const [key, val] of Object.entries(parts)) {
if (key !== 'payload' && val && val.data) {
const tmpPath = tmpFile('.png');
fs.writeFileSync(tmpPath, val.data);
uploadedFiles[key] = tmpPath;
tmpFiles.push(tmpPath);
}
}
} else {
payload = JSON.parse(body.toString());
}
const action = payload.action || 'print';
if (action === 'info') {
const output = await runBinary(['--info']);
return json(res, 200, { output });
}
let previewPath = null;
if (action === 'preview') {
previewPath = tmpFile('.png');
tmpFiles.push(previewPath);
}
const args = buildArgs(payload, previewPath, uploadedFiles);
if (args.length === 0) {
return json(res, 400, { error: 'No print content specified.' });
}
const output = await runBinary(args);
if (action === 'preview' && fs.existsSync(previewPath)) {
const b64 = fs.readFileSync(previewPath).toString('base64');
return json(res, 200, { output, preview: `data:image/png;base64,${b64}` });
}
json(res, 200, { output });
} catch (e) {
json(res, 500, { error: e.message });
} finally {
for (const f of tmpFiles) try { fs.unlinkSync(f); } catch (_) {}
for (const f of Object.values(uploadedFiles)) try { fs.unlinkSync(f); } catch (_) {}
}
}
// ── server ────────────────────────────────────────────────────────────────────
const server = http.createServer(async (req, res) => {
const t = Date.now();
res.on('finish', () => console.log(`[req] ${req.method} ${req.url} → ${res.statusCode} (${Date.now()-t}ms)`));
res.setHeader('X-Content-Type-Options', 'nosniff');
if (req.method === 'GET' && req.url === '/') {
fs.readFile(HTML_FILE, (err, data) => {
if (err) { res.writeHead(500); res.end('Could not load UI'); return; }
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': data.length });
res.end(data);
});
return;
}
if (req.method === 'GET' && req.url === '/api/info') {
return handleInfo(req, res);
}
if (req.method === 'GET' && req.url === '/api/list-supported') {
return handleListSupported(req, res);
}
if (req.method === 'POST' && req.url === '/api/print') {
return handlePrint(req, res);
}
res.writeHead(404);
res.end('Not found');
});
server.listen(PORT, '127.0.0.1', () => {
console.log(`ptouch-print UI → http://127.0.0.1:${PORT}`);
});