-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmusiclibrary.js
52 lines (42 loc) · 1.26 KB
/
musiclibrary.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
var EventEmitter = require('events').EventEmitter
, path = require('path')
, util = require('util')
, findit = require('findit')
var FORMATS = ['.mp3', '.m4a'] //supported formats
module.exports = MusicLibrary
//
// MusicLibrary: populates self#songs with name:filepath pairs
//
// MusicLibrary emits these events:
// songs:add
// ready
//
// opts has these optional keys:
// root: relative directory to serve [defaults to ./public/music]
//
function MusicLibrary (opts) {
EventEmitter.call(this)
if(!opts) opts = {}
else if(typeof opts !== 'object') throw new Error('MusicLibrary opts must be of type object')
this.opts = opts
this.songs = {}
this.populate()
}
util.inherits(MusicLibrary, EventEmitter)
MusicLibrary.prototype.populate = function () {
var self = this
, root = this.opts.root || path.join(__dirname, '/public/music')
, finder = findit.find(root)
// cache music library for socket connections
finder.on('file', function(fpath, stat) {
var ext = path.extname(fpath)
if(FORMATS.indexOf(ext) !== -1 && stat.size) {
var songTitle = path.basename(fpath, ext)
self.songs[songTitle] = fpath
self.emit('songs:add', songTitle)
}
})
finder.on('end', function () {
self.emit('ready')
})
}