Skip to content

Commit

Permalink
do a faster mime lookup based on extension if possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
John Reeves committed Jan 15, 2012
1 parent f0f2e63 commit 1d6b0ba
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -6,7 +6,8 @@
"dependencies": {
"LazyBoy": "0.1.6",
"underscore": "1.1.6",
"mime-magic": "0.2.3"
"mime-magic": "0.2.3",
"mime": "1.2.4"
}
}

34 changes: 28 additions & 6 deletions src/commands.js
Expand Up @@ -3,7 +3,8 @@ var child_process = require( 'child_process' ),
exec = child_process.exec,
pathmod = require( 'path' ),
fs = require( 'fs' ),
mime = require( 'mime-magic' ),
mimeMagic = require( 'mime-magic' ),
mime = require( 'mime' ),
conf = require( '../config' );

var commands = {
Expand Down Expand Up @@ -55,7 +56,7 @@ var commands = {
finfo.type = 'dir';
} else if ( stats.isFile() ) {
//TODO: get specific type
mime.fileWrapper( abs + '/' + f, function( err, mt )
mimeLookup( abs + '/' + f, function( mt )
{
finfo.type = mt;
fileMapped();
Expand Down Expand Up @@ -83,10 +84,7 @@ var commands = {
return handleError( {error: 'Not a file'}, done );
}

mime.fileWrapper( abs, function( err, mt ) {
if ( err ) {
return handleError( err, done );
}
mimeLookup( abs, function( mt ) {

self.res.writeHead(200, {
'Content-Type': mt,
Expand Down Expand Up @@ -120,4 +118,28 @@ function handleError( err, done )
done( {error: err} );
}

function mimeLookup( path, done ) {
var tp = mime.lookup( path, 'unknown' );
if ( tp !== 'unknown' ) {
_.defer( function(){
done( tp );
} );
return;
}

console.log( 'resorting to slow lookup: ', path );
mimeMagic.fileWrapper( path, function( err, mt ) {
if ( err ) {
done( tp );
} else {
done( mt );
}
});
}

mime.define( {
'application/x-gzip': ['gz', 'tgz'],
'text/sql': ['sql']
});

module.exports = commands;
5 changes: 3 additions & 2 deletions www/js/app.js
Expand Up @@ -25,7 +25,8 @@ $(function()
var node = $(ev.currentTarget);
var path = node.attr( 'href' ).substr( '#files/'.length );

path = encodeURI( path.substr( 0, path.length - 1 ) );
//path = encodeURI( path.substr( 0, path.length - 1 ) );
path = encodeURI( path );

if ( node.hasClass( 'type-file' ) ) {
window.location = '/v/get/' + path;
Expand Down Expand Up @@ -55,7 +56,7 @@ $(function()
$('<li/>')
.append( $('<a/>')
.addClass( f.type === 'dir' ? 'type-dir' : 'type-file' )
.attr( 'href', '#files/' + path + '/' + f.name + '/' )
.attr( 'href', '#files/' + path + '/' + f.name )
.text( f.name ) )
.appendTo( ul );
});
Expand Down

0 comments on commit 1d6b0ba

Please sign in to comment.