forked from weworkweplay/raspeye-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
277 lines (223 loc) · 8.64 KB
/
server.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
var express = require('express'),
app = express(),
gm = require('gm'),
fs = require('fs'),
multiparty = require('multiparty'),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
easyimg = require('easyimage'),
lastSave = 0,
connectionCount,
checkStatusOnUpload = false,
settings;
settings = {
checkForPanic: true,
httpAuth: {
username: 'upload',
password: 'upload'
},
moveNightImages: false,
timeBetweenSaves: 180000,
timeBeforePanic: 1200000,
timeBetweenChecks: 300000
};
app.use(express.compress());
app.use(express.urlencoded());
app.use(express.json());
app.use(express.static('./public'));
server.listen(1337, '127.0.0.1');
// Methods
function checkStatus () {
fs.readdir('./images/', function (err, files) {
if (!err) {
var latest = files[files.length - 1],
now = new Date().getTime();
if (now - parseInt(latest.slice(0, -4), 10) > settings.timeBeforePanic) {
checkStatusOnUpload = true;
panic();
} else {
setTimeout(checkStatus, settings.timeBetweenChecks);
}
}
});
}
function panic () {
console.log('Panic! This will get executed when the RaspEye is down.');
}
setTimeout(checkStatus, settings.timeBetweenChecks);
// Routes
// Get all image paths, sorted by date
// Gets cached for three minutes
app.get('/timeline/', function (req, res) {
var cachePath = './data/timeline.json',
buildCache = function (callback) {
var getDayStamp = function (timestamp) {
var day = new Date(parseInt(timestamp, 10));
day.setHours(0, 0, 0, 0);
return day.getTime();
};
fs.readdir('./images/', function (err, files) {
if (err) { console.log(err); return res.send(500); }
files.reverse();
var days = [],
sorted = {};
dayStamp = getDayStamp(files[0].slice(0, -4));
days.push(dayStamp);
sorted[dayStamp] = [];
// Sort all by day
for (var i = 0; i < files.length; i++) {
var timestamp = parseInt(files[i].slice(0, -4), 10);
if (timestamp >= dayStamp) {
sorted[dayStamp].push(files[i]);
} else {
dayStamp = getDayStamp(timestamp);
days.push(dayStamp);
sorted[dayStamp] = [];
}
}
// Dirty: write the whole part to a JSON file
// We need a WriteStream because the JSON file could get pretty big
var cache = fs.createWriteStream(cachePath);
cache.on('close', callback);
cache.write('{\n\t');
cache.write('"days": [');
cache.write(days.join(','));
cache.write('],\n\t');
for (var j = 0; j < days.length; j++) {
var day = days[j],
s = sorted[day];
cache.write('"' + day + '": [\n');
for (var k = 0; k < s.length - 1; k++) {
cache.write('\t\t"' + s[k] + '",\n');
}
cache.write('\t\t"' + s[s.length - 1] + '"\n');
cache.write('\t]');
if (j < days.length - 1) {
cache.write(',\n\t');
}
}
cache.write('\n}');
cache.end();
});
}
fs.open(cachePath, 'r', function (err, fd) {
if (err) return res.send(500);
fs.fstat(fd, function (err, stats) {
if (err) return res.send(500);
// Cache results for 3 minutes
if (new Date().getTime() - stats.mtime.getTime() < 180000) {
res.sendfile(cachePath);
} else {
buildCache(function () {
res.sendfile(cachePath);
});
}
});
});
});
// Send static full resolution images
app.get('/images/*.jpg', function (req, res) {
res.sendfile(__dirname + req.path);
});
// Send static live image
app.get('/live.jpg', function (req, res) {
fs.readdir(__dirname + '/live/', function (err, files) {
if (!err) {
res.sendfile(__dirname + '/live/' + files[files.length - 1]);
}
});
});
// Send thumbnails
app.get('/thumbnails/*.jpg', function (req, res) {
res.sendfile(__dirname + req.path);
});
// Receive post uploads
app.post('/upload/', express.basicAuth(settings.httpAuth.username, settings.httpAuth.password), function (req, res) {
console.log('Upload requested');
var now = new Date().getTime(),
liveStream = fs.createWriteStream(__dirname + '/live/' + now + '.jpg');
// request pipes the file from the Raspberry Pi, just blindly write it to the file system (could be dangerous!)
req.pipe(liveStream);
req.on('end', function () {
io.sockets.emit('image:live');
if (now - lastSave >= settings.timeBetweenSaves) {
gm(__dirname + '/live/' + now + '.jpg').color(function (err, color) {
if (!err) {
var targetFolder = (settings.moveNightImages && color < 10000 && color > 0) ? '/night' : '';
fs.renameSync(__dirname + '/live/' + now + '.jpg', __dirname + targetFolder + '/images/' + now + '.jpg');
fs.readdir(__dirname + '/live/', function (err, files) {
// Don't delete the current file or the livestream will be black
for (var i = 0; i < files.length - 1; i++) {
fs.unlink(__dirname + '/live/' + files[i]);
}
});
easyimg.resize({
src: __dirname + targetFolder + '/images/' + now + '.jpg',
dst: __dirname + targetFolder + '/thumbnails/' + now + '.jpg',
width: 600,
height: 338,
quality: 50
}, function (err, image) {});
lastSave = now;
if (checkStatusOnUpload) {
setTimeout(checkStatus, 60000);
checkStatusOnUpload = false;
}
}
});
}
res.status(200).end();
});
});
// Parse timelapse generation request
app.get('/generate/:start/:end/', function (req, res) {
var start = parseInt(req.params.start, 10),
end = parseInt(req.params.end, 10),
timelapseName = req.params.start + '-' + req.params.end;
fs.exists('./timelapses/' + timelapseName + '.mp4', function (exists) {
if (exists) {
// If the timelapse already exists, force download
res.download('./timelapses/' + timelapseName + '.mp4', 'timelapse.mp4');
return;
}
fs.readdir('./images/', function (err, files) {
if (err) return res.send(500);
// Create a .txt file formatted "file: 'path.jpg'" that ffmpeg can parse
var cache = fs.createWriteStream('./queue/' + timelapseName + '.txt');
files.filter(function(file) {
var timestamp = parseInt(file.slice(0, -4), 10);
return (timestamp >= start && timestamp <= end);
}).forEach(function (v) {
cache.write('file \'../images/' + v + '\'\n');
});
cache.end();
});
// Redirect to a waiting page that will poll the image generation
res.redirect('/queue.html?start=' + start + '&end=' + end);
});
});
// Static timelapse download
app.get('/timelapse/:start-:end.mp4', function (req, res) {
var start = parseInt(req.params.start, 10),
end = parseInt(req.params.end, 10),
timelapseName = req.params.start + '-' + req.params.end;
fs.exists('./timelapses/' + timelapseName + '.mp4', function (exists) {
if (exists) {
res.download('./timelapses/' + timelapseName + '.mp4', 'timelapse.mp4');
return;
}
res.send(404);
});
});
io.sockets.on('connection', function (socket) {
connectionCount++;
io.sockets.emit('connections:count', {count: connectionCount});
fs.readdir('./images/', function (err, files) {
if (files)
socket.emit('image:initial', {url: '/images/' + files[files.length - 1]});
});
socket.on('disconnect', function () {
connectionCount--;
io.sockets.emit('connections:count', {count: connectionCount});
});
});