-
Notifications
You must be signed in to change notification settings - Fork 13
/
webserver.js
313 lines (261 loc) · 7.64 KB
/
webserver.js
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
var fs = require('fs');
var path = require('path');
var zipstream = require('zipstream');
var async = require('async');
var QRCode = require('qrcode-npm');
var xpath = require('xpath');
var dom = require('xmldom').DOMParser;
var express = require('express');
var dns = require('dns');
var os = require('os');
var busboy = require('connect-busboy');
var app = express();
/**
* Features:
* - project listing
* - on-the-fly compression for packed downloads
* - project creation/updating
*
* TODO:
* - subdir for each project?
*/
app.use(busboy());
app.use(express.logger());
WEBCONTENT_DIR = path.join(__dirname, 'WebContent');
VIEW_DIR = path.join(__dirname, 'views');
PROJECT_DIR = path.join(WEBCONTENT_DIR, 'data');
PORT = parseInt(process.argv[2], 10) || 8888;
// static files
app.use(express.static(WEBCONTENT_DIR));
app.set('views', VIEW_DIR);
app.set('view engine', 'jade');
function readAppPackage(cb) {
fs.readFile('android_package.json', function (err, data) {
if (err)
return cb(err);
cb(err, JSON.parse(data));
});
}
function getServerIPs(cb) {
var interfaces = os.networkInterfaces();
var addresses = [];
for (k in interfaces) {
for (k2 in interfaces[k]) {
var address = interfaces[k][k2];
if (address.family == 'IPv4' && !address.internal) {
addresses.push(address.address)
}
}
}
console.log('addresses:'+addresses);
cb(null, addresses);
}
function getServerIP(cb) {
dns.lookup(os.hostname(), cb);
}
function extractSvgs(xml_file, cb) {
fs.readFile(xml_file, {encoding: 'utf8'}, function (err, data) {
if (err) return cb(err);
var doc = new dom().parseFromString(data);
var nodes = xpath.select("//svgpath", doc);
async.map(nodes, function (node, cb) {
cb(null, node.firstChild.data);
}, cb);
});
}
function projectFiles(project, cb) {
var files = {};
var xml_file = project+'.xml';
var arff_file = project+'.arff';
files.xml = xml_file;
files.arff = arff_file;
async.parallel({
svg: function (cb) {
extractSvgs(path.join(PROJECT_DIR, xml_file), cb);
},
ip: getServerIP,
ips: getServerIPs
}, function (err, results) {
if (err) return cb(err);
files.svg = results.svg;
files.zip = 'http://'+results.ip[0]+':'+PORT+'/projects/'+project+'.zip';
files.zips = [];
for (var i = 0; i < results.ips.length; i++) {
files.zips.push('http://'+results.ips[i]+':'+PORT+'/projects/'+project+'.zip');
}
cb(null, files);
});
}
// html + js for android app
app.get('/appupdate/get.zip', function (req, res) {
readAppPackage(function (err, data) {
if (err) {
return res.send(500, err);
}
var zip = zipstream.createZip({level: 1});
res.header('Content-Type', 'application/zip');
zip.pipe(res);
async.eachSeries(data.files, function (filename, cb) {
zip.addFile(fs.createReadStream(path.join(WEBCONTENT_DIR, filename)), {name: filename}, cb);
}, function () {
zip.finalize(function (written) {
console.log("sent "+written+" bytes zipped app package");
});
})
});
});
// project list
app.get('/projects/list.json', function (req, res) {
fs.readdir(PROJECT_DIR, function (err, files) {
if (err) {
console.error(err);
return res.send(500, err);
}
async.filter(files, function (file, cb) {
cb(path.extname(file) == '.xml')
}, function (results) {
res.header('Content-Type', 'application/json');
res.send(JSON.stringify(results));
});
});
});
// file list and download link for project
app.get('/projects/:project/files.json', function (req, res) {
projectFiles(req.params.project, function (err, files) {
if (err) {
console.error(err);
return res.send(500, err);
}
res.header('Content-Type', 'application/json');
res.send(JSON.stringify(files));
});
});
// project download
app.get('/projects/:project.zip', function (req, res) {
var android = req.query.android == 'true';
projectFiles(req.params.project, function (err, files) {
if (err) {
console.error(err);
return res.send(500, err);
}
var zip = zipstream.createZip({level: 1});
res.header('Content-Type', 'application/zip');
zip.pipe(res);
var zipfiles = [];
zipfiles.push(files.xml);
zipfiles.push(files.arff);
Array.prototype.push.apply(zipfiles, files.svg);
async.eachSeries(zipfiles, function (filename, cb) {
var zipfilename = filename;
var srcfile = path.join(PROJECT_DIR, filename);
if (fs.existsSync(srcfile)) {
zip.addFile(fs.createReadStream(srcfile), {name: zipfilename}, cb);
}
else {
console.log("file "+srcfile+" not found. skip and continue.");
cb();
}
}, function () {
zip.finalize(function (written) {
console.log("sent "+written+" bytes zipped project package");
});
});
});
});
app.post('/projects/:project.arff', function(req,res) {
// console.log(req.headers);
var target_path = path.join(PROJECT_DIR, req.params.project + ".arff");
console.log("Uploading: new arff file to: " + target_path);
var ws = fs.createWriteStream(target_path);
req.on('data', function(data) {
ws.write(data);
});
req.on('end', function() {
ws.end();
res.send(200, "ok");
});
});
app.post('/projects/:project.arff2', function(req, res) {
var fstream;
console.log(req);
req.pipe(req.busboy);
req.busboy.on('file', function (fieldname, file, filename) {
var target_path = path.join(PROJECT_DIR, req.params.project + ".arff");
var project_path = path.join(PROJECT_DIR, req.params.project + ".xml");
if (fs.existsSync(project_path)) {
console.log("Uploading: " + filename + " to: " + target_path);
fstream = fs.createWriteStream(target_path);
file.pipe(fstream);
fstream.on('close', function () {
res.redirect('back');
});
} else {
var msg = "Cannot upload arff because project does not exist: " + project_path;
console.log(msg);
return res.send(406, msg);
}
});
});
//file list and download link for project
app.get('/upload', function (req, res) {
var html = '<html><body><form method="post" enctype="multipart/form-data" action="/projects/minimal-data.arff">'
+'<input type="file" name="arff">'
+'<input type="submit">'
+'</form></body></html>';
res.send(html);
});
// new project
app.put('/projects/new/:project.xml', function (req, res) {
if (req.ip != '127.0.0.1') {
return res.send(403, 'access denied');
}
var stream = fs.createWriteStream(path.join(PROJECT_DIR, req.params.project+'.xml'));
req.pipe(stream);
stream.on('finish', function () {
res.send('ok');
});
stream.on('error', function () {
res.send(500, 'error');
});
});
// upload (and update)
app.put('/projects/:project/upload/:file', function (req, res) {
if (req.ip != '127.0.0.1') {
return res.send(403, 'access denied');
}
var stream = fs.createWriteStream(path.join(PROJECT_DIR, req.params.file));
req.pipe(stream);
stream.on('finish', function () {
res.send('ok');
});
stream.on('error', function () {
res.send(500, 'error');
});
});
// index
app.get('/', function (req, res) {
getServerIPs(function (err, addresses) {
if (addresses.length == 0) {
var err = "No network interfaces found!";
console.error(err);
return res.send(500, err);
}
var qrs = [];
var apps = [];
for (var i = 0; i < addresses.length; i++) {
var ip = addresses[i];
var applink = 'http://'+ip+':'+PORT+'/appupdate/get.zip'
var url = 'app,' + applink
var qr = QRCode.qrcode(4, 'L');
qr.addData(url);
qr.make();
var app = {};
app.qr = qr.createImgTag(5);
app.applink = applink;
apps.push(app);
}
res.render('index', {apps: apps});
});
});
console.log('serving at 0.0.0.0:'+PORT);
app.listen(PORT, '0.0.0.0');