From 51974e8c2e540f159b50f8563ad6690d7c10b314 Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Fri, 3 Jan 2014 15:56:11 -0500 Subject: [PATCH] look up metadata once then start server Fixes #33 --- app.js | 12 ++++++++---- lib/filemap.js | 46 +++++++++++++++++++++++++++++----------------- routes/index.js | 38 +++++--------------------------------- 3 files changed, 42 insertions(+), 54 deletions(-) diff --git a/app.js b/app.js index f7f271b..e23011f 100644 --- a/app.js +++ b/app.js @@ -2,6 +2,7 @@ var express = require('express'); var http = require('http'); var routes = require('./routes'); var config = require('./config/config'); +var FileMap = require('./lib/filemap'); var app = express(); app.configure(function () { @@ -17,10 +18,13 @@ app.configure(function () { app.use(express.static(__dirname + '/public')); }); -app.get('/list', routes.list); -app.get('/transcode/:extension/:fileID', routes.transcode); +FileMap.update(function () { + routes.FileMap = FileMap; + app.get('/list', routes.list); + app.get('/transcode/:extension/:fileID', routes.transcode); -http.createServer(app).listen(app.get('port'), function () { - console.log('Express server listening on port ' + app.get('port')); + http.createServer(app).listen(app.get('port'), function () { + console.log('Express server listening on port ' + app.get('port')); + }); }); diff --git a/lib/filemap.js b/lib/filemap.js index 1f0a524..6ec63a8 100644 --- a/lib/filemap.js +++ b/lib/filemap.js @@ -1,35 +1,47 @@ var shell = require('shelljs'); var fs = require('fs'); +var Metalib = require('fluent-ffmpeg').Metadata; var music_folders = require('../config/config.json').music_folders .map(escapejson); var supported_extension_re = /\.(mp3|ogg|wav|m4a|flac)$/; var file_dict = {}; +var file_metadata = []; -function escapejson (filename) { - return filename.replace(/\\/g, ''); -}; +function escapejson (filename) { return filename.replace(/\\/g, ''); }; -var FileMap = { - retrieve: function (id) { - return file_dict[id]; - }, - retrieveAll: function () { - return file_dict; - }, +module.exports = { + retrieve: function (id) { return file_dict[id]; }, + retrieveAll: function () { return file_metadata; }, previously_transcoded: function (file_path) { var lib_file_path = 'library/' + file_path; return fs.existsSync(lib_file_path) ? lib_file_path : ''; }, - update: function () { + update: function (cb) { + var counter = file_metadata.length = 0; shell.find(music_folders).filter(function (file_path) { return supported_extension_re.test(file_path) && fs.existsSync(file_path); - }).forEach(function (file_path) { - file_dict[fs.statSync(file_path).ino] = file_path; + }).forEach(function (file_path, i, all_songs) { + if (i === 0) console.log('Parsing ' + all_songs.length + ' songs.'); + var songID = fs.statSync(file_path).ino; + file_dict[songID] = file_path; + new Metalib(file_path, function (metadata, err) { + if (err) return console.error(err); + file_metadata.push({ + songID: songID, + ext: file_path.replace(/.+\./, ''), + title: metadata.title || file_path.replace(/.+\//, ''), + artist: metadata.artist, + album: metadata.album, + year: metadata.date, + track: metadata.track, + duration: metadata.durationraw, + }); + if (++counter === all_songs.length && typeof cb === 'function') { + console.log('Done parsing.'); + cb(); + } + }); }); } }; -FileMap.update(); - -exports.FileMap = FileMap; - diff --git a/routes/index.js b/routes/index.js index 6013a66..869da37 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,41 +1,13 @@ var fs = require('fs'); -var Metalib = require('fluent-ffmpeg').Metadata; -var FileMap = require('../lib/filemap').FileMap; var convert = require('../lib/convert'); -exports.list = function (req, res){ - var filemap = FileMap.retrieveAll(); - var filemapKeys = Object.keys(filemap); - var files = []; - - filemapKeys.forEach(function (id) { - var filename = filemap[id]; - new Metalib(filename, function (metadata, err) { - if (err) return console.error(err); - files.push({ - songID: id, - ext: filename.replace(/.+\./, ''), - title: metadata.title || filename.replace(/.+\//, ''), - artist: metadata.artist, - album: metadata.album, - year: metadata.date, - track: metadata.track, - duration: metadata.durationraw, - }); - - // On the last file - if (files.length === filemapKeys.length) { - res.send(files); - } - }); - }); -}; - +exports.FileMap = null; +exports.list = function (req, res) { res.send(exports.FileMap.retrieveAll()); }; exports.transcode = function (req, res) { // Check if original file ext is in filemap - var file_path = FileMap.retrieve(req.params.fileID); + var file_path = exports.FileMap.retrieve(req.params.fileID); if (!file_path) { - console.log('original file was not in filemap'); + console.log('Original file was not in filemap'); return res.send(404); } @@ -49,7 +21,7 @@ exports.transcode = function (req, res) { // Was the requested extension previously encoded? var previous_file_name = req.params.fileID + '.' + requested_extension; - var previous_file_path = FileMap.previously_transcoded(previous_file_name); + var previous_file_path = exports.FileMap.previously_transcoded(previous_file_name); if (previous_file_path) { console.log('Found previously encoded version in library/'); return res.sendfile(previous_file_path);