-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
375 lines (330 loc) · 11.6 KB
/
app.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/***********************************************************************
* VizScribe web application for protocol analysis
* Code by Senthil Chandrasegaran & Karthik Badam
* To add more time-series datasets, please see the README file.
**********************************************************************/
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var ejs = require('ejs');
var url = require('url');
var resumable = require('resumable');
var jwt = require('express-jwt');
var app = express();
var fs = require('fs'),
exec = require('child_process').exec,
util = require('util'),
admZip = require('adm-zip');
var PythonShell = require('python-shell');
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.bodyParser());
app.engine('html', require('ejs').renderFile);
/* uploaded files */
var Files = {};
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
// variables for video and transcript:
var inputvideo = { id: 'inputvideo' };
var inputtrans = { id: 'inputtrans' };
var sketchlog = { id: 'sketchlog' };
var speechlog = { id: 'speechlog' };
var activitylog = { id: 'activitylog' };
var outputvideo = { id: 'outputvideo', src: '' };
var outputtrans = { id: 'outputtrans', target: '' };
var outputlog = { id: 'outputlog', target: '' };
var outputSpeechLog = { id: 'outputSpeechLog', target: '' };
var outputActivityLog = { id: 'outputActivityLog', target: '' };
var userlog = { id: 'userlog' };
var clicklog = { id: 'clicklog' };
/* listen */
var httpserver = http.createServer(app);
httpserver.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
/* socket io */
var io = require('socket.io').listen(httpserver);
// Delete this row if you want to see debug messages
io.set('log level', 1);
/* socket.io for uploaded videos */
io.sockets.on('connection', function (socket) {
socket.on('Start', function (data) { // data contains the variables
// that we passed through in
// the html file
var Name = data['Name'];
Files[Name] = { //Create a new Entry in The Files Variable
FileSize: data['Size'],
Data: "",
Downloaded: 0
}
var Place = 0;
try {
var Stat = fs.statSync('public/video/' + Name);
if (Stat.isFile()) {
Files[Name]['Downloaded'] = Stat.size;
Place = Stat.size / 524288;
}
}
catch (er) { } //It's a New File
fs.open("public/video/" + Name, "a", 0755, function (err, fd) {
if (err) {
console.log(err);
}
else {
Files[Name]['Handler'] = fd; // We store the file
// handler so we can write
// to it later
socket.emit('MoreData', { 'Place': Place,
Percent: 0,
'Name': Name });
}
});
});
socket.on('Upload', function (data) {
var Name = data['Name'];
Files[Name]['Downloaded'] += data['Data'].length;
Files[Name]['Data'] += data['Data'];
if (Files[Name]['Downloaded'] == Files[Name]['FileSize']) {
// If File is Fully Uploaded, that is
fs.write(Files[Name]['Handler'], Files[Name]['Data'],
null, 'Binary', function (err, Writen) {
socket.emit('MoreData',
{'Place': Files[Name]['FileSize']/ 524288,
Percent: 100,
'Name': Name });
Files[Name]['Data'] = ""; //Reset The Buffer
/* check if a zip file */
if (Name.indexOf(".zip") >= 0) {
var zip = new admZip('public/video/' + Name);
zip.extractAllTo("public/images/sketches",
/*overwrite*/true);
}
socket.emit('Done', {'URL' : 'public/video/' + Name});
});
} else if (Files[Name]['Data'].length > 10485760) {
//If the Data Buffer reaches 10MB
fs.write(Files[Name]['Handler'],
Files[Name]['Data'], null,
'Binary', function (err, Writen) {
Files[Name]['Data'] = ""; //Reset The Buffer
var Place = Files[Name]['Downloaded'] / 524288;
var Percent = (Files[Name]['Downloaded'] /
Files[Name]['FileSize']) * 100;
socket.emit('MoreData',
{'Place': Place,
'Percent': Percent,
'Name': Name });
});
} else {
var Place = Files[Name]['Downloaded'] / 524288;
var Percent = (Files[Name]['Downloaded'] /
Files[Name]['FileSize']) * 100;
socket.emit('MoreData',
{'Place': Place,
'Percent': Percent,
'Name': Name });
}
});
});
/***************** UPLOADING DATA TO THE SERVER**********************/
// The following function launches the index.html page where the user
// uploads the data
// See views/index.html for the corresponding html file
app.get('/', function (req, res) {
//this '/' refers to '/index.html'
// note changing it to app.get('/index.html'... will require the
// user to include 'index.html' in the web address.
res.render('index.html', {
inputvideo: inputvideo,
inputtrans: inputtrans,
outputvideo: outputvideo,
outputtrans: outputtrans,
sketchlog: sketchlog,
speechlog: speechlog,
activitylog: activitylog,
outputlog: outputlog,
outputSpeechLog: outputSpeechLog,
outputActivityLog: outputActivityLog,
/*
* TO ADD NEW DATASET:
* Copy the block of code below,
* Uncomment it,
* Then rename all occurrences of 'newData' to a meaningful name.
*/
/*
newDataSeries: newDataSeries,
newDataSeriesOutput: newDataSeriesOutput,
*/
});
});
/****************END UPLOADING DATA TO THE SERVER**********************/
/****************HANDLE UPLOADED DATA : HOST URL***********************/
// This function renders the uploaded video
app.get('/video.html', function (req, res) {
res.render('video.html');
});
// The next few functions handle the uploaded data and create URLs for
// the client-side code to access them.
app.post('/transcript_file', function (req, res) {
var transcriptParams = req.body;
outputtrans.target = transcriptParams.transcript;
// this sets the above defined variables
res.end();
});
app.get('/receive_transcript_file', function (req, res) {
res.writeHead(200);
res.write(outputtrans.target);
res.end()
});
app.get('/log_file', function (req, res) {
var selectedURL = url.parse(req.url, true); //creates object
var logFileParams = selectedURL.query;
outputlog.target = logFileParams.logFile;
// this sets the above defined variables
res.end();
});
app.get('/receive_log_file', function (req, res) {
res.writeHead(200);
res.write(outputlog.target);
res.end()
});
app.post('/speechLog_file', function (req, res) {
var speechLogFileParams = req.body;
outputSpeechLog.target = speechLogFileParams.speechLogFile;
// this sets the above defined variables
res.end();
});
app.get('/receive_speechLog_file', function (req, res) {
res.writeHead(200);
res.write(outputSpeechLog.target);
res.end();
});
app.post('/activityLog_file', function (req, res) {
var activityLogFileParams = req.body;
outputActivityLog.target = activityLogFileParams.activityLogFile;
// this sets the above defined variables
res.end();
});
app.get('/receive_activityLog_file', function (req, res) {
res.writeHead(200);
res.write(outputActivityLog.target);
res.end()
});
app.get('/receive_video_file', function (req, res) {
res.writeHead(200);
res.write(outputvideo.src);
res.end()
});
app.get('/video_parameters', function (req, res) {
var selectedURL = url.parse(req.url, true); //creates object
var videoParams = selectedURL.query;
outputvideo.src = videoParams.videoURL;
res.end();
});
/*
* TO ADD NEW DATASET:
* Copy the block of code below and uncomment.
*/
/*
app.post('/newDataSeries_file', function (req, res) {
var newDataSeriesFileParams = req.body;
newDataSeriesOutput.target = newDataSeriesFileParams.newDataSeriesFile;
res.end();
});
app.get('/receive_newDataSeries_file', function (req, res) {
res.writeHead(200);
res.write(newDataSeriesOutput.target);
res.end()
});
*/
/**********END OF HANDLING UPLOADED DATA : HOST URL********************/
/************************DATA ON CLIENT PAGE***************************/
// This function handles the main.html file, which is the VizScribe
// interface. See views/main.html for the corresponding file
app.get('/main', function (req, res) {
console.log('video ' + outputvideo.src);
res.render('main.html', {
inputvideo: inputvideo,
inputtrans: inputtrans,
outputvideo: outputvideo,
outputtrans: outputtrans,
sketchlog: sketchlog,
speechlog: speechlog,
activitylog: activitylog,
outputlog: outputlog,
outputSpeechLog: outputSpeechLog,
outputActivityLog: outputActivityLog,
/*
* TO ADD NEW DATASET:
* Copy-Paste the block of code below,
* Uncomment it,
* Then rename all occurrences of 'newDataSeries'
* to a more meaningful name.
*/
/*
newDataSeries: newDataSeries,
newDataSeriesOutput: newDataSeriesOutput,
*/
});
});
/**********************END OF DATA ON CLIENT PAGE**********************/
// LOGGING USER ACTIVITY (FOR USER STUDIES)
// records a log of user activity
app.post('/userlog', function (req, res){
// write user log file as text
fs.writeFile('public/userlog/userlog.csv', String(req.body.data),
function (err) {
if (err) throw err;
console.log('userlog.csv was written');
res.send(200);
});
});
app.post('/clicklog', function (req, res){
//res.send(req.body);
// write user log file as text
fs.writeFile('public/clicklog/clicklog.csv', String(req.body.data),
function (err) {
if (err) throw err;
console.log('clicklog.csv was written');
res.send(200);
});
});
// EXPERIMENTAL: USE PYTHON NLP FOR SERVER-SIDE TEXT PROCESSING
var options = {
mode: 'text',
pythonOptions: ['-u'],
scriptPath: './public/pythonscripts/',
};
// This function works, but needs better data handling, and as such is
// not called right now. The idea is to scale or sort the word cloud
// based on the information content of each word.
app.post('/infoContent', function (req, res){
// invoke this just once, and send all the data over to the client.
// This will make the code more responsive.
var pyShell = new PythonShell('infoContent.py', options)
pyShell.send(req.body.data);
pyShell.on('message', function(message){
res.send(200, {data: message});
});
pyShell.end(function(err){
if (err) throw err;
});
});