This repository was archived by the owner on Jun 12, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
93 lines (78 loc) · 2.67 KB
/
Copy pathserver.js
File metadata and controls
93 lines (78 loc) · 2.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
const idx = require('idx');
const path = require('path');
const uuidv4 = require('uuid/v4');
const utils = require('./src/utils');
const answers = require('./src/answers');
const trainingPhrases = require('./src/trainingPhrases');
const express = require('express');
const nunjucks = require('express-nunjucks');
const multer = require('multer');
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, utils.AGENT_UPLOAD_PATH.join(path.sep));
},
filename: function(req, file, cb) {
cb(null, `${file.originalname.split('.')[0]}--${Date.now()}.zip`);
},
});
const upload = multer({storage});
const DELETE_ALL_UUID = !!process.env.DELETE_ALL_UUID ?
process.env.DELETE_ALL_UUID : uuidv4();
const app = express();
nunjucks(app, {});
app.set('views', __dirname + '/views');
// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
// http://expressjs.com/en/starter/basic-routing.html
app.get('/', function(request, response) {
response.render('index');
});
app.post('/extract', upload.single('agentZipFile'), (request, response) => {
const currentPath = idx(request, (_) => _.file.path);
if (currentPath === undefined) {
response.redirect(302, '/');
return;
}
utils.unzipFile(currentPath).then((unzipPath) => {
const agentName = path.basename(currentPath, '.zip');
Promise.all([
answers.extract(agentName, unzipPath, utils.AGENT_CSV_PATH),
trainingPhrases.extract(agentName, unzipPath, utils.AGENT_CSV_PATH),
]).then(([answersFilename, trainingPhrasesFilename]) => {
answersFilename = answersFilename.replace('./public', '');
trainingPhrasesFilename = trainingPhrasesFilename.replace('./public', '');
const filePrefix = agentName;
response.render('extracted', {
answersFilename,
trainingPhrasesFilename,
filePrefix,
});
});
}).catch((err) => {
console.error(err);
response.redirect(302, '/');
});
});
app.get(`/deleteAll/${DELETE_ALL_UUID}`, (request, response) => {
utils.deleteEverything()
.then((result) => {
response.json(result);
}).catch((error) => {
response.json(error);
});
});
app.get('/delete/:filePrefix', (request, response) => {
const filesThatStartsWith = (f) => {
return f.startsWith(request.params.filePrefix) === true;
};
utils.deleteEverything(filesThatStartsWith)
.then((result) => {
response.render('deleted');
}).catch((error) => {
response.json(error);
});
});
// listen for requests :)
const listener = app.listen(process.env.PORT || 3000, function() {
console.log('Your app is listening on port ' + listener.address().port);
});