2018-02-08 17:45:07 +01:00
|
|
|
var PORT = 8080; //Set port for the app
|
|
|
|
|
2018-02-08 23:16:28 +01:00
|
|
|
fs = require("fs-extra");
|
2018-02-08 17:45:07 +01:00
|
|
|
var express = require('express');
|
2018-02-08 23:16:28 +01:00
|
|
|
var formidable = require('formidable'); //form upload processing
|
2018-02-08 17:45:07 +01:00
|
|
|
var s_whiteboard = require("./s_whiteboard.js");
|
|
|
|
|
|
|
|
var app = express();
|
|
|
|
app.use(express.static(__dirname + '/public'));
|
|
|
|
var server = require('http').Server(app);
|
|
|
|
server.listen(PORT);
|
|
|
|
var io = require('socket.io')(server);
|
|
|
|
console.log("Webserver & socketserver running on port:"+PORT);
|
|
|
|
|
|
|
|
app.get('/loadwhiteboard', function(req, res) {
|
|
|
|
var wid = req["query"]["wid"];
|
|
|
|
var ret = s_whiteboard.loadStoredData(wid);
|
|
|
|
res.send(ret);
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
|
2018-02-08 23:16:28 +01:00
|
|
|
app.post('/upload', function(req, res) { //File upload
|
|
|
|
var form = new formidable.IncomingForm(); //Receive form
|
|
|
|
var formData = {
|
|
|
|
files : {},
|
|
|
|
fields : {}
|
|
|
|
}
|
|
|
|
|
|
|
|
form.on('file', function(name, file) {
|
|
|
|
formData["files"][file.name] = file;
|
|
|
|
});
|
|
|
|
|
|
|
|
form.on('field', function(name, value) {
|
|
|
|
formData["fields"][name] = value;
|
|
|
|
});
|
|
|
|
|
|
|
|
form.on('error', function(err) {
|
|
|
|
console.log('File uplaod Error!');
|
|
|
|
});
|
|
|
|
|
|
|
|
form.on('end', function() {
|
|
|
|
progressUploadFormData(formData);
|
|
|
|
res.send("done");
|
|
|
|
//End file upload
|
|
|
|
});
|
|
|
|
form.parse(req);
|
|
|
|
});
|
|
|
|
|
|
|
|
function progressUploadFormData(formData) {
|
|
|
|
console.log("Progress new Form Data");
|
|
|
|
var fields = formData.fields;
|
|
|
|
var files = formData.files;
|
|
|
|
var whiteboardId = fields["whiteboardId"];
|
|
|
|
|
|
|
|
var name = fields["name"] || "";
|
|
|
|
var date = fields["date"] || (+new Date());
|
|
|
|
var filename = whiteboardId+"_"+date+".png";
|
|
|
|
|
|
|
|
fs.ensureDir("./public/uploads", function(err) {
|
2019-01-11 10:33:32 +01:00
|
|
|
if(err) {
|
|
|
|
console.log("Could not create upload folder!", err);
|
|
|
|
return;
|
|
|
|
}
|
2018-02-08 23:16:28 +01:00
|
|
|
var imagedata = fields["imagedata"];
|
2019-01-11 10:33:32 +01:00
|
|
|
if(imagedata && imagedata != "") { //Save from base64 data
|
2018-02-08 23:16:28 +01:00
|
|
|
imagedata = imagedata.replace(/^data:image\/png;base64,/, "").replace(/^data:image\/jpeg;base64,/, "");
|
|
|
|
console.log(filename, "uploaded");
|
|
|
|
fs.writeFile('./public/uploads/'+filename, imagedata, 'base64', function(err) {
|
|
|
|
if(err) {
|
|
|
|
console.log("error", err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-08 17:45:07 +01:00
|
|
|
io.on('connection', function(socket){
|
2018-02-09 02:04:50 +01:00
|
|
|
|
2018-02-08 23:38:41 +01:00
|
|
|
socket.on('disconnect', function () {
|
2019-01-11 10:55:37 +01:00
|
|
|
socket.broadcast.emit('refreshUserBadges', null); //Removes old user Badges
|
2018-02-08 23:38:41 +01:00
|
|
|
});
|
|
|
|
|
2018-02-08 17:45:07 +01:00
|
|
|
socket.on('drawToWhiteboard', function(content) {
|
2018-02-08 23:43:14 +01:00
|
|
|
content = escapeAllContentStrings(content);
|
2019-01-11 10:55:37 +01:00
|
|
|
socket.broadcast.to(content["wid"]).emit('drawToWhiteboard', content); //Send to all users in the room (not own socket)
|
2018-02-08 17:45:07 +01:00
|
|
|
s_whiteboard.handleEventsAndData(content); //save whiteboardchanges on the server
|
|
|
|
});
|
2018-02-09 02:04:50 +01:00
|
|
|
|
|
|
|
socket.on('joinWhiteboard', function(wid) {
|
2019-01-11 10:55:37 +01:00
|
|
|
socket.join(wid); //Joins room name=wid
|
2018-02-09 02:04:50 +01:00
|
|
|
});
|
2018-02-08 23:43:14 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
//Prevent cross site scripting
|
|
|
|
function escapeAllContentStrings(content, cnt) {
|
|
|
|
if(!cnt)
|
|
|
|
cnt = 0;
|
|
|
|
|
|
|
|
if(typeof(content)=="string") {
|
|
|
|
return content.replace(/<\/?[^>]+(>|$)/g, "");
|
|
|
|
}
|
|
|
|
for(var i in content) {
|
|
|
|
if(typeof(content[i])=="string") {
|
|
|
|
content[i] = content[i].replace(/<\/?[^>]+(>|$)/g, "");
|
|
|
|
} if(typeof(content[i])=="object" && cnt < 10) {
|
|
|
|
content[i] = escapeAllContentStrings(content[i], ++cnt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return content;
|
|
|
|
}
|