-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
207 lines (184 loc) · 6.67 KB
/
server.js
File metadata and controls
207 lines (184 loc) · 6.67 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
var temp = require('temp');
var fs = require('fs-extra');
var async = require('async');
var express = require('express');
var bodyParser = require('body-parser');
var formidable = require('formidable');
var spawn = require('child_process').spawn;
var request = require('request');
var footsee = require("./lib/footsee");
var blender = require('./lib/blender');
var app = express();
// parse request bodies with type application/json
app.use(bodyParser.json({
limit: '10mb',
uploadDir: '/tmp'
}));
app.post('/build', function(req, res) {
var form = new formidable.IncomingForm(),
files = {},
fields = {};
form.on('field', function(field, value) {
fields[field] = value;
});
form.on('file', function(field, file) {
console.log(file.name);
files[field] = file;
});
form.on('end', function() {
console.log(JSON.stringify(fields));
console.log(JSON.stringify(files));
var convert_plantar = spawn('convert', [files.plantar.path, "-resize", "1280x720!", files.plantar.path]);
convert_plantar.on('close', function(code, signal) {
var convert_medial = spawn('convert', [files.medial.path, "-resize", "1280x720!", files.medial.path]);
convert_medial.on('close', function(code, signal) {
footsee.build({
"plantar": files.plantar.path,
"medial": files.medial.path,
"rulerless": files.rulerless.path,
"foot_length": parseFloat(fields.foot_length),
"side": fields.side
}, function(err, data) {
// clean up upload paths
fs.unlink(files.plantar.path);
fs.unlink(files.medial.path);
fs.unlink(files.rulerless.path);
if (err) return res.status(500).end(err);
console.log(data);
if (fields.debug=="1") {
req.registration = data;
return debug(req, res);
}
var output_base_filename = "model";
var blend_path = temp.path({suffix: '.blend'});
var transform_mesh = [
// make a copy of the things model
fs.copy.bind(fs, "./models/things.blend", blend_path),
blender.apply_warp.bind(blender, data, blend_path)
];
if (fields.side=="left") {
transform_mesh.push(blender.apply_warp.bind(blender, {warp: [[-1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]}, blend_path));
}
// optionally engrave label
if (fields.label) {
output_base_filename = fields.label;
transform_mesh.push(function(callback) {
strToImg(fields.label, function(err, img_path) {
console.log("handwriting.io "+img_path);
imgToSvg(img_path, function(err, svg_path) {
console.log("svg "+svg_path);
blender.add_path({model_name: fields.model_name, svg_path: svg_path}, blend_path, function(err, output) {
callback(null, output);
});
}); // imgToSvg
}); // strToImg
});
}
// optionally export specific component
if (fields.model_name) {
var stl_path = temp.path({suffix: '.stl'});
transform_mesh.push(blender.export_stl.bind(blender, {"model_name":fields.model_name}, stl_path, blend_path));
}
async.series(transform_mesh, function(err, filenames) {
if (fields.model_name) {
res.setHeader('Content-disposition', `attachment; filename=${output_base_filename}.stl`);
fs.readFile(stl_path, function(err, file) {
res.end(file);
fs.unlink(stl_path);
});
} else {
res.setHeader('Content-disposition', `attachment; filename=${output_base_filename}.blend`);
fs.readFile(blend_path, function(err, file) {
res.end(file);
fs.unlink(blend_path);
});
}
fs.unlink(data.output);
});
}); // footsee.build
});
});
}); // form end
form.parse(req);
});
app.get('/:any.jpg', function(req, res) {
fs.readFile(req.params.any+".jpg", function(err, data) {
if (err) return res.status(404);
res.end(data);
});
});
app.get('/', function(req, res) {
res.end(`
<style>
label {
}
</style>
<form action="build" method="POST" enctype="multipart/form-data" >
<label>label</label>
<input type="text" name="label" /></br>
<label>medial</label>
<input type="file" name="medial" /></br>
<label>plantar</label>
<input type="file" name="plantar" /></br>
<label>side</label>
<select name="side">
<option value="left">left</option>
<option value="right">right</option>
</select>
<hr>
<label>ruler(less) measurement</label>
<input type="file" name="rulerless" /></br>
<label>or enter a foot length</label>
<input type="text" name="foot_length" />
<hr>
<select name="debug">
<option value="1">debug mode</option>
<option value="0">download model</option>
</select>
<label>download_options</label>
<select name="model_name">
<option value="">download everything</option>
<option value="footbed">download insole</option>
<option value="john_arch_footbed">download john arch</option>
</select>
<hr>
<input type="submit" value="upload" />
</form>
`);
});
var server = app.listen(80, function () {
});
var debug = function(req, res) {
blender.read_measurement({measurement: "foot_length"}, req.registration.output, function(err, measurement) {
res.setHeader('Content-disposition', 'text/HTML');
res.end(`
<html>
${measurement}
<img src="rulerless_seg.jpg" />
<img src="medial_sagittal_warped.jpg" />
<img src="plantar_transverse_warped.jpg" />
</html>
`);
});
}
// create a svg from an image
var imgToSvg = function(img_path, callback) {
var pnm_path = temp.path({suffix: '.pnm'});
var svg_path = temp.path({suffix: '.svg'});
var convert = spawn('convert', [img_path, "-alpha", "remove", "-resize", "256x256", pnm_path]);
convert.on('close', function(code, signal) {
var potrace = spawn('potrace', ["--svg", "-k", "0.5", pnm_path, "-o", svg_path]);
potrace.on('close', function(code, signal) {
callback(null, svg_path);
});
});
};
// create an image from a string
var strToImg = function(string, callback) {
var image_path = temp.path({suffix: '.png'});
var writeStream = fs.createWriteStream(image_path);
request(`https://9YVSD2H8A9DCK8A8:DNZCYBZSCP83N31F@api.handwriting.io/render/png/?handwriting_id=31SB2CWG00DZ&handwriting_size=70px&line_spacing=1.5&width=720px&height=470px&text=${string}`).pipe(writeStream);
writeStream.on('finish', function() {
callback(null, image_path);
});
};