Skip to content

Commit

Permalink
final piece of the puzzle for Issue #6, support different files with …
Browse files Browse the repository at this point in the history
…simalar file names (by using unique ids)
  • Loading branch information
nickdesaulniers committed Nov 19, 2012
1 parent b150686 commit 9ccd68c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 67 deletions.
2 changes: 1 addition & 1 deletion app.js
Expand Up @@ -30,7 +30,7 @@ app.configure('development', function(){
});

app.get('/', routes.index);
app.get('/transcode/:extension/:filename', routes.transcode);
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'));
Expand Down
13 changes: 7 additions & 6 deletions lib/filemap.js
Expand Up @@ -17,14 +17,15 @@ var FileMap = {
retrieveAll: function () {
return file_dict;
},
store: function (path) {
// TODO
previously_transcoded: function (file_path) {
var lib_file_path = 'library/' + file_path;
return fs.existsSync(lib_file_path) ? lib_file_path : '';
},
update: function () {
shell.find(music_folders).filter(function (file_name) {
return supported_extension_re.test(file_name) && fs.existsSync(file_name);
}).forEach(function (file_name) {
file_dict[fs.statSync(file_name).ino] = file_name;
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;
});
}
};
Expand Down
9 changes: 4 additions & 5 deletions public/javascripts/myscript.js
Expand Up @@ -42,13 +42,12 @@ function isSupported (extString) {
}

var file_extension_re = /\.([0-9a-z]+)(?:[\?#]|$)/i;
function getSupportedExt (filename) {
var ext = filename.match(file_extension_re)[1];
function getSupportedExt (ext) {
return isSupported(ext) ? ext : preferred_extension;
}

function getAudioSrc (filename) {
return '/transcode/' + getSupportedExt(filename) + '/' + filename;
function getAudioSrc (dataset) {
return '/transcode/' + getSupportedExt(dataset.ext) + '/' + dataset.songid;
}

$(document).ready(function () {
Expand All @@ -68,6 +67,6 @@ $(document).ready(function () {
div.appendChild(audio);
audio.removeEventListener('canplay', listener);
});
audio.src = getAudioSrc(this.dataset.filename);
audio.src = getAudioSrc(this.dataset);
});
});
90 changes: 38 additions & 52 deletions routes/index.js
@@ -1,7 +1,6 @@
var fs = require('fs');
var child_process = require('child_process');
var musicmetadata = require('musicmetadata');
var path = require('path');
var dirs = require('../config/config').music_folders.map(escapejson);
var FileMap = require('../lib/filemap').FileMap;

Expand Down Expand Up @@ -29,12 +28,12 @@ exports.index = function(req, res){
var filename = filemap[id];
var parser = new musicmetadata(fs.createReadStream(filename));
parser.on('metadata', function (result) {
// clean up
// clean up (lots of object modification: bad)
delete result.picture;
result.track = flatten(result.track);
result.disk = flatten(result.disk);
result.filename = path.basename(filename);
//result.songID = id;
result.songID = id;
result.ext = filename.replace(/.+\./, '');

files.push(result);

Expand All @@ -50,10 +49,31 @@ exports.index = function(req, res){
};

exports.transcode = function (req, res) {
var child = null;
var actualFile = req.params.filename;
var requestedFile = actualFile.replace(supported_extension_re, '.' +
req.params.extension);
// Check if original file ext is in filemap
var file_path = FileMap.retrieve(req.params.fileID);
if (!file_path) {
console.log('original file was not in filemap');
return res.send(404);
}

// was the requested extension the file's current extension?
var requested_extension = req.params.extension;
var actual_extension = file_path.replace(/.+\./, '');
if (requested_extension === actual_extension) {
console.log('The file\'s actual extension was requested');
return res.sendfile(file_path);
}

// 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);
if (previous_file_path) {
console.log('Found previously encoded version in library/');
return res.sendfile(previous_file_path);
}

// Transcoding needed
console.log('Transcoding ' + actual_extension + ' to ' + requested_extension);

// encoding library
// ogg -> libvorbis
Expand All @@ -68,49 +88,15 @@ exports.transcode = function (req, res) {
case 'mp3': encoder = ' -acodec libmp3lame'; break;
}

var find_command = 'find ' + dirs.map(escapeshell).join(' ') + ' -name ' +
escapeshell(actualFile);
var transcode_command = find_command + ' -print0 | ' +
'xargs -0 -J actualFile ffmpeg -i actualFile' + encoder + ' -map 0:0 ' +
'library/' + escapeshell(requestedFile);
var chosen_command = transcode_command;
var needsTranscoding = true;

console.log('Requested File: ' + requestedFile);
console.log('Actual File: ' + actualFile);

// Check if the file has been transcoded, doesn't need to, or does
fs.exists('library/' + requestedFile, function (exists) {
if (exists) {
console.log('found ' + requestedFile + ' in library/');
res.sendfile('library/' + requestedFile);
console.log('sent');
} else {
console.log('did not find ' + requestedFile + ' in library/');

// Check if file doesn't need transcoding
if (actualFile === requestedFile) {
chosen_command = find_command;
needsTranscoding = false;
}

console.log(needsTranscoding ? 'Transcoding' : 'Symlinking');
child = child_process.exec(chosen_command,
function (error, stdout, stderr) {
var filename;
if (error) return console.log(error);
//if (stderr) return console.log(stderr);

// Symlink it!
if (!needsTranscoding) {
filepath = stdout.split('\n')[0];
fs.symlinkSync(filepath, 'library/' + requestedFile);
}

res.sendfile('library/' + requestedFile);
console.log('sent');
child.kill('SIGTERM');
});
}
var command =
'ffmpeg -i ' + escapeshell(file_path) + encoder + ' -map 0:0 library/' +
previous_file_name;

child = child_process.exec(command, function (error, stdout, stderr) {
if (error) return console.log(error);
//if (stderr) return console.log(stderr);
console.log('transcoded');
res.sendfile('library/' + previous_file_name);
child.kill('SIGTERM');
});
}
6 changes: 3 additions & 3 deletions views/index.ejs
Expand Up @@ -18,7 +18,7 @@
<thead>
<tr>
<% for (var key in files[0]) { %>
<% if (files[0].hasOwnProperty(key) && key !== 'filename') { %>
<% if (key !== 'songID' && key !== 'ext') { %>
<th><%= key %></th>
<% } %>
<% } %>
Expand All @@ -28,10 +28,10 @@
<% for (var i = 0, len = files.length; i < len; i++) { %>
<tr>
<% for (var key in files[i]) { %>
<% if (files[i].hasOwnProperty(key) && key !== 'filename') { %>
<% if (key !== 'songID' && key !== 'ext') { %>
<td>
<% if (key === 'title') { %>
<a class="song_title" href="#" data-filename="<%= files[i].filename %>">
<a class="song_title" href="#" data-songid="<%= files[i].songID %>" data-ext="<%= files[i].ext %>">
<% } %>
<%= files[i][key] %>
<% if (key === 'title') { %>
Expand Down

0 comments on commit 9ccd68c

Please sign in to comment.