Index: lams_build/conf/whiteboard/scripts/s_whiteboard.js =================================================================== diff -u -rcf53a39fbd08dd3125cceba14221ac809ae6df13 -r612dedddab7ffe856fc5cb1626a0f1db55b9cf1d --- lams_build/conf/whiteboard/scripts/s_whiteboard.js (.../s_whiteboard.js) (revision cf53a39fbd08dd3125cceba14221ac809ae6df13) +++ lams_build/conf/whiteboard/scripts/s_whiteboard.js (.../s_whiteboard.js) (revision 612dedddab7ffe856fc5cb1626a0f1db55b9cf1d) @@ -4,18 +4,14 @@ var savedBoards = {}; var savedUndos = {}; -var saveDelay = false; +var saveDelay = {}; -if (config.backend.enableFileDatabase) { - //read saved boards from file - fs.readFile("savedBoards.json", (err, data) => { - if (err) { - return console.log( - "No persistend Whiteboard Datafile found... this is not a problem on the first start!" - ); - } - savedBoards = JSON.parse(data); - }); +if (config.backend.enableFileDatabase){ + // make sure that folder with saved boards exists + fs.mkdirSync("savedBoards", { + // this option also mutes an error if path exists + recursive : true + }); } module.exports = { @@ -27,12 +23,19 @@ //Clear the whiteboard delete savedBoards[wid]; delete savedUndos[wid]; + // delete the corresponding file too + fs.unlink("savedBoards/" + wid + ".json", function(err) { + if (err) { + return console.log(err); + } + }); } else if (tool === "undo") { //Undo an action if (!savedUndos[wid]) { savedUndos[wid] = []; } - if (savedBoards[wid]) { + let savedBoard = this.loadStoredData(wid); + if (savedBoard) { for (var i = savedBoards[wid].length - 1; i >= 0; i--) { if (savedBoards[wid][i]["username"] == username) { var drawId = savedBoards[wid][i]["drawId"]; @@ -56,9 +59,7 @@ if (!savedUndos[wid]) { savedUndos[wid] = []; } - if (!savedBoards[wid]) { - savedBoards[wid] = []; - } + let savedBoard = this.loadStoredData(wid); for (var i = savedUndos[wid].length - 1; i >= 0; i--) { if (savedUndos[wid][i]["username"] == username) { var drawId = savedUndos[wid][i]["drawId"]; @@ -67,7 +68,7 @@ savedUndos[wid][i]["drawId"] == drawId && savedUndos[wid][i]["username"] == username ) { - savedBoards[wid].push(savedUndos[wid][i]); + savedBoard.push(savedUndos[wid][i]); savedUndos[wid].splice(i, 1); } } @@ -92,58 +93,78 @@ "setTextboxFontColor", ].includes(tool) ) { + let savedBoard = this.loadStoredData(wid); //Save all this actions - savedBoards[wid] = savedBoards[wid] ? savedBoards[wid] : []; delete content["wid"]; //Delete id from content so we don't store it twice if (tool === "setTextboxText") { - for (var i = savedBoards[wid].length - 1; i >= 0; i--) { + for (var i = savedBoard.length - 1; i >= 0; i--) { //Remove old textbox tex -> dont store it twice if ( - savedBoards[wid][i]["t"] === "setTextboxText" && - savedBoards[wid][i]["d"][0] === content["d"][0] + savedBoard[i]["t"] === "setTextboxText" && + savedBoard[i]["d"][0] === content["d"][0] ) { - savedBoards[wid].splice(i, 1); + savedBoard.splice(i, 1); } } } - savedBoards[wid].push(content); + savedBoard.push(content); } - this.saveToDB(); + this.saveToDB(wid); }, - saveToDB : function(){ + saveToDB : function(wid){ if (config.backend.enableFileDatabase) { //Save whiteboard to file - if (!saveDelay) { - saveDelay = true; + if (!saveDelay[wid]) { + saveDelay[wid] = true; setTimeout(function () { - saveDelay = false; - fs.writeFile("savedBoards.json", JSON.stringify(savedBoards), (err) => { - if (err) { - return console.log(err); - } - }); + saveDelay[wid] = false; + if (savedBoards[wid]) { + fs.writeFile("savedBoards/" + wid + ".json", JSON.stringify(savedBoards[wid]), (err) => { + if (err) { + return console.log(err); + } + }); + } }, 1000 * 10); //Save after 10 sec } } }, + // Load saved whiteboard loadStoredData: function (wid) { - //Load saved whiteboard - return savedBoards[wid] ? savedBoards[wid] : []; + if (wid in savedBoards) { + return savedBoards[wid]; + } + + savedBoards[wid] = []; + + // try to load from DB + if (config.backend.enableFileDatabase) { + //read saved board from file + var filePath = "savedBoards/" + wid + ".json"; + if (fs.existsSync(filePath)) { + var data = fs.readFileSync(filePath); + if (data) { + savedBoards[wid] = JSON.parse(data); + } + } + } + + return savedBoards[wid]; }, copyStoredData : function(sourceWid, targetWid) { const sourceData = this.loadStoredData(sourceWid); if (sourceData.length === 0 || this.loadStoredData(targetWid).lenght > 0) { return; } savedBoards[targetWid] = sourceData.slice(); - this.saveToDB(); + this.saveToDB(targetWid); }, saveData: function(wid, data) { const existingData = this.loadStoredData(wid); - if (existingData.lenght > 0 || !data) { + if (existingData.length > 0 || !data) { return; } savedBoards[wid] = JSON.parse(data); - this.saveToDB(); + this.saveToDB(wid); } };