diff --git a/.gitignore b/.gitignore index 86297eb..e8689ea 100644 --- a/.gitignore +++ b/.gitignore @@ -66,3 +66,4 @@ typings/ .env /public/apidoc +/savedBoards.json diff --git a/README.md b/README.md index e08c190..fe058af 100644 --- a/README.md +++ b/README.md @@ -165,7 +165,7 @@ Many more settings can be tweaked. All of them are described in the [default con ## Things you may want to know -- Whiteboards are gone if you restart the Server, so keep that in mind (or save your whiteboard) +- Whiteboards are gone if you restart the Server enable "enableFileDatabase" in the config file or export the board to prevent that. - You should be able to customize the layout without ever touching the whiteboard.js (take a look at index.html & main.js) ## ToDo diff --git a/config.default.yml b/config.default.yml index da713fe..0f55026 100644 --- a/config.default.yml +++ b/config.default.yml @@ -6,6 +6,9 @@ backend: # Enable the function to save to a webdav-server (check README for more info) -- boolean enableWebdav: false + # Enable the function to save the whiteboard to a file so you save the state even on server restarts -- boolean + enableFileDatabase: true + # Backend performance tweaks performance: # Whiteboard information broadcasting frequency (in Hz i.e. /s) -- number diff --git a/scripts/config/config-schema.json b/scripts/config/config-schema.json index 4f04b49..52a88c9 100644 --- a/scripts/config/config-schema.json +++ b/scripts/config/config-schema.json @@ -14,6 +14,9 @@ "enableWebdav": { "type": "boolean" }, + "enableFileDatabase": { + "type": "boolean" + }, "performance": { "additionalProperties": false, "type": "object", diff --git a/scripts/s_whiteboard.js b/scripts/s_whiteboard.js index 4299785..52540ed 100644 --- a/scripts/s_whiteboard.js +++ b/scripts/s_whiteboard.js @@ -1,7 +1,23 @@ -//This file is only for saving the whiteboard. (Not to a file, only to RAM atm. Whiteboard is gone after server restart) +//This file is only for saving the whiteboard. +const fs = require("fs"); +const config = require("./config/config"); var savedBoards = {}; var savedUndos = {}; +var saveDelay = false; + +if (config.backend.enableFileDatabase) { + //read saved boards from file + fs.readFile("savedBoards.json", (err, data) => { + if (err) { + return console.log( + "Not persistend Whiteboard Datafile found... this is not a problem on the first start!" + ); + } + savedBoards = JSON.parse(data); + }); +} + module.exports = { handleEventsAndData: function (content) { var tool = content["t"]; //Tool witch is used @@ -77,9 +93,7 @@ module.exports = { ].includes(tool) ) { //Save all this actions - if (!savedBoards[wid]) { - savedBoards[wid] = []; - } + 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--) { @@ -94,6 +108,21 @@ module.exports = { } savedBoards[wid].push(content); } + + if (config.backend.enableFileDatabase) { + //Save whiteboard to file + if (!saveDelay) { + saveDelay = true; + setTimeout(function () { + saveDelay = false; + fs.writeFile("savedBoards.json", JSON.stringify(savedBoards), (err) => { + if (err) { + return console.log(err); + } + }); + }, 1000 * 10); //Save after 10 sec + } + } }, loadStoredData: function (wid) { //Load saved whiteboard