This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (48 loc) · 1.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var express = require('express');
var http = require('http');
var fs = require('fs');
var gm = require('gm').subClass({ imageMagick: true });
var WebSocketServer = require('ws').Server;
var app = express();
var redis_url = (process.env.REDISTOGO_URL || 'redis://localhost:6379') + '?return_buffers=true';
var redis = require('redis-url').connect(redis_url);
var server = http.createServer(app);
server.listen(process.env.PORT || 3000)
var wss = new WebSocketServer({server: server});
var io = require('socket.io').listen(server, {transports: ['xhr-polling'], "polling duration": 10,'log level': 2})
app.use(express.bodyParser());
app.get('/recent.jpg', function(req, res){
res.type('image/jpeg');
redis.get("recent", function(err, reply) {
res.send(reply);
});
})
if(process.env.USERNAME && process.env.PASSWORD)
app.post('/',express.basicAuth(process.env.USERNAME, process.env.PASSWORD))
app.post('/', function(req, res){
if(req.files.file.path){
var newPath = __dirname + "/public/recent.jpg";
var newPath = req.files.file.path + '.resized';
gm(req.files.file.path)
.autoOrient()
.resize(640, 640)
.write(newPath, function (err) {
if(err) console.log(err);
console.log("written!", newPath);
fs.readFile(newPath, function (err, data) {
if (err) throw err;
redis.set("recent", data, function(){
console.log("persisted to redis");
// publish that we got a new image
for(var i in wss.clients)
wss.clients[i].send("?"+(+new Date));
io.sockets.emit('image', "?"+(+new Date))
});
});
// respond to the uploader
res.send(err ? 'some kind of ERROR HAPPENED\n' : 'COFFEE!!!\n');
})
}
})
// serve all files in this directory
app.use(express.static(__dirname + '/public'));