2017-04-09 20:57:32 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const glob = require('glob');
|
|
|
|
|
2017-04-11 22:58:43 +00:00
|
|
|
function pickRandom(arr) {
|
|
|
|
var index = Math.floor(Math.random() * arr.length);
|
|
|
|
return arr[index];
|
|
|
|
}
|
|
|
|
|
2017-04-09 20:57:32 +00:00
|
|
|
class FileBackend {
|
|
|
|
constructor(config) {
|
|
|
|
this.config = config;
|
|
|
|
}
|
|
|
|
|
|
|
|
find(tag, callback) {
|
2017-04-11 03:08:59 +00:00
|
|
|
glob(this.config.media_path + '/' + tag + ' - *.mp3', (err, files) => {
|
2017-04-09 20:57:32 +00:00
|
|
|
if (files.length > 0) {
|
2017-04-11 22:58:43 +00:00
|
|
|
return callback(pickRandom(files));
|
2017-04-09 20:57:32 +00:00
|
|
|
}
|
2017-04-10 02:05:52 +00:00
|
|
|
callback();
|
2017-04-09 20:57:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = function(config) {
|
|
|
|
return new FileBackend(config);
|
|
|
|
};
|
|
|
|
|
|
|
|
// vim:ts=2 sw=2 et:
|