summaryrefslogtreecommitdiff
path: root/modules/zelfoverhoor
diff options
context:
space:
mode:
Diffstat (limited to 'modules/zelfoverhoor')
-rw-r--r--modules/zelfoverhoor/zelfoverhoor.js34
1 files changed, 23 insertions, 11 deletions
diff --git a/modules/zelfoverhoor/zelfoverhoor.js b/modules/zelfoverhoor/zelfoverhoor.js
index 08431d1..03de63c 100644
--- a/modules/zelfoverhoor/zelfoverhoor.js
+++ b/modules/zelfoverhoor/zelfoverhoor.js
@@ -1,7 +1,10 @@
var cmn=require("../$common.js"),
- fs=require("fs");
+ fs=require("fs"),
+ mkdirp=require("mkdirp");
var moddir;
+var dbdir=cmn.persistdir+"/zelfoverhoor";
+mkdirp.sync(dbdir);
var accounts=require("./accounts.json");
@@ -28,21 +31,21 @@ var persistDB=(function(){
var s=JSON.stringify(questiondb);
if(s!=qdbCache){
qdbCache=s;
- fs.writeFile(moddir+"/questiondb.json",JSON.stringify(questiondb),function(err){
+ fs.writeFile(dbdir+"/questiondb.json",s,function(err){
if(err)console.log("write questiondb.json:",err);
});
}
s=JSON.stringify(questionsets);
if(s!=qsetsCache){
qsetsCache=s;
- fs.writeFile(moddir+"/questionsets.json",JSON.stringify(questionsets),function(err){
+ fs.writeFile(dbdir+"/questionsets.json",s,function(err){
if(err)console.log("write questionsets.json:",err);
});
}
s=JSON.stringify(userlists);
if(s!=ulCache){
ulCache=s;
- fs.writeFile(moddir+"/userlists.json",JSON.stringify(userlists),function(err){
+ fs.writeFile(dbdir+"/userlists.json",s,function(err){
if(err)console.log("write userlists.json:",err);
});
}
@@ -82,15 +85,24 @@ function checkQsetJson(json,errfunc){
return qset;
}
+function canAccessFile(fname){
+ try {
+ fs.accessSync(fname,fs.constants.F_OK|fs.constants.R_OK|fs.constants.W_OK);
+ return true;
+ } catch(e) {
+ return false;
+ }
+}
+
module.exports=function(app,io,_moddir){
moddir=_moddir;
- try {
- questiondb=JSON.parse(fs.readFileSync(moddir+"/questiondb.json"));
- questionsets=JSON.parse(fs.readFileSync(moddir+"/questionsets.json"));
- userlists=JSON.parse(fs.readFileSync(moddir+"/userlists.json"));
- } catch(e){
- console.log("error on reading zelfoverhoor db");
+ if(canAccessFile(dbdir+"/questiondb.json")&&canAccessFile(dbdir+"/questionsets.json")&&canAccessFile(dbdir+"/userlists.json")){
+ questiondb=JSON.parse(fs.readFileSync(dbdir+"/questiondb.json"));
+ questionsets=JSON.parse(fs.readFileSync(dbdir+"/questionsets.json"));
+ userlists=JSON.parse(fs.readFileSync(dbdir+"/userlists.json"));
+ } else {
+ console.log("newly creating zelfoverhoor db");
questiondb={};
questionsets={};
userlists={};
@@ -229,7 +241,7 @@ module.exports=function(app,io,_moddir){
app.post("/zelfoverhoor/error",function(req,res){
if(typeof req.body=="string"){
- fs.appendFile(moddir+"/errors.txt",new Date().toString()+" "+req.body+"\n",function(err){
+ fs.appendFile(dbdir+"/errors.txt",new Date().toString()+" "+req.body+"\n",function(err){
if(err)console.log(err);
});
}