Skip to content

Commit

Permalink
look up metadata once then start server Fixes #33
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Desaulniers committed Jan 3, 2014
1 parent 94553d7 commit 51974e8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 54 deletions.
12 changes: 8 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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'));
});
});

46 changes: 29 additions & 17 deletions lib/filemap.js
Original file line number Diff line number Diff line change
@@ -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;

38 changes: 5 additions & 33 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -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);
}

Expand All @@ -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);
Expand Down

0 comments on commit 51974e8

Please # to comment.