-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupload.js
292 lines (272 loc) · 11.2 KB
/
upload.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
//引入express模块
var express = require('express');
//引入multer模块
var multer = require ('multer');
var path = require('path');
var fs = require('fs');
//引入child_process
var cp=require('child_process');
var geoip = require('geoip-country');
var ip = "118.195.210.89";
var geo = geoip.lookup(ip);
var upload = multer({ dest: path.join(__dirname,'upload-single')});
var app = express();
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'Gongclub123',
database : 'HCP_CC'
});
connection.connect();
app.use(express.static(path.join(__dirname, './')));
app.use(express.json());
app.use(express.text());
app.use(express.urlencoded({ extended: true }));
function getClientIp(req) {
return req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
};
app.get('/download/*', (req, res) => {
console.log('download test');
console.log(getClientIp(req));
console.log(req.url)
var url=req.url;
var arr=url.split('/');
var b = arr.splice(2);
var file=b.join('/');
if (typeof(file) !== undefined){
console.log(file);
var path = __dirname + '/'+file;
console.log(path);
var size = fs.statSync(path).size;
var f = fs.createReadStream(path);
res.writeHead(200,{
'Content-Type':'application/force-download',
'Content-Disposition':'attachment;filename='+file,
'Content-Length':size
})
f.pipe(res);
}
})
app.post('/multiSurface',function(req,res){
let parcellation=req.body.parcellation;
let parcelID=req.body.parcelID;
console.log(parcellation);
console.log(parcelID);
var r = Math.random().toString(36).substr(2, 3) + "-" + Math.random().toString(36).substr(2, 3) + "-" + Math.random().toString(36).substr(2, 4);
console.log('python ' + path.join(__dirname, 'py/multiSurfResult.py') + ' ' + parcellation+' '+parcelID+' '+r.toUpperCase()+' '+__dirname)
cp.exec('python ' + path.join(__dirname, 'py/multiSurfResult.py') + ' ' + parcellation+' '+parcelID+' '+r.toUpperCase()+' '+__dirname, (err, stdout, stderr) => {
if (err) console.log('stderr', err);
if (stdout) {
console.log('stdout', stdout);
if (stderr) console.log('stderr', stderr);
res.json({
path: r.toUpperCase(),
std: stdout,
err: err,
stderr: stderr
})
}
res.end();
});
})
app.get('/threshold',function(req,res) {
console.log(req.query.type);
console.log(req.query.name);
connection.query("SELECT value from HCP_CC."+req.query.type+" where name='"+req.query.name+"';",function (error,results){
if (error) throw error;
if (results){
let dataString = JSON.stringify(results);
let data = JSON.parse(dataString);
res.json({
value:data[0].value,
});
}
res.end();
})
})
app.get('/parcelmeta',function(req,res){
console.log(req.query.template);
//console.log("SELECT zscore,behavior,pname from HCP_CC.meta_cognition where template='"+req.query.template+"' and pname in ("+req.query.roi1+","+req.query.roi2+") order by pname,zscore desc;")
connection.query("SELECT zscore,behavior,pname from HCP_CC.meta_cognition where template='"+req.query.template+"' and pname in ("+req.query.roi1+","+req.query.roi2+") order by pname,zscore;", function (error, results, fields) {
if (error) throw error;
if (results){
let dataString = JSON.stringify(results);
let data = JSON.parse(dataString);
//console.log('The solution is: ', data);
let zscore=[],behavior=[];
for(i=0;i<data.length;i++){
zscore.push(data[i].zscore);
behavior.push(data[i].behavior);
}
res.json({
zscore:zscore,
behavior:behavior,
});
}
res.end();
});
});
app.post('/upload', upload.single('nifti1-file'), function (req, res) {
//TODO:need consider .nii.gz
console.log(req.file);
console.log(req.body);
var file = req.file;
if (!req.file) {
res.json({
path: '',
std: '',
err: 'ERR: Plz choose a file to upload!',
stderr: 'ERR: Plz choose a file to upload!'
})
res.end();
} else{
res.set({
'content-type': 'application/json; charset=utf-8'
});
console.log("Request handler 'upload' was called.");
if (path.extname(file.originalname) === '.nii') {
var r = Math.random().toString(36).substr(2, 3) + "-" + Math.random().toString(36).substr(2, 3) + "-" + Math.random().toString(36).substr(2, 4);
var upload_file = path.join(__dirname, "/upload-single/" + r.toUpperCase() + path.extname(file.originalname));
fs.rename(file.path, upload_file, function () {
var arg1 = upload_file;
var arg2 = path.join(__dirname, '/save-file/', r.toUpperCase());
var arg3 = path.join(__dirname, '/models');
console.log('python ' + path.join(__dirname, 'py/genResult.py') + ' ' + arg1 + ' ' + arg2 + ' ' + arg3);
cp.exec('python ' + path.join(__dirname, 'py/genResult.py') + ' ' + arg1 + ' ' + arg2 + ' ' + arg3, (err, stdout, stderr) => {
let std;
if (err) console.log('stderr', err);
if (stdout) {
console.log('stdout', stdout);
std = stdout;
}
if (stderr) console.log('stderr', stderr);
res.json({name: file.originalname, path: r.toUpperCase(), std: std, err: err, stderr: stderr})
res.end();
});
});
} else {
res.json({
name: file.originalname,
path: '',
std: '',
err: 'ERR: You should upload a nifti-file!',
stderr: 'ERR: You should upload a nifti-file!'
})
res.end();
}
}
});
app.post('/uploadSurface', upload.single('gifti1-file'), function (req, res) {
//TODO:need consider .nii.gz
console.log('upload');
console.log(req.file);
var file = req.file;
var index = req.body.opt;
if (!req.file) {
res.json({
path: '',
std: '',
err: 'ERR: Plz choose a file to upload!',
stderr: 'ERR: Plz choose a file to upload!'
})
res.end();
} else {
res.set({
'content-type': 'application/json; charset=utf-8'
});
console.log("Request handler 'upload' was called.");
if (path.extname(file.originalname) === '.gii') {
var r = Math.random().toString(36).substr(2, 3) + "-" + Math.random().toString(36).substr(2, 3) + "-" + Math.random().toString(36).substr(2, 4);
var upload_file = path.join(__dirname, "/upload-single/" + r.toUpperCase() + '.tex.gii');
fs.rename(file.path, upload_file, function () {
var arg1 = upload_file;
var arg2 = index;
console.log('python ' + path.join(__dirname, 'py/genSurfResult.py') + ' ' + arg1 + ' ' + arg2+ ' '+__dirname)
cp.exec('python ' + path.join(__dirname, 'py/genSurfResult.py') + ' ' + arg1 + ' ' + arg2 + ' '+__dirname, (err, stdout, stderr) => {
let std;
console.log('err'+err)
console.log('stdout'+stdout)
console.log('stderr'+stderr)
if (stdout) {
cp.exec('ls ' + path.join(__dirname, 'save-file/' + r.toUpperCase() + '/json/*.nii'), (err0, stdout0, stderr0) => {
console.log(err0)
if (err0 || stderr0) {
res.json({
name: file.originalname,
path: '',
flag:[],
std: '',
err: 'ERR: There\'s something wrong with your mask! ',
stderr: 'ERR: There\'s something wrong with your mask! Nothing generated! '
})
}
else if (stdout0) {
console.log(stdout0.split("\n"));
std=stdout0.split("\n");
var resultno=stdout0.split("\n").length;
var result_arr=[];
for (i = 0; i < resultno; i++){
let fname = std[i].split("/").pop().split(".");
result_arr.push(fname[0]+'.'+fname[1]+'.'+fname[2])
}
console.log(stdout0)
if (stdout0.length === 0) {
res.json({
name: file.originalname,
path: '',
flag:[],
std: '',
err: 'ERR: There\'s something wrong with your mask! ',
stderr: 'ERR: There\'s something wrong with your mask! '
});
res.end();
} else if (stdout0.length > 0) {
console.log('stderr', stderr);
res.json({
name: file.originalname,
path: r.toUpperCase(),
flag: result_arr,
std: std,
err: err,
stderr: stderr
})
res.end();
}
}
})
}
});
});
} else {
res.end();
}
}
});
//app.listen(5700)
app.listen(8080,'0.0.0.0',() => {
// client.country('142.1.1.1').then(response => {
// console.log(response);
// console.log(response.country.isoCode); // 'CA'
// });
console.log(`Example app listening on port 8080`)
});
app.get('/', function(req,res){
console.log('test_get');
console.log(geo.country);
console.log(getClientIp(req));
if (getClientIp(req)=='CN'){
console.log('yes');
res.redirect('http://ccmapping.cn/');
}else{
res.redirect('http://ccmapping.org/');
}
//res.sendFile(path.join(__dirname,'./CCmapping_index.html'));
});
// app.get('/', function(req, res){
// res.set('Content-Type', 'text/plain');
// res.send('hello world');
// });