-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
executable file
·191 lines (148 loc) · 5.41 KB
/
server.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
#!/usr/bin/env node
/**
* Provided under the MIT License (c) 2014
* See LICENSE @file for details.
*
* @file server.js
*
* @author juanvallejo
* @date 4/12/16
*
* Scanner application 'server'. Handles all data processing and i/o.
* Reads data from a local mysql database, builds an internal structure
* with it, and allows for easy manipulation of it. Outputs to .xlsx file.
*/
// define runtime consts
var SERVER_HOST = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
var SERVER_PORT = process.env.OPENSHIFT_NODEJS_PORT || 8000;
var fs = require('fs');
var http = require('http');
var io = require('socket.io');
var Multiparty = require('multiparty');
var Jsdom = require('node-jsdom');
var typeDefs = {
'css' : 'text/css' ,
'csv' : 'text/csv' ,
'html' : 'text/html' ,
'ico' : 'image/x-icon' ,
'jpg' : 'image/jpeg' ,
'jpeg' : 'image/jpeg' ,
'js' : 'application/javascript' ,
'map' : 'application/x-navimap' ,
'pdf' : 'application/pdf' ,
'png' : 'image/png' ,
'ttf' : 'application/octet-stream',
'txt' : 'text/plain' ,
'woff' : 'application/x-font-woff'
}
var app = http.createServer(function(req, res) {
if(req.url == '/' || req.url == '/index.html') {
return fs.readFile(__dirname + '/index.html', function(err, data) {
if(err) {
console.log('Error reading requested path', req.url);
return res.end(data);
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(data);
});
}
if(req.url == '/upload' && req.method == 'POST') {
var form = new Multiparty.Form();
return form.parse(req, function(err, fields, files) {
if(err) {
console.log('Content type =', req.headers);
console.log('ERR MULTIPARTY', err);
return res.end('err_upload');
}
if(!files.excel) {
console.log('Unexpected fileformname from client.');
return res.end('err_filename');
}
fs.readFile(files.excel[0].path, function(err, data) {
if(err) {
console.log('ERR MULTIPART', err);
return res.end('err_parse');
}
var doc = data.toString().split('<body>');
if(!doc.length || !doc[1]) {
return res.end('err_doctype');
}
doc = doc[1].split('</body>')[0];
if(!doc) {
return res.end('err_doctype');
}
parseDocData(doc, function(data) {
res.end(JSON.stringify(data));
});
});
});
}
fs.readFile(__dirname + req.url, function(err, data) {
if(err) {
res.writeHead(404);
return res.end('404. Page not found.');
}
var ext = req.url.split('.');
ext = ext[ext.length - 1];
res.writeHead(200, {'Content-Type': (typeDefs[ext] || 'text/plain')});
res.end(data);
});
});
app.listen(SERVER_PORT, SERVER_HOST);
io.listen(app).on('connection', function(client) {
console.log('Client', client.id, 'has connected.');
// emit id to client
client.emit('id', {body: client.id});
});
function parseDocData(html, callback) {
var response = {};
try {
var document = Jsdom.jsdom(html);
var table = document.getElementsByTagName('table').item(0);
var cols = table.getElementsByTagName('tr');
var validCols = [];
var validCountCols = [];
var validRows = [];
var totalHourCount = 0;
var totalSemesterHourCount = 0;
var totalYearHourCount = 0;
var beginYearDate = new Date('2015,8,18');
var beginSemDate = new Date('2016,1,1');
for(var i = 1; i < cols.length; i++) {
var row = cols.item(i).getElementsByTagName('td');
var dateString = row.item(14).innerHTML;
var dateStringArr = dateString.split('/');
var rowDate = new Date(dateStringArr[2] + ',' + dateStringArr[1] + ',' + dateStringArr[0]);
totalHourCount += parseFloat(row.item(13).innerHTML);
validRows.push(cols.item(i));
validCountCols.push(cols.item(i).getElementsByTagName('td').item(13))
validCols.push({
name: row.item(1).innerHTML,
date: rowDate
});
if(rowDate.getTime() >= beginYearDate.getTime()) {
totalYearHourCount += parseFloat(row.item(13).innerHTML);
}
if(rowDate.getTime() >= beginSemDate.getTime()) {
totalSemesterHourCount += parseFloat(row.item(13).innerHTML);
}
}
response = {
error: false,
message: '',
name: (validCols[0].name || ''),
hours: {
semester: totalSemesterHourCount,
year: totalYearHourCount,
total: totalHourCount
}
};
} catch(e) {
console.log('JSDOM PARSE', e);
response = {
error: true,
message: e.toString()
}
}
callback.call(this, response);
}