diff --git a/lib/middleware/directory.js b/lib/middleware/directory.js index 9b5cc918d..ac8ea7a5c 100644 --- a/lib/middleware/directory.js +++ b/lib/middleware/directory.js @@ -70,6 +70,7 @@ exports = module.exports = function directory(root, options){ if (!root) throw new Error('directory() root path required'); var hidden = options.hidden , icons = options.icons + , view = options.view || 'tiles' , filter = options.filter , root = normalize(root + sep); @@ -109,7 +110,7 @@ exports = module.exports = function directory(root, options){ // not acceptable if (!type) return next(utils.error(406)); - exports[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path); + exports[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, view); }); }); }; @@ -119,7 +120,7 @@ exports = module.exports = function directory(root, options){ * Respond with text/html. */ -exports.html = function(req, res, files, next, dir, showUp, icons, path){ +exports.html = function(req, res, files, next, dir, showUp, icons, path, view){ 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){ @@ -130,8 +131,8 @@ exports.html = function(req, res, files, next, dir, showUp, icons, path){ files.sort(fileSort); if (showUp) files.unshift({ name: '..' }); str = str - .replace('{style}', style) - .replace('{files}', html(files, dir, icons)) + .replace('{style}', style.concat(iconStyle(files, icons))) + .replace('{files}', html(files, dir, icons, view)) .replace('{directory}', dir) .replace('{linked-path}', htmlPath(dir)); res.setHeader('Content-Type', 'text/html'); @@ -185,32 +186,80 @@ function htmlPath(dir) { }).join(' / '); } +/** + * Load icon images, return css string. + */ + +function iconStyle (files, useIcons) { + if (!useIcons) return ''; + var data = {}; + var views = { tiles: [], details: [], mobile: [] }; + + for (var i=0; i < files.length; i++) { + var file = files[i]; + if (file.name == '..') continue; + + var isDir = '..' == file.name || (file.stat && file.stat.isDirectory()); + var icon = isDir ? icons.folder : icons[extname(file.name)] || icons.default; + + var ext = extname(file.name); + ext = isDir ? '.directory' : (icons[ext] ? ext : '.default'); + + if (data[icon]) continue; + data[icon] = ext + ' .name{background-image: url(data:image/png;base64,' + load(icon)+');}'; + views.tiles.push('.view-tiles ' + data[icon]); + views.details.push('.view-details ' + data[icon]); + views.mobile.push('#files ' + data[icon]); + } + + var style = views.tiles.join('\n') + + '\n'+views.details.join('\n') + + '\n@media (max-width: 768px) {\n\t' + + views.mobile.join('\n\t') + + '\n}'; + return style; +} + /** * Map html `files`, returning an html unordered list. */ -function html(files, dir, useIcons) { - return '