From 6a26b8ec0236de87ba196b317a7cf467e8d7987b Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 13 Aug 2018 00:04:58 -0600 Subject: [PATCH 1/3] expose fs.stat of root and files to all template functions --- index.js | 90 +++++++++++++++++++++++++++++----------------------- test/test.js | 18 +++++------ 2 files changed, 59 insertions(+), 49 deletions(-) diff --git a/index.js b/index.js index e707bbc5..885690dd 100644 --- a/index.js +++ b/index.js @@ -154,13 +154,38 @@ function serveIndex(root, options) { }); files.sort(); + // add parent directory as first + if (showUp) { + files.unshift('..'); + } + // content-negotiation var accept = accepts(req); var type = accept.type(mediaTypes); // not acceptable if (!type) return next(createError(406)); - serveIndex[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, view, template, stylesheet); + + // stat all files + fstat(path, files, function (err, stats) { + if (err) return next(err); + + // combine the stats into the file list + var fileList = files.map(function (file, i) { + return { name: file, stat: stats[i] }; + }); + + // sort file list + fileList.sort(fileSort); + + // make similar to file object (with stat) + var directory = { + name: originalDir, + stat: stat + } + + serveIndex[mediaType[type]](req, res, directory, fileList, next, showUp, icons, path, view, template, stylesheet); + }); }); }); }; @@ -170,46 +195,29 @@ function serveIndex(root, options) { * Respond with text/html. */ -serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path, view, template, stylesheet) { +serveIndex.html = function _html(req, res, directory, files, next, showUp, icons, path, view, template, stylesheet) { var render = typeof template !== 'function' ? createHtmlRender(template) : template - if (showUp) { - files.unshift('..'); - } - - // stat all files - stat(path, files, function (err, stats) { + // read stylesheet + fs.readFile(stylesheet, 'utf8', function (err, style) { if (err) return next(err); - // combine the stats into the file list - var fileList = files.map(function (file, i) { - return { name: file, stat: stats[i] }; - }); - - // sort file list - fileList.sort(fileSort); + // create locals for rendering + var locals = { + directory: directory.name, + displayIcons: Boolean(icons), + fileList: files, + path: path, + style: style, + viewName: view + }; - // read stylesheet - fs.readFile(stylesheet, 'utf8', function (err, style) { + // render html + render(locals, function (err, body) { if (err) return next(err); - - // create locals for rendering - var locals = { - directory: dir, - displayIcons: Boolean(icons), - fileList: fileList, - path: path, - style: style, - viewName: view - }; - - // render html - render(locals, function (err, body) { - if (err) return next(err); - send(res, 'text/html', body) - }); + send(res, 'text/html', body) }); }); }; @@ -218,24 +226,26 @@ serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path * Respond with application/json. */ -serveIndex.json = function _json(req, res, files) { +serveIndex.json = function _json(req, res, directory, nodes) { + var files = nodes.map(function (file) { return file.name }) send(res, 'application/json', JSON.stringify(files)) -}; +} /** * Respond with text/plain. */ -serveIndex.plain = function _plain(req, res, files) { +serveIndex.plain = function _plain(req, res, directory, nodes) { + var files = nodes.map(function (file) { return file.name }) send(res, 'text/plain', (files.join('\n') + '\n')) -}; +} /** * Map html `files`, returning an html unordered list. * @private */ -function createHtmlFileList(files, dir, useIcons, view) { +function createHtmlFileList(files, dirname, useIcons, view) { var html = '