-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
101 lines (86 loc) · 2.83 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
const express = require('express');
const fileUpload = require('express-fileupload');
const { exec, spawn } = require('child_process');
const app = express();
const port = process.env.PORT || 3000;
// Set public folder as root
app.use(express.static('public'));
// Allow the app to upload files and let the server use the conversion directory
// app.use(fileUpload());
app.use(fileUpload({
limits: {
fileSize: 2 * 1024 * 1024 * 1024 * 1024 * 1024
},
}));
app.use(express.static('conversion'));
//handles file uploading
app.post('/', function(req, res) {
if(req.files.imgUpload){
let thisFile;
for (var key in req.files) {
thisFile = req.files[key];
console.log(req.files[key].name);
thisFile.mv('public/images/' + req.files[key].name, function(err) {
if (err) {
console.log(err);
}
else {
console.log('File uploaded!');
}
});
}
}
else if(req.files.geoUpload){
var file = req.files.geoUpload;
var filename = "uploaded.geojson";
file.mv('public/assets/mygeodata/upload/'+ filename, function (err) {
if (err) res.send(err);
else {
console.log(file.name + " uploaded successfully");
res.sendFile(`${__dirname}/public/upload.html`);
}
});
}
else if(req.files.pcUpload){
var file = req.files.pcUpload;
var filename = file.name;
let output;
if (filename.charAt(filename.length-1)=="t") output="output.txt";
else if(filename.charAt(filename.length-1)=="z") output="output.laz";
else output="output.las"
file.mv('conversion/files/'+ output, function (err) {
if (err) res.send(err);
else {
console.log("\r\n" + filename + " uploaded successfully" + "\r\n");
console.log("converting cloud..." + "\r\n");
// exec('conversion.bat', (err, stdout) => {
// if (err) {
// console.error(err);
// return;
// }
// console.log(stdout);
// res.sendFile(`${__dirname}/public/upload.html`);
// });
const bat = spawn('cmd.exe', ['/c', 'conversion.bat']);
bat.stdout.on('data', (data) => {
console.log(data.toString());
});
bat.stderr.on('data', (data) => {
console.error(data.toString());
});
bat.on('exit', (code) => {
console.log(`Child exited with code ${code}`);
res.sendFile(`${__dirname}/public/upload.html`);
});
}
});
}
});
// Allow front-end access to node_modules folder
app.use('/scripts', express.static(`${__dirname}/node_modules/`));
// Redirect all traffic to index.html
app.use((req, res) => res.sendFile(`${__dirname}/public/index.html`));
// Listen for HTTP requests on port 3000
app.listen(port, () => {
console.log('listening on %d', port);
});