-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
167 lines (144 loc) · 4.88 KB
/
index.js
File metadata and controls
167 lines (144 loc) · 4.88 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
const express = require('express')
const bp = require('body-parser')
const app = express()
const { exec } = require("child_process");
const fileUpload = require("express-fileupload");
const path = require("path");
const morgan = require("morgan");
const { Z_NO_COMPRESSION } = require('zlib');
const Validator = require('validatorjs');
const validator = (body, rules, customMessages, callback) => {
const validation = new Validator(body, rules, customMessages);
validation.passes(() => callback(null, true));
validation.fails(() => callback(validation.errors, false));
};
app.use(express.static('public'));
app.use(bp.json())
app.use(express.urlencoded({extended: true}));
app.use(fileUpload({
limits: {
fileSize: 4 *1024 * 1024 // 4 MB
},
abortOnLimit: true,
createParentPath: true
}))
app.use(morgan("dev"))
// cache bypass for updated preview/original
app.get('/original/:code', (req, res) => {
res.redirect('/original');
})
app.get('/preview/:code', (req, res) => {
res.redirect('/preview');
})
app.get('/original', (req, res) => {
res.setHeader('Content-Type', 'image/svg+xml');
res.sendFile(__dirname + '/files/in/image.svg');
})
app.get('/preview', (req, res) => {
res.setHeader('Content-Type', 'image/svg+xml');
res.sendFile(__dirname + '/vis.svg');
})
app.post("/upload", (req, res) => {
if (!req.files) {
return res.status(400).send("No files were uploaded.");
}
const filename = req.files.file.name;
const filepath = __dirname + "/files/in/image.svg";
const extensionName = filename.substring(filename.length - 4); // fetch the file extension
const allowedExtension = ['.svg','.SVG'];
if(!allowedExtension.includes(extensionName)){
return res.status(422).send("Invalid Image");
}
req.files.file.mv(filepath, (err) => {
if (err) {
return res.status(500).send(err);
}
return res.send({ status: "success", path: filepath });
});
});
app.post("/render_svg", (req, res) => {
let json = req.body
validator(
json,
{
// validation goes there
"colors_only": "boolean",
"color_key": "regex:/^(#[0-9a-fA-F]{6}\\s?)+$/",
"scale": "required|numeric|min:0.1",
"cut": "in:on",
"hatch": "in:on",
"hatch_density": "numeric|min:0.1",
},
{},
(err, status) => {
if (!status) {
res.status(412).send(err);
} else {
let cmd = "./wild_driver_bin";
cmd += " --json --input " + __dirname + "/files/in/image.svg"
if (json.colors_only) {
cmd += " --colors_only"
}
console.log(json.color_key)
if (json.color_key) {
cmd += ` --color_key ${json.color_key.replace(/#/g, "\\#")} `
}
cmd += " --scale " + parseFloat(json.scale)
if (json.cut) {
cmd += " --cut"
}
if (json.hatch) {
cmd += " --hatch"
}
if (json.hatch_density) {
cmd += " --hatch_density " + parseFloat(json.hatch_density)
}
cmd += " --output " + __dirname + "/files/out/"
exec(cmd + "box.wild --box", (error, stdout, stderr) => {
console.log("====== box ======\n")
console.log(cmd + "box.wild --box\n")
console.log(stdout)
console.log(stderr)
if (error) { return res.json({"success": false, "step": 1, "error": error}) }
exec(cmd + "dry_run.wild --dry_run", (error, stdout, stderr) => {
console.log("====== dry_run ======\n")
console.log(cmd + "dry_run.wild --dry_run\n")
console.log(stdout)
console.log(stderr)
if (error) { return res.json({"success": false, "step": 2, "error": error}) }
exec(cmd + "draw.wild", (error, stdout, stderr) => {
console.log("====== draw ======\n")
console.log(cmd + "draw.wild\n")
console.log(stdout)
console.log(stderr)
if (error) { return res.json({"success": false, "step": 3, "error": error}) }
return res.json({"success": true, "stdout": stdout})
});
});
});
}
}
)
})
app.get('/run/:target', (req, res) => {
let tgt = '';
if(req.params.target === 'box'){ tgt = 'box.wild' }
if(req.params.target === 'dry_run'){ tgt = 'dry_run.wild' }
if(req.params.target === 'draw'){ tgt = 'draw.wild' }
if(tgt === ''){ return res.status(404).send("Invalid endpoint") }
exec(
"stty -F /dev/ttyS0 9600 crtscts && cat ./files/out/" + tgt + " > /dev/ttyS0",
(error, stdout, stderr) => {
console.log("====== send to plotter ======\n")
console.log(error)
console.log(stdout)
console.log(stderr)
return res.json({ "error": error ? true : false, "stdout": stdout, "stderr": stderr });
}
);
})
let port = process.env.PORT || 8080
app.listen(port, (err) => {
if(err) throw err;
console.log("listening on port " + port);
})