add function to save the whiteboard to a file

This commit is contained in:
raphael 2021-03-07 16:54:54 +01:00
parent e516a24426
commit 1c23b8fc6a
5 changed files with 41 additions and 5 deletions

1
.gitignore vendored
View File

@ -66,3 +66,4 @@ typings/
.env
/public/apidoc
/savedBoards.json

View File

@ -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

View File

@ -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

View File

@ -14,6 +14,9 @@
"enableWebdav": {
"type": "boolean"
},
"enableFileDatabase": {
"type": "boolean"
},
"performance": {
"additionalProperties": false,
"type": "object",

View File

@ -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