From e6352b43e7599eca6194cb19491c9040bb3d8f15 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Tue, 31 Dec 2019 17:41:09 +0100 Subject: Store all mutable data in the same persist directory --- modules/$common.js | 1 + modules/changes/changes.js | 2 +- modules/datumpjeprik/datumpjeprik.js | 2 +- modules/lijst/lijst.js | 2 +- modules/save/save.js | 2 +- modules/todo/todo.js | 2 +- modules/zelfoverhoor/zelfoverhoor.js | 34 +++++++++++++++++++++++----------- 7 files changed, 29 insertions(+), 16 deletions(-) (limited to 'modules') diff --git a/modules/$common.js b/modules/$common.js index 3d66042..59b9a66 100644 --- a/modules/$common.js +++ b/modules/$common.js @@ -7,6 +7,7 @@ var globalAccounts=require(cwd+"/globalAccounts.json"); module.exports={ "serverdir":cwd, "webfilesdir":cwd+"/web_files", + "persistdir":cwd+"/persist", "simpleHTMLescape":function simpleHTMLescape(str){ return str.replace(/&/g,'&').replace(//g,'>'); }, diff --git a/modules/changes/changes.js b/modules/changes/changes.js index 302628b..f16c2ea 100644 --- a/modules/changes/changes.js +++ b/modules/changes/changes.js @@ -12,7 +12,7 @@ var cmn=require("../$common.js"), var moddir=null; persist=persist.create({ - dir:"persist/changes", + dir:cmn.persistdir+"/changes", continuous:false, interval:false }); diff --git a/modules/datumpjeprik/datumpjeprik.js b/modules/datumpjeprik/datumpjeprik.js index dc56759..c0c9c3d 100644 --- a/modules/datumpjeprik/datumpjeprik.js +++ b/modules/datumpjeprik/datumpjeprik.js @@ -18,7 +18,7 @@ var periodStart="2015-04-09",periodEnd="2015-07-05", periodLength=diffDays(localDateStringToDate(periodStart),localDateStringToDate(periodEnd))+1; persist=persist.create({ - dir:"persist/datumpjeprik", + dir:cmn.persistdir+"/datumpjeprik", continuous:false, interval:false }); diff --git a/modules/lijst/lijst.js b/modules/lijst/lijst.js index d30f3f4..0c5ae95 100644 --- a/modules/lijst/lijst.js +++ b/modules/lijst/lijst.js @@ -4,7 +4,7 @@ var cmn=require("../$common.js"), Naampje=require("naampje").name; persist=persist.create({ - dir:"persist/lijst", + dir:cmn.persistdir+"/lijst", continuous:false, interval:false }); diff --git a/modules/save/save.js b/modules/save/save.js index d64e66e..9873a52 100644 --- a/modules/save/save.js +++ b/modules/save/save.js @@ -26,7 +26,7 @@ function makefilename(){ module.exports=function(app,io,_moddir){ moddir=_moddir; - directory=moddir+"/saves"; + directory=cmn.persistdir+"/save"; mkdirp.sync(directory); diff --git a/modules/todo/todo.js b/modules/todo/todo.js index 7f45263..f3581f1 100644 --- a/modules/todo/todo.js +++ b/modules/todo/todo.js @@ -9,7 +9,7 @@ var cmn=require("../$common.js"), var moddir=null; persist=persist.create({ - dir:"persist/todo", + dir:cmn.persistdir+"/todo", continuous:false, interval:false }); 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); }); } -- cgit v1.2.3-70-g09d2