-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
131 lines (111 loc) · 3.36 KB
/
main.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
'use strict';
const express = require('express'),
app = express(),
bodyParser = require('body-parser'),
formidable = require('formidable'),
path = require('path'),
fs = require('fs'),
spawn = require('child_process').spawn;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true,
}));
app.use('/favicon.ico', express.static('./public/favicon.ico'));
// Setup our static resources folder
app.use(express.static('public'));
// Setup Pug
app.set('view engine', 'pug');
app.set('views', './views');
// Setup views
app.get('/', function (req, res) {
res.render('home');
});
app.get('/aboutus', function (req, res) {
res.render('aboutus');
});
app.post('/getmatch', function (req, res) {
// set up recieving file
let form = new formidable.IncomingForm();
form.multiples = false;
form.uploadDir = path.join(__dirname, '/temp');
let filepath,
oldFilePath,
filename,
timestamp;
// parse the incoming file
form.parse(req, (err, fields, files) => {
timestamp = fields.timestamp;
});
// save the file as its name in the /temp folder
form.on('file', (field, file) => {
oldFilePath = file.path;
filename = file.name;
});
// if something wonky happens
form.on('error', function (err) {
console.log('An error has occured: \n' + err);
});
// once its finished saving, do the business. This is where the magic comparing happens
form.on('end', () => {
// path to the file including the timestamp. etc: c:/something/somethingelse/90-40-3_a_123456789.pdf
filepath = path.join(form.uploadDir, filename.substring(0, filename.length - 4) + '_' + timestamp + filename.substring(filename.length - 4));
fs.rename(oldFilePath, filepath, err => {
if (err) {
console.log(err);
}
runJoshsPython(filepath, res, filename.substring(0, filename.length - 4) + '_' + timestamp + '.jpg');
});
});
});
// Start the server and have it listen on a port
const server = app.listen(3000, function () {
console.log(`Server started on port ${server.address().port}!`);
});
// this function runs the python programs
let runJoshsPython = function (pathToPDF, res, filename) {
// run 'Compare to Query.py'
const comparePy = spawn('python', ['Query.py', pathToPDF, filename]);
// if something wonky happens let me know
comparePy.on('error', err => {
console.log('error:', err);
});
// if the program has an exception let me know
comparePy.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
});
// once the program gives any output
comparePy.stdout.on('data', chunk => {
// format output
let textchunk = chunk.toString('utf8');
// send results back to browser
returnResults(textchunk, res);
});
};
// this function returns the results back to the browser
let returnResults = function (winners, res) {
// split string of winners into array
winners = winners.split(' ');
// send back JSON of top 20 results
res.send(JSON.stringify({
1: winners[0],
2: winners[1],
3: winners[2],
4: winners[3],
5: winners[4],
6: winners[5],
7: winners[6],
8: winners[7],
9: winners[8],
10: winners[9],
11: winners[10],
12: winners[11],
13: winners[12],
14: winners[13],
15: winners[14],
16: winners[15],
17: winners[16],
18: winners[17],
19: winners[18],
20: winners[19],
}));
};