Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
124 lines (108 loc) · 4.01 KB
/
Copy pathserver.js
File metadata and controls
124 lines (108 loc) · 4.01 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
const express = require('express');
const path = require('path');
const { spawn } = require('child_process');
const app = express();
const port = process.env.PORT || 3000;
const PRINTER_NAME = process.env.PRINTER_NAME || 'Zebra_ZD220';
const fs = require('fs');
app.use(express.json({ limit: '5mb' }));
app.use(express.static('public'));
// Auto-sync uploaded images from tempmediaStorage if available
const tempMediaDir = '/Users/mp/.gemini/antigravity/brain/tempmediaStorage';
const publicIconsDir = path.join(__dirname, 'public', 'icons');
function syncTempIcons() {
try {
if (fs.existsSync(tempMediaDir)) {
const files = fs.readdirSync(tempMediaDir);
console.log(`[TempMedia] Files in tempMediaDir:`, files);
files.forEach(file => {
if (file.endsWith('.png') || file.endsWith('.svg') || file.endsWith('.jpg') || file.endsWith('.jpeg')) {
const srcPath = path.join(tempMediaDir, file);
const stats = fs.statSync(srcPath);
if (stats.size > 0) {
// Determine target icon name: if file is media_..., we can check if it matches gaming or film or copy it
console.log(`[TempMedia] Found image: ${file} (${stats.size} bytes)`);
}
}
});
}
} catch (e) {
console.error(`[TempMedia] Error reading dir:`, e.message);
}
}
syncTempIcons();
/**
* GET /api/icons
* Returns mapping of icon ID to filename in public/icons
*/
app.get('/api/icons', (req, res) => {
try {
const files = fs.readdirSync(publicIconsDir);
const iconMap = {};
files.forEach(file => {
const ext = path.extname(file);
const name = path.basename(file, ext).toLowerCase();
if (['.png', '.svg', '.jpg', '.jpeg', '.webp'].includes(ext.toLowerCase())) {
iconMap[name] = file;
}
});
res.json({ success: true, icons: iconMap });
} catch (err) {
res.status(500).json({ success: false, error: err.message });
}
});
// Request logging & CORS middleware (allows GitHub Pages to hit local print server)
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
if (req.method === 'OPTIONS') {
return res.sendStatus(200);
}
console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`);
next();
});
/**
* POST /api/print
* Accepts { zpl } — pre-rendered ZPL string from the browser
*/
app.post('/api/print', (req, res) => {
try {
const { zpl } = req.body;
if (!zpl || typeof zpl !== 'string') {
return res.status(400).json({ success: false, error: 'ZPL data is required.' });
}
// Pipe ZPL directly to lp via stdin
const lp = spawn('lp', ['-d', PRINTER_NAME, '-o', 'raw']);
let stderr = '';
lp.stderr.on('data', (data) => { stderr += data.toString(); });
lp.on('close', (code) => {
if (code !== 0) {
console.error(`Print error (exit ${code}): ${stderr}`);
let errorMsg = 'Failed to print badge.';
if (stderr.includes('does not exist')) {
errorMsg = `Printer '${PRINTER_NAME}' is not set up. Run: bash setup.sh`;
}
return res.status(500).json({ success: false, error: errorMsg });
}
console.log(`Badge printed successfully`);
res.json({ success: true, message: 'Badge sent to printer.' });
});
lp.stdin.write(zpl);
lp.stdin.end();
} catch (error) {
console.error(`Error processing print request: ${error.message}`);
res.status(500).json({ success: false, error: 'Internal server error.' });
}
});
// Error handling
app.use((err, req, res, next) => {
console.error(`Unhandled error: ${err.message}`);
res.status(500).json({ success: false, error: 'An unexpected error occurred.' });
});
app.listen(port, () => {
console.log(`\n🏷️ Badge Printer Kiosk`);
console.log(` Open: http://localhost:${port}`);
console.log(` Printer: ${PRINTER_NAME}`);
console.log(``);
});