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
|
var cmn=require("../$common.js"),
fs=require("fs"),
mkdirp=require("mkdirp");
var moddir;
var directory;
var filenameRegex=/[^a-zA-Z0-9._-]/g;
function pad(s,n){
s=s+"";
var p="";
while(s.length+p.length<n)p+="0";
return p+s;
}
function makefilename(){
var d=new Date();
var s=[
pad(d.getFullYear(),4), pad(d.getMonth()+1,2), pad(d.getDate(),2),
pad(d.getHours(),2), pad(d.getMinutes(),2), pad(d.getSeconds(),2),
pad(d.getMilliseconds(),3)
].join("-");
return s+".txt";
}
module.exports=function(app,io,_moddir){
moddir=_moddir;
directory=moddir+"/saves";
mkdirp.sync(directory);
app.get("/save",function(req,res){
res.sendFile(moddir+"/index.html");
});
app.post("/save",function(req,res){
var s=req.body.trim();
if(s=="")res.status(400).send("No text sent");
fs.writeFile(directory+"/"+makefilename(),s,function(err){
if(err){
console.log(err);
res.status(500).send("An error occurred");
return;
}
res.send();
});
});
app.all(["/save/read","/save/read/*"],cmn.authgen());
app.get("/save/read",function(req,res){
res.sendFile(moddir+"/read.html");
});
app.get("/save/read/files",function(req,res){
fs.readdir(directory,function(err,files){
if(err){
console.log(err);
res.status(500).send(err);
return;
}
res.send(JSON.stringify(files));
});
});
app.get("/save/read/file/:fname",function(req,res){
var fname=req.params.fname.replace(filenameRegex,"");
fs.readFile(directory+"/"+fname,function(err,text){
if(err){
res.status(404).send("File not found");
return;
}
res.send(text);
});
});
app.delete("/save/read/file/:fname",function(req,res){
var fname=req.params.fname.replace(filenameRegex,"");
fs.unlink(directory+"/"+fname,function(err){
if(err){
res.status(404).send("File not found");
return;
}
res.send();
});
});
};
|