Permalink
Browse files

directory middleware: Added directory icon only if stat is called

  • Loading branch information...
1 parent 57dc27c commit 839325fa0e2729364e62ae8b7c2a775706c9717b @langpavel committed Nov 13, 2012
Showing with 37 additions and 17 deletions.
  1. +37 −17 lib/middleware/directory.js
  2. BIN lib/public/icons/directory.png
@@ -38,6 +38,7 @@ var cache = {};
* - `hidden` display hidden (dot) files. Defaults to false.
* - `icons` display icons. Defaults to false.
* - `filter` Apply this filter function to files. Defaults to false.
+ * - 'stat' Call stat on each entry. Defaults to false. Sort strategy can set this to true.
* - `sort` Apply sort strategy. Defaults to alphabetical.
* Can be string or function passable to Array#sort.
* If provided function has needStat property, result of `fs.stat`
@@ -59,6 +60,7 @@ exports = module.exports = function directory(root, options){
, icons = options.icons
, filter = options.filter
, sort = options.sort
+ , doStat = options.stat
, root = normalize(root);
if (sort && typeof sort !== 'function') {
@@ -69,6 +71,8 @@ exports = module.exports = function directory(root, options){
'\nor custom function');
}
+ doStat = doStat || (sort && sort.needStat);
+
return function directory(req, res, next) {
if ('GET' != req.method && 'HEAD' != req.method) return next();
@@ -105,21 +109,27 @@ exports = module.exports = function directory(root, options){
// content-negotiation
for (var key in exports) {
if (~accept.indexOf(key) || ~accept.indexOf('*/*')) {
- if (sort && sort.needStat) {
+ if (doStat) {
var pending = files.length;
var stats = [];
files.forEach(function(name) {
fs.stat(join(path, name), function(err, stat) {
if(stat) {
stat.name = name;
stats.push(stat);
+ } else {
+ stats.push({
+ name: name,
+ error: err
+ });
}
// finis the job if after all files stated
pending--;
if (pending === 0) {
- stats.sort(sort);
+ if(sort && sort.needStat)
+ stats.sort(sort);
files = stats.map(function(stat) { return stat.name; });
- exports[key](req, res, files, next, originalDir, showUp, icons);
+ exports[key](req, res, files, next, originalDir, showUp, icons, stats);
}
});
});
@@ -141,15 +151,18 @@ exports = module.exports = function directory(root, options){
* Respond with text/html.
*/
-exports.html = function(req, res, files, next, dir, showUp, icons){
+exports.html = function(req, res, files, next, dir, showUp, icons, stats){
fs.readFile(__dirname + '/../public/directory.html', 'utf8', function(err, str){
if (err) return next(err);
fs.readFile(__dirname + '/../public/style.css', 'utf8', function(err, style){
if (err) return next(err);
- if (showUp) files.unshift('..');
+ if (showUp) {
+ files.unshift('..');
+ if(stats) stats.unshift({ name: '..' });
+ }
str = str
.replace('{style}', style)
- .replace('{files}', html(files, dir, icons))
+ .replace('{files}', html(files, dir, icons, stats))
.replace('{directory}', dir)
.replace('{linked-path}', htmlPath(dir));
res.setHeader('Content-Type', 'text/html');
@@ -213,25 +226,31 @@ function htmlPath(dir) {
* Map html `files`, returning an html unordered list.
*/
-function html(files, dir, useIcons) {
- return '<ul id="files">' + files.map(function(file){
- var icon = ''
+function html(files, dir, useIcons, stats) {
+ var sb = [];
+ sb.push('<ul id="files">\n');
+ for(var i = 0, l = files.length; i < l; i++) {
+ var file = files[i]
+ , icon = ''
, classes = [];
if (useIcons && '..' != file) {
icon = icons[extname(file)] || icons.default;
+ if (stats && stats[i].isDirectory && stats[i].isDirectory())
+ icon = icons.directory;
icon = '<img src="data:image/png;base64,' + load(icon) + '" />';
classes.push('icon');
}
- return '<li><a href="'
- + join(dir, file)
- + '" class="'
- + classes.join(' ') + '"'
- + ' title="' + file + '">'
- + icon + file + '</a></li>';
-
- }).join('\n') + '</ul>';
+ sb.push('<li><a href="'
+ , join(dir, file)
+ , '" class="'
+ , classes.join(' '), '"'
+ , ' title="', file, '">'
+ , icon + file + '</a></li>\n');
+ }
+ sb.push('</ul>');
+ return sb.join('');
}
/**
@@ -276,5 +295,6 @@ var icons = {
, '.cpp': 'page_white_cplusplus.png'
, '.swf': 'page_white_flash.png'
, '.pdf': 'page_white_acrobat.png'
+ , 'directory': 'directory.png'
, 'default': 'page_white.png'
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 839325f

Please sign in to comment.