forked from fivetanley/tehmusic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
386 lines (331 loc) · 11.3 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
module.exports = function(server) {
var DotEnv = require('dotenv-node'),
bcrypt = require('bcrypt'),
FirebaseTokenGenerator = require("firebase-token-generator"),
Firebase = require('firebase'),
expressValidator = require('express-validator'),
express = require('express'),
util = require('util'),
fs = require('fs'),
AWS = require('aws-sdk'),
crypto = require('crypto');
// Load config
if (fs.existsSync('./.env')) {
new DotEnv();
}
// Configure AWS
var s3 = new AWS.S3({
apiVersion: 'latest',
accessKeyId: process.env.S3_ACCESS_KEY_ID,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
region: process.env.S3_REGION
});
// Create our token generator
var tokenGenerator = new FirebaseTokenGenerator(process.env.FIREBASE_SECRET);
// Create a token for our own usage
var adminToken = tokenGenerator.createToken({}, { admin: true });
var fbRef = new Firebase(process.env.FIREBASE_ROOT);
fbRef.auth(adminToken, function(err) {
if (err) {
console.log("Could not authenticate admin account to firebase? " + err);
// panic?
}
});
var songLoop = null;
// We'll keep (in memory) an object holding all the current users,
// so we can do uber fast auth.
var cachedUsers = {};
var usersRef = fbRef.child('users');
usersRef.on('value', function(snapshot) {
cachedUsers = snapshot.val();
if (cachedUsers === null) {
cachedUsers = {};
}
setTimeout(function() { songLoop(); }, 1);
});
var cachedUserPrivates = {};
var userPrivatesRef = fbRef.child('user_privates');
userPrivatesRef.on('value', function(snapshot) {
cachedUserPrivates = snapshot.val();
if (cachedUserPrivates === null) {
cachedUserPrivates = {};
}
});
var currentSong = null;
// Make someone lose DJ status when all connections disappear
fbRef.child('online_users').on('child_removed', function(snapshot) {
fbRef.child('users').child(snapshot.name()).child('spinning').set(false);
// clear out the current song if it's this person's
if (currentSong && currentSong.user_id === snapshot.name()) {
fbRef.child('current_song').set(null);
}
});
songLoop = function() {
// is the current song's dj still up?
if (currentSong && !currentSong.done &&
!cachedUsers[currentSong.user_id].spinning) {
fbRef.child('current_song').child('done').set(true);
return;
}
// is it time to move to the next DJ?
if (!currentSong || currentSong.done) {
// choose next DJ
var djs = [];
Object.keys(cachedUsers).forEach(function(userId) {
var user = cachedUsers[userId];
if (user.spinning) {
djs.push({ id: userId, user: user });
}
});
djs = djs.sort(function(a, b) {
return a.user.spinning < b.user.spinning ? -1 : 1;
});
if (djs.length === 0) {
fbRef.child('current_song').remove();
return;
}
var currentDjIndex = -1;
if (currentSong) {
djs.forEach(function(dj, idx) {
if (dj.id == currentSong.user_id) {
currentDjIndex = idx;
}
});
}
if (currentDjIndex < 0) {
console.log("Could not find current DJ! just choosing the first one.");
}
currentDjIndex += 1;
if (currentDjIndex >= djs.length) {
currentDjIndex = 0;
}
var newDj = djs[currentDjIndex];
// find the next song on this djs current playlist
var currentPlaylistId = newDj.user.current_playlist_id;
if (currentPlaylistId === null) {
// no playlists, kick this user out of the booth!
console.log("no playlists for " + newDj.id);
fbRef.child('users').child(newDj.id).child('spinning').set(false, function() {
setTimeout(songLoop, 0);
return;
});
}
var playlistSongsRef = fbRef.child('playlists').
child(newDj.id).
child(currentPlaylistId).
child('songs');
playlistSongsRef.once('value', function(snapshot) {
var playlistSongs = snapshot.val();
if (playlistSongs === null) {
// no songs, kick out this user!
console.log("no songs for " + newDj.id);
fbRef.child('users').child(newDj.id).child('spinning').set(false, function() {
setTimeout(songLoop, 0);
return;
});
}
var first = null;
var highestOrder = 0;
Object.keys(playlistSongs).forEach(function(playlistSongId) {
var playlistSong = playlistSongs[playlistSongId];
if (!first || playlistSong.order < first.order) {
first = playlistSong;
first.songId = playlistSongId;
}
if (playlistSong.order > highestOrder) {
highestOrder = playlistSong.order;
}
});
// move that song to the end of the playlist
playlistSongsRef.child(first.songId).child('order').set(highestOrder + 1);
// ... and set it to the current song.
fbRef.child('current_song').set({
song_id: first.songId,
user_id: newDj.id,
start_time: (new Date()).getTime()
});
// ... and add it to the song history
fbRef.child('history').push().set({
song_id: first.songId,
user_id: newDj.id,
start_time: (new Date()).getTime()
});
});
}
};
setInterval(songLoop, 5000);
// Keep current song state in memory all the time
fbRef.child('current_song').on('value', function(snapshot) {
currentSong = snapshot.val();
songLoop();
});
server.use(express.bodyParser());
server.use(expressValidator());
// Force SSL when in production
if (process.env.ENV === "production") {
server.get('*', function(req, res, next){
if(req.headers['x-forwarded-proto'] != 'https')
res.redirect('https://' + req.headers['host'] + '/' + req.url)
else
next() /* Continue to other routes if we're not redirecting */
});
}
var hmac = function(ary) {
return crypto.createHmac('sha1', process.env.FIREBASE_SECRET).
update(ary.join(':')).
digest('hex');
}
var genAuth = function(username) {
return {
username: username,
token: tokenGenerator.createToken({ username: username }),
hmac: hmac([username])
}
}
var checkAuth = function(data) {
var goodHmac = hmac([data.username]);
if (goodHmac === data.hmac) {
return true;
}
return false;
}
// Create an API namespace, so that the root does not
// have to be repeated for each end point.
server.namespace('/api', function() {
server.post('/login', function(req, res) {
var user = cachedUsers[req.body.username];
if (!user) {
res.send({ error: "invalid username or password" });
return;
}
var userPrivates = cachedUserPrivates[req.body.username];
bcrypt.compare(req.body.password, userPrivates.password, function(err, match) {
if (match === true) {
res.send({
status: "ok",
auth: genAuth(req.body.username)
});
return;
}
res.send({ error: "invalid username or password" });
});
});
server.get('/config.js', function(req, res) {
res.header("Content-Type", "application/javascript");
res.end("window.FIREBASE_ROOT = '" + process.env.FIREBASE_ROOT + "';");
});
server.post('/register', function(req, res) {
req.checkBody('username', 'Invalid username').notEmpty().isAlphanumeric();
req.checkBody('email', 'Invalid email').notEmpty().isEmail();
req.checkBody('password', 'No password').notEmpty();
req.checkBody('joinCode', 'No join code').notEmpty();
var errors = req.validationErrors(true);
if (errors) {
res.send({ error: util.inspect(errors) });
return;
}
if (req.body.joinCode != process.env.JOIN_CODE) {
res.send({ error: "Incorrect join code" });
return;
}
var user = cachedUsers[req.body.username];
if (user) {
res.send({ error: "user already exists" });
return;
}
bcrypt.hash(req.body.password, 10, function(err, hash) {
usersRef.child(req.body.username).set({
screenname: req.body.username,
email: req.body.email
}, function(err) {
if (err) {
console.log("error saving user " + err);
res.send({ error: "error saving user" });
return;
}
userPrivatesRef.child(req.body.username).set({
password: hash
}, function(err) {
if (err) {
console.log("error saving user password " + err);
res.send({ error: "error saving user" });
return;
}
// create a default playlist
var playlist = { name: "Default" };
var playlistRef = fbRef.child('playlists').child(req.body.username).push(playlist);
usersRef.child(req.body.username).child('current_playlist_id').set(playlistRef.name());
res.send({
status: "ok",
auth: genAuth(req.body.username)
});
});
});
});
});
server.post('/upload', function(req, res) {
var auth = JSON.parse(req.body.auth);
if (!checkAuth(auth)) {
res.send({ error: "unauthorized" });
return;
}
// add file to firebase...
var songRef = fbRef.child('songs').push({
name: req.files.file.name || "",
artist: req.body.artist || "",
title: req.body.title || req.files.file.name || "",
album: req.body.album || "",
genre: req.body.genre || "",
uploaded_by: auth.username
});
console.log("file at " + req.files.file.path);
s3.client.putObject({
Bucket: process.env.S3_BUCKET,
Key: 'media/' + songRef.name(),
Body: fs.createReadStream(req.files.file.path),
ACL: "public-read",
ContentType: req.files.file.type
}, function(err, data) {
console.log("upload done, err: " + err + " data: " + data);
if (err) {
res.send({ error: "error uploading " + err });
return;
}
songRef.child('url').set('https://s3.amazonaws.com/' +
process.env.S3_BUCKET +
'/media/' +
songRef.name());
var success = function() {
res.send({
status: "ok",
file: {
id: songRef.name()
}
});
};
if (req.files.picture) {
var data = fs.readFileSync(req.files.picture.path);
var sha1sum = crypto.createHash('sha1');
sha1sum.update(data);
var sha1 = sha1sum.digest('hex');
s3.client.putObject({
Bucket: process.env.S3_BUCKET,
Key: 'media/' + sha1,
Body: data,
ACL: "public-read",
ContentType: req.files.picture.type
}, function(err, data) {
songRef.child('image_url').set('https://s3.amazonaws.com/' +
process.env.S3_BUCKET +
'/media/' +
sha1);
success();
});
}
else {
success();
}
});
});
});
};