-
Notifications
You must be signed in to change notification settings - Fork 2
/
stream.js
405 lines (367 loc) · 12 KB
/
stream.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
var express = require('express'),
fs = require('fs'),
jade = require('jade'),
file = require('file'),
path = require('path'),
mm = require('musicmetadata'),
crypto = require('crypto'),
Sequelize = require('sequelize'),
request = require('request'),
qs = require('querystring'),
execFile = require('child_process').execFile;
passport = require('passport'),
LocalStrategy = require('passport-local').Strategy;
var app = express(),
db = new Sequelize('ricochet', 'stream', 'lolwat', {
logging: false
}),
config = require('./config.js').config,
Schema = require('./includes/schema.js'),
LastFM = require('./includes/lastfm.js'),
Elasticsearch = require('./includes/elasticsearch.js');
var models = Schema(db, config, updateIndex),
lastfm = LastFM(app, db, config),
elasticsearch = Elasticsearch('http://micro.hm:9200', 'ricochet', 'track');
// -- Passport Configuration
passport.use(new LocalStrategy(function(username, password, done) {
models.User.find({ where: { username: username }}).success(function(user) {
if (!user) {
return done(null, false, { message: 'Incorrect username' });
}
if (!user.isValidPassword(password)) {
return done(null, false, { message: 'Username and password do not match' });
}
return done(null, user);
});
}));
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
function isLoggedIn(req, res, next) {
// Middleware to authenticate requests
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login');
}
// -- Setup Express
app.use(express.static(__dirname + '/static/'));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
app.use(passport.session());
app.set('views', __dirname + '/templates/');
app.set('view engine', 'jade');
// -- Express endpoints
app.get('/', isLoggedIn, function(req, res) {
passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/login'
});
models.Track.findAll({
order: 'album ASC, track_num ASC, title ASC'
})
.success(function(tracks) {
tracks = models.Track.hydrateMultiple(tracks);
res.render('index', {
pageTitle: 'Ricochet',
library: tracks,
user: req.user
});
});
});
app.get('/play/:id', isLoggedIn, function(req, res) {
req.connection.setTimeout(750000); // 15 minutes
// Look up the track to get its filename
var id = req.params['id'];
models.Track.find({
where: { id: id }
})
.success(function(track) {
try {
fs.readFile(track.filename, function(err, data) {
if (err) throw err;
res.type('audio/mpeg');
res.send(data);
});
} catch (e) {
console.log('Could not find file ' + track.filename);
return;
}
// Mark as now playing in LastFM
lastfm.nowPlaying(track, req.user);
})
.error(function(err) {
console.log('Could not find file in index with id ' + id);
});
});
app.get('/play/:id/:action', isLoggedIn, function(req, res) {
var id = req.params['id'],
action = req.params['action'];
models.Track.find({
where: { id: id }
})
.success(function(track) {
if (action == 'data') {
res.json(track.hydrate());
}
});
});
app.post('/play/:id/:action', isLoggedIn, function(req, res) {
var id = req.params['id'],
action = req.params['action'];
models.Track.find({
where: { id: id }
})
.success(function(track) {
if (action == 'increment') {
track.play_count++;
track.save()
.success(function(track) {
res.render('row', {
track: track.hydrate()
});
});
} else if (action == 'edit') {
track.title = req.body['title'];
track.artist = req.body['artist'];
track.album = req.body['album'];
track.genre = req.body['genre'];
track.year = req.body['year'];
track.track_num = req.body['track_num'];
track.track_total = req.body['track_total'];
track.disc_num = req.body['disc_num'];
track.disc_total = req.body['disc_total'];
console.log("Updating track with new title " + track.title);
track.save()
.success(function(t) {
res.render('row', {
track: t.hydrate(),
});
});
} else if (action == 'scrobble') {
lastfm.scrobble(track, req.user, req.body['elapsed']);
}
})
.error(function(err) {
console.log('Could not find file in index with id ' + id);
});
});
app.get('/search/:query?', isLoggedIn, function(req, res) {
// Perform a plaintext search
var query = req.params['query'];
// If the query is empty, return all tracks
if (!query) {
models.Track.findAll({
order: 'album ASC, track_num ASC, title ASC'
})
.success(function(tracks) {
res.render('library', {
library: models.Track.hydrateMultiple(tracks)
});
});
return;
}
// Otherwise perform plaintext search
search(query, function(results) {
res.render('library', {
library: models.Track.hydrateMultiple(results)
});
});
});
app.post('/server/reindex', isLoggedIn, function(req, res) {
// Reindex the server
updateIndex();
res.send("OK");
});
app.get('/server/status', function(req, res) {
var logged_in = req.isAuthenticated();
var data = {
is_logged_in: logged_in,
uptime: process.uptime()
}
if (logged_in) {
data.user = {
id: req.user.id
}
}
res.json(data);
});
// -- Profile
app.get('/profile', isLoggedIn, function(req, res) {
models.User.find(req.user.id).success(function(user) {
res.json(user);
return;
});
});
app.post('/profile/:action', isLoggedIn, function(req, res) {
var action = req.params['action'];
models.User.find(req.user.id).success(function(user) {
if (action == 'edit_profile') {
user.email_address = req.body['email_address'];
user.save();
res.json(user);
return;
} else if (action == 'edit_password') {
var new_pwd = req.body['password'],
new_pwd_confirm = req.body['password_confirm'];
// Make sure passwords are not empty
if (new_pwd == '' || new_pwd_confirm == '') {
res.status(400).send("Password cannot be empty")
return;
}
// Make sure passwords match
if (new_pwd != new_pwd_confirm) {
res.status(400).send("Password mismatch");
return;
}
user.password = models.User.createPassword(new_pwd);
user.save();
res.json(user);
return;
} else if (action = 'edit_lastfm') {
var scrobble = req.body['scrobble-status'] ? true : false;
user.lastfm_scrobble = scrobble;
user.save();
res.json(user);
return;
}
res.status(501).send("Not Implemented");
});
});
// -- Login & Authentication
app.get('/login', function(req, res) {
res.render('login', {
pageTitle: 'Login'
});
});
app.post('/login',
passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/login'
}));
app.all('/logout', function(req, res) {
req.logout();
res.redirect('/login');
});
// -- LastFM
app.get('/lastfm', function(req, res) {
var redirect_url = getRootURL() + "/lastfm/authorize",
url = "http://www.last.fm/api/auth/?api_key=" + config.lastfm_key + "&cb=" + redirect_url;
res.redirect(url);
});
app.get('/lastfm/authorize', isLoggedIn, function(req, res) {
var token = req.query.token;
if (!token) {
res.redirect('/');
return;
}
models.User.find(req.user.id).success(function(user) {
lastfm.getSession(user, token, function() {
res.redirect('/');
});
});
});
app.get('/lastfm/deauth', isLoggedIn, function(req, res) {
models.User.find(req.user.id).success(function(user) {
lastfm.destroySession(user, function() {
res.redirect('/');
});
});
});
// Start the server
app.listen(config.port);
// -- Indexes the music library
function updateIndex() {
// Use `find` to get a list of all files recursively in library_path
execFile('find', [ config.library_path, '-type', 'f' ], function(err, stdout, stderr) {
var file_list = stdout.split('\n');
file_list.pop(); // removes current dir from file_list
file_list.forEach(function(filename, i) {
console.log("Indexing " + filename);
// bail if we've found an invalid extension
var extension = path.extname(filename).toLowerCase();
if (config.valid_extensions.indexOf(extension) < 0) {
console.log("Invalid extension \"" + extension + "\" found");
return;
}
var id = crypto.createHash('md5').update(filename).digest('hex');
var parser = mm(fs.createReadStream(filename), { duration: true });
// Get id3 tags
//id3({ file: filename, type: id3.OPEN_LOCAL }, function (err, tags) {
parser.on('metadata', function(tags) {
// Start building a model of metadata
var track_data = {
filename: filename,
title: getTitle(tags.title, filename),
duration: tags.duration,
artist: tagOrDefault(tags.artist, ''),
album: tagOrDefault(tags.album, ''),
genre: tagOrDefault(tags.genre, ''),
year: tagOrDefault(tags.year, ''),
track_num: tags.track.no,
track_total: tags.track.of,
disc_num: tags.disk.no,
disc_total: tags.disk.of
};
// Check if the index already exists, otherwise create it
models.Track.findOrCreate({ id: id }, track_data)
.success(function(track, created) {
if (created) {
console.log("Created " + track.title);
} else {
console.log("Indexed " + track.title);
}
// Register this track for search indexing
elasticsearch.index(track);
})
.error(function(err) {
console.log(err);
});
});
parser.on('done', function(err) {
if (err) {
console.log(err);
stream.destroy();
}
});
});
});
}
// -- Indexer helper functions
function getTitle(title, filename) {
if (!(title == null || title == '')) {
return title;
}
var filetitle = path.basename(data.filename).match(/^\d+ (.*)\..*/);
if (filetitle != null && filetitle.length > 1) {
return filetitle[1];
}
}
function tagOrDefault(tag, def) {
if (tag == '') {
return def;
}
return tag;
}
// -- Search functions
function search(query, then) {
elasticsearch.searchWildcard(query, function(task_ids) {
models.Track.findAll({
where: {
id: task_ids
}
})
.success(function(tracks) {
then(tracks);
});
});
}
// -- General helper functions
function getRootURL() {
return config.root_url + (config.port == 80 ? '' : ':' + config.port)
}