-
Notifications
You must be signed in to change notification settings - Fork 0
/
saveload.js
59 lines (57 loc) · 2.21 KB
/
saveload.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
let path = require("path");
let fs = require("fs");
module.exports = {
prepare: (app, private) => {
app.post("/saveme", (req, res) => {
let safeF = String(req.query.f).replace(/\./g, "_");
try {
fs.mkdirSync(private.baseLogLocation + "/" + safeF);
} catch (e) {
//directory exists, ignore
}
let file = private.baseLogLocation + `/${safeF}/${safeF}_${Date.now()}.json`;
fs.writeFile(file, JSON.stringify(req.body), (e) => {
console.log(e || file);
res.sendStatus(200);
res.end();
});
});
app.get("/loadme", (req, res) => {
let saveF = String(req.query.f).replace(/\./g, "_");
fs.readdir(private.baseLogLocation + "/" + saveF, (err, files) => {
if (err || files.length == 0) {
res.send("");
res.end();
console.log(err);
} else {
latestTime = 0;
files.forEach(i => {
console.log(i);
let lastTimeRe = /.+?(\d+)\.json/.exec(i);
if (lastTimeRe) {
let lastTime = Number(lastTimeRe[1]);
if (lastTime > latestTime) {
latestTime = lastTime;
}
}
});
if (latestTime != 0) {
fs.readFile(path.join(private.baseLogLocation + "/" + saveF, saveF + "_" + latestTime + ".json"), (err, data) => {
if (err) {
res.send("");
console.log(err);
} else {
res.send(String(data));
console.log(latestTime);
}
res.end();
});
} else {
res.send("");
res.end();
}
}
})
});
}
}