-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
172 lines (128 loc) · 4.6 KB
/
routes.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
const express = require('express');
//const app = express();
const router = express.Router();
const jsonMethods = require('./js/jsonMethods');
const htmlMethods = require('./js/htmlMethods');
const qs = require('querystring');
const fileUpload = require('express-fileupload');
const bodyParser = require("body-parser");
//Middle ware that is specific to this router
router.use(function timeLog(req, res, next) {
console.log('Time: ', Date.now());
next();
});
//router.use( bodyParser.json() );
router.use( bodyParser.urlencoded({
extended: true
}));
router.use(fileUpload( { limits: { fileSize: 5 * 1e6 } } ));
//initialize the file handling routes
router.get('/', function( req, res ){ res.status( 200 ).sendFile( __dirname + '/index.html' ); } );
router.get('/loadCognition', function( req, res ) { res.send('Load cognition file!'); });
router.post('/saveCognition', function( req, res, next ) { console.log( 'Accessing the Save Cognition route...' );
next();
},
function( req, res, next ) {
let reqData = [];
let buffer;
req.on( 'error', function(){
console.log( 'error!' );
});
req.on( 'data', function( data ) {
reqData.push( data );
console.log( 'data at router.post: ', data );
});
req.on( 'end', function(){
buffer = Buffer.concat( reqData ).toString();
var jParsed = JSON.parse( buffer );
console.log( 'data parsed: ', jParsed );
jsonMethods.saveCognitionJson( jParsed.fullpath, jParsed.data );
console.log( req );
});
next();
},
function( req, res ) {
res.send('save the current cognition file!');
}
);
router.get('/listUserFiles' ,
function( req, res ) {
var userfiles = jsonMethods.listFilesInDir( (__dirname + '/userfiles'), res );
res.status( 200 ).send();
}
);
router.get('/listThemes' ,
function( req, res ) {
var themes = jsonMethods.listFilesInDir( (__dirname + '/themes'), res );
res.status( 200 ).send();
}
);
router.get('/loadTheme' , function( req, res ) { res.status( 200 ).send( 'Loading Theme File' ); });
router.post('/saveTheme' , function( req, res, next ) { console.log( 'Accessing the Save Cognition route...' );
next();
},
function( req, res, next ) {
req.on( 'data', function( data ) {
console.log( 'data at router.post: ', data );
var jParsed = JSON.parse( data );
console.log( 'data parsed: ', jParsed );
jsonMethods.saveThemeJson( jParsed.fullpath, jParsed.data );
});
next();
},
function( req, res ) {
res.send('save the current theme file!');
}
);
router.get('/loadUserImages' ,
function( req, res ) {
var media = jsonMethods.listFilesInDir( (__dirname + '/userImages'), res );
res.status( 200 ).send();
}
);
router.post('/uploadUserImages' , function( req, res, next ) {
console.log( 'Accessing the Upload User Images route...' );
next();
},
function( req, res ){
var log = {
success: [],
fail: []
};
if ( !req.files )
return res.status( 400 ).send( 'No files were uploaded.' );
// The name of the input field (i.e. "uploadFile") is used to retrieve the uploaded file
let uploadFile = req.files.hiddenUploader;
console.log( "uploadFile: ", uploadFile );
if ( uploadFile.length > 0 ){
var filesUploaded = [];
[...uploadFile].forEach( uploadImage );
}
else {
uploadImage( uploadFile );
}
function uploadImage( file ){
// Use the mv() method to place the file somewhere on your server
file.mv( (__dirname + '/userImages') + "/" + file.name, function( err ){
if ( err ){ uploadLog( file, false, err ); }
else { uploadLog( file, true ); }
});
}
function uploadLog( file, success, err ){
if ( success ){
log.success.push( file.name )
}
else {
log.fail.push( file.name );
log.fail[ lastIndex ].reason = err;
log.fail[ lastIndex ].err = "500";
}
if ( ( uploadFile.length > 0 && uploadFile.length === ( log.success.length + log.fail.length ) ) || ( !uploadFile.length ) ){
sendLog();
}
}
function sendLog(){
res.send( log );
}
});
module.exports = router;