-
Notifications
You must be signed in to change notification settings - Fork 22
/
server.js
314 lines (247 loc) · 9.49 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
var THUMBNAILS_DIR = "thumbnails/";
var BUFFER_DIR = "buffer/";
var fs = require('fs')
, async = require('async')
, md5 = require('MD5')
, express = require('express')
, spawn = require('child_process').spawn
, exec = require('child_process').exec
, humanize = require('humanize')
, utils = require('./lib/utils')
, _ = require('lodash')
, mw = require('./lib/middlewares')
, env = process.env.NODE_ENV || "development"
;
utils.ensureDirectoryExists('videos');
utils.ensureDirectoryExists('thumbnails');
var hooks = require('./hooks/index');
var settings = require('./settings.'+env+'.json');
var server = express();
var port = settings.port || 1212;
server.set('port', port);
server.recordingWindow = { start: -8, duration: 20 };
require('./config/express')(server);
server.lastRecording = { time: 0, data: {} };
server.set('secret', settings.secret);
server.info = function() {
return { recordingWindow: server.recordingWindow, channel: settings.channel, lastRecording: server.lastRecording };
}
/* *************
* Server routes
*/
server.get('/start', mw.restricted, function(req, res) {
var channel = req.param('channel');
if(settings.videostreams[channel]) {
exec("pm2 restart stream-"+channel);
return res.send("Running pm2 restart stream-"+channel);
}
});
server.get('/stop', mw.restricted, function(req, res) {
var channel = req.param('channel');
if(settings.videostreams[channel]) {
exec("pm2 stop stream-"+channel);
return res.send("Running pm2 stop stream-"+channel);
}
});
server.get('/settings', mw.restricted, function(req, res) {
var start = req.param('start', server.recordingWindow.start);
var duration = req.param('duration', server.recordingWindow.duration);
var channel = req.param('channel');
if(channel && settings.videostreams[channel] && channel != settings.channel) {
console.log(humanize.date('Y-m-d H:i:s')+" changing videostream channel to "+channel);
settings.channel = channel;
utils.saveSettings(settings);
exec("pm2 restart stream");
}
server.recordingWindow.start = start;
server.recordingWindow.duration = duration;
res.send(server.info());
});
server.get('/hooks', mw.restricted, function(req, res) {
res.send(settings.hooks);
});
server.get('/record', mw.restricted, function(req, res) {
if(server.busy) {
return res.send("Sorry server already busy recording");
}
if(((new Date).getTime() - server.lastRecording.time) < 5000) {
console.error("Last recording less than 5s ago, aborting");
return res.send("Last recording less than 5s ago, aborting");
}
var channel = req.param('channel', settings.channel);
var start = req.param('start', server.recordingWindow.start);
var duration = req.param('duration', server.recordingWindow.duration);
var text = req.param('text','');
console.log(humanize.date('Y-m-d H:i:s')+" /record?channel="+channel+"&start="+start+"&duration="+duration+"&text="+text);
res.send("Recording video...");
server.lastRecording.time = new Date;
server.busy = true;
utils.record(channel, start, duration, function(err, videofilename) {
if(err || !videofilename) return res.send(500, "No video filename returned");
var videoId = videofilename.replace('videos/','').replace('.mp4','');
var videoUrl = settings.base_url+"/video?v="+videoId;
// Generating the thumbnail and animated gif
async.parallel([
function(done) {
utils.mp4toJPG(videofilename, Math.floor(duration/2), done);
},
function(done) {
utils.mp4toGIF(videofilename, Math.max(2,start), Math.min(14,duration), done);
}], function(err, results) {
server.busy = false;
var data = {
id: videoId
, text: text
, video: videoUrl
, videofilename: videofilename
, thumbnail: videoUrl.replace('video','thumbnail')
, gif: settings.base_url+"/videos/"+videoId+".gif"
, gifsize: fs.statSync('videos/'+videoId+'.gif').size
}
server.lastRecording.data = data;
try {
hooks.all(data);
} catch(e) {
console.error("Error in hooks: ", e, e.stack);
}
});
});
});
server.post('/webhook', function(req, res) {
console.log("Received webhook: ", req.body);
res.send(req.body);
});
server.post('/hooks/test', function(req, res) {
var service = req.body.service;
var options = req.body.options;
if(!hooks[service]) {
return res.send({code:500, status: "error", error: "Unknown service"});
}
var hook = new hooks[service](options);
var data = {
id: '2014-06-19-20-31-24',
text: 'Goal for Belgium! #ZKO 0-1 #BEL #WorldCup \n📺Video:',
video: 'http://replaylastgoal.com/video?v=ned3-2014-06-26-21-35-23',
videofilename: 'videos/ned3-2014-06-26-21-35-23.mp4',
thumbnail: 'http://replaylastgoal.com/thumbnail?v=ned3-2014-06-26-21-35-23',
gif: 'http://replaylastgoal.com/videos/ned3-2014-06-26-21-35-23.gif',
gifsize: 1916223
};
console.log(humanize.date('Y-m-d H:i:s')+" Sending test data to hook "+service, data);
hook(data, function(err, result) {
if(err) {
console.error(err);
return res.send({code:500, status: "error", error: err.toString()});
}
res.send({code: 200, status: "success", data: data});
});
});
server.get('/hooks/activate', function(req, res) {
var service = req.param('service');
var id = req.param('id');
console.log(humanize.date('Y-m-d H:i:s')+" Activating "+service+" hook "+id);
if(!hooks[service]) {
return res.send({code:500, status: "error", error: "Unknown service"});
}
var index = _.findIndex(settings.hooks, {id: id});
if(index == -1) {
return res.send({code: 404, status: "Not found", error: "We couldn't find any hook with this id"});
}
settings.hooks[index].active = true;
settings.hooks[index].date.modified = new Date();
utils.saveSettings(settings);
res.send({code: 200, status: "Hook activated"});
});
server.post('/hooks/save', function(req, res) {
var service = req.body.service;
var options = req.body.options || {};
if(!hooks[service]) {
return res.send({code:500, status: "error", error: "Unknown service"});
}
var hookconfig = {
service: service,
options: options,
id: md5(JSON.stringify(options)+settings.secret),
date: { created: new Date() },
active: false
};
if(_.findIndex(settings.hooks, {id:hookconfig.id}) != -1) {
return res.send({code: 500, status: "error", error: "Hook already present"})
}
var hook = new hooks[service](options);
var activationUrl = settings.base_url+"/hooks/activate?service="+service+"&id="+hookconfig.id;
var removingUrl = settings.base_url+"/hooks/remove?service="+service+"&id="+hookconfig.id;
var data = {
id: '',
text: 'Please click this link to activate your hook: '+activationUrl+'\n\n(You can remove this webhook at anytime by clicking this link: '+removingUrl+' )',
video: '',
videofilename: '',
thumbnail: '',
gif: '',
gifsize: 0
};
console.log(humanize.date('Y-m-d H:i:s')+" test> Sending test data to hook "+service, data);
hook(data, function(err, result) {
if(err) {
console.error(err);
return res.send({code:500, status: "error", error: err.toString()});
}
settings.hooks.push(hookconfig);
console.log(humanize.date('Y-m-d H:i:s')+" test> Adding new hook", hookconfig);
utils.saveSettings(settings);
res.send({code: 200, status: "success", hook: hookconfig});
});
});
server.get('/hooks/remove', function(req, res) {
var service = req.param('service');
var id = req.param('id');
if(!hooks[service]) {
return res.send({code:500, status: "error", error: "Unknown service"});
}
var hooksArray = _.filter(settings.hooks, function(hook) {
return (hook.id != id);
});
if(hooksArray.length == settings.hooks.length) {
return res.send({code: 404, status: "No such hook found"});
}
settings.hooks = hooksArray;
utils.saveSettings(settings);
res.send({code: 200, status: "success"});
});
server.get('/hooks/add', function(req, res) {
res.render('addhook');
});
server.get('/latest.gif', function(req, res) {
res.redirect("/gif?v="+server.lastRecording.data.id);
});
server.get('/', function(req, res) {
res.render('home', { title: "@ReplayLastGoal" });
});
server.get('/latest', function(req, res) {
res.redirect("/video?v="+server.lastRecording.data.id);
});
server.get('/video', mw.requireValidVideoID, function(req, res, next) {
var v = req.param('v');
var video = settings.base_url+'/videos/'+v+'.mp4';
var thumbnail = settings.base_url+'/thumbnail?v='+ v;
res.render('video.hbs', {title: "View video replay of the world cup goal", thumbnail: thumbnail, video: video });
});
server.get('/thumbnail', mw.requireValidVideoID, function(req, res, next) {
var v = req.param('v');
res.sendfile('./'+THUMBNAILS_DIR + v + '.jpg');
});
server.get('/gif', mw.requireValidVideoID, function(req, res, next) {
var v = req.param('v');
res.sendfile('./videos/' + v + '.gif');
});
server.get('/live', mw.restricted, function(req, res) {
var channel = req.param('channel', settings.channel);
res.render('live.hbs', {
videostream: "/buffer/"+channel+"/livestream.m3u8" // settings.videostreams[channel]
});
});
server.use('/videos', express.static('videos/'));
server.use('/buffer', express.static('buffer/'));
server.use('/status', require('./lib/status'));
console.log(humanize.date('Y-m-d H:i:s')+" Server listening in "+server.set('env')+" environment on port "+port+" with the following settings: ", server.info());
server.listen(port);