-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
98 lines (90 loc) · 3.03 KB
/
index.js
File metadata and controls
98 lines (90 loc) · 3.03 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
const fs = require('fs');
const path = require('path');
const express = require('express');
const sharp = require('sharp');
const svgmod = require('./svgmod');
const wikimediaApi = require('./wikimediaApi');
const log = require('./log')
function generateLabel(page, size=1.5, callback=null) {
wikimediaApi.getPage(page, (body, err) => {
if (!body) {
if (callback) callback(null, err);
else throw err;
return;
}
params = wikimediaApi.extractNameAndId(body.title);
wikijump2x1 = svgmod.templates.wikijump2x1;
template = svgmod.openSVG(wikijump2x1.path);
// replace contents
new_url = wikimediaApi.WIKI_ENDPOINT + params.id;
svgmod.replaceText(svgmod.getElement(template, wikijump2x1.url), new_url);
svgmod.replaceQRCode(svgmod.getElement(template, wikijump2x1.qrcode), new_url);
try {
svgmod.replaceBoxedText(template, wikijump2x1.name, params.name);
} catch (e) {
if (e.message.includes('Content does not fit')) {
log.warn(`ID:${params.id} '${params.name}' doesn't fit on the label. Review this label to determine if it is appropriate. Continuing...`);
} else {
throw e;
}
}
svgmod.replaceText(svgmod.getElement(template, wikijump2x1.id), params.id);
if (callback) {
callback(template, null);
} else {
// save to file
svgmod.saveAsPng(template, path.join(`./output/${params.id}`), size);
}
});
}
const app = express();
const port = 3000;
app.get('/', (req, res) => {
// validate
let errors = [];
query = req.query;
if (!('id' in query)) errors.push('"id" not supplied as query parameter. Provide something that exists in the Protospace wiki');
if ('size' in query && isNaN(query.size)) errors.push('"size" must be a number');
if ('size' in query && query.size > 4) errors.push('"size" cannot be >4');
if ('size' in query && query.size < 1) errors.push('"size" much be >=1');
if (errors.length > 0) {
res.status(400).send('Please fix the following errors:<br/><br/>' + errors.join('<br/>')).end();
return
}
// TODO: add 1x2 template and provide that via query param
size = query.size || 1.5
log.info(`call ${req.url}`)
generateLabel(query.id, size, (svg, err) => {
if (err) {
if (err.includes('The page you specified doesn\'t exist')) {
res
.status(400)
.end(`Page with ID ${query.id} doesn't exist. Please select a different page.`);
return;
} else {
res
.status(500)
.end(`Failed to get data from wiki with error: ${err}`);
return;
}
}
dpi = 300;
height = size * dpi;
let img = sharp(Buffer.from(svg.svg()), {density: dpi})
.resize(height)
.png()
.toBuffer((err, data, info) => {
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': info.size
});
res.end(data);
});
});
});
app.listen(port, () => {
log.info(`Listening at http://localhost:${port}`);
});
process.on('SIGINT', function() {
process.exit();
});