├── .gitignore ├── package.json └── main.js /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | node_modules 10 | 11 | pids 12 | logs 13 | results 14 | 15 | npm-debug.log 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "guimotståelig-bildestrøm", 3 | "version": "0.0.0", 4 | "repository": "", 5 | "author": "", 6 | "license": "BSD", 7 | "dependencies": { 8 | "chokidar": "~0.7.0", 9 | "socket.io": "~0.9.16" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /main.js: -------------------------------------------------------------------------------- 1 | var app = require('http').createServer() 2 | , io = require('socket.io').listen(app, { log: false }) 3 | , fs = require('fs') 4 | , chokidar = require('chokidar'); 5 | 6 | app.listen(1237); 7 | 8 | var emit = function(event, data){ 9 | clients.forEach(function (client) { 10 | client.emit(event, data); 11 | }); 12 | }; 13 | 14 | var resetTimer = function () { 15 | clearTimeout(timeout); 16 | timeout = setTimeout(loadFromArchive, (3 + Math.random()*5)*1000); 17 | }; 18 | 19 | var loadFromArchive = function(){ 20 | var files = fs.readdirSync(__dirname + '/imagepool'); 21 | if(files.length > 0){ 22 | var index = Math.floor((Math.random()*files.length)); 23 | fs.readFile(__dirname + '/imagepool/' + files[index], 'base64', function(err, data){ 24 | if(!err){ 25 | emit('picture', 'data:image/jpg;base64,' + data); 26 | }else{ 27 | emit('error', err); 28 | } 29 | }); 30 | }; 31 | resetTimer(); 32 | }; 33 | 34 | var uploadDir = chokidar.watch(__dirname + '/images', {ignored: /^\./, persistent: true}); 35 | 36 | var clients = []; 37 | var timeout = 0; 38 | io.sockets.on('connection', function (socket) { 39 | console.log('client connected'); 40 | clients.push(socket); 41 | uploadDir 42 | .on('add', function(path) { 43 | fs.readFile(path, 'base64', function(err, data){ 44 | if(!err){ 45 | emit('picture', 'data:image/jpg;base64,' + data); 46 | resetTimer(); 47 | }else{ 48 | emit('error', err); 49 | } 50 | }); 51 | }); 52 | socket.on('addphoto', function (data) { 53 | console.log('photo gotten'); 54 | if(data && data.dataurl){ 55 | var base64Data = data.dataurl.replace(/^data:image\/jpeg;base64,/, ""); 56 | base64Data += base64Data.replace('+', ' '); 57 | binaryData = new Buffer(base64Data, 'base64').toString('binary'); 58 | 59 | fs.writeFile(__dirname + '/imagepool/' + Math.floor(Math.random() * 10000000) + ".jpg", binaryData, "binary", function (err) {}); 60 | emit('picture', data.dataurl); 61 | resetTimer(); 62 | }else{ 63 | console.log('Worng input', data); 64 | } 65 | 66 | }); 67 | 68 | resetTimer(); 69 | }); --------------------------------------------------------------------------------