-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
68 lines (62 loc) · 1.93 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
const express = require("express");
const path = require("path");
const app = express();
const fileUpload = require("express-fileupload");
const exec = require("child_process").execSync;
const fs = require("fs");
app.use(express.static(path.join(__dirname, "build")));
app.use(fileUpload());
//upload enpoint
app.post("/upload/:id", async (req, res) => {
if (req.files === null || req.files === undefined) {
return res.status(400).json({ msg: "No file uploaded." });
}
const file = req.files.file;
const id = req.params.id.substr(1);
const dir = `${__dirname}/public/uploads/${id}/`;
if (!fs.existsSync(dir)) {
// checks to see if directory has already been made
fs.mkdirSync(dir);
}
file.mv(`${dir}/${file.name}`, (err) => {
if (err) {
console.error(err);
return res.status(500).send(err);
}
res.json({ fileName: file.name, filePath: `/uploads/${file.name}` });
});
});
// analysis endpoint
app.get("/analyze/:id", async (req, res) => {
if (!req.params.id) {
return res.status(400).json({ msg: "No ID provided" });
}
const id = req.params.id.substr(1);
const dir = `${__dirname}/public/uploads/${id}/`;
try {
exec(
`cd ${dir}; ../slippi-set-stats-linux .; cp ../parse.js .; node parse.js; rm *.slp parse.js output.json;`
);
} catch (err) {
console.error(err);
return res.status(500).send(err);
}
//console.log("made it");
res.sendFile(`${dir}/simpleOutput.txt`);
});
app.get("/simple/:id", async (req, res) => {
if (!req.params.id) {
return res.status(400).json({ msg: "No ID provided" });
}
const id = req.params.id.substr(1);
const dir = `${__dirname}/public/uploads/${id}/`;
if (fs.existsSync(`${dir}/simpleOutput.txt`)) {
res.sendFile(`${dir}/simpleOutput.txt`);
} else {
res.status(500).send({ err: "Was not created" });
}
});
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
app.listen(5000);