From ff8471ffa1351ac4153c88cf2cc530f5d7b0a6ed Mon Sep 17 00:00:00 2001 From: Cam Wiegert Date: Tue, 10 Nov 2015 11:22:18 -0600 Subject: [PATCH] Add opts.showDotfiles --- README.md | 4 ++++ lib/ecstatic/aliases.json | 1 + lib/ecstatic/defaults.json | 1 + lib/ecstatic/opts.js | 9 +++++++++ lib/ecstatic/showdir.js | 9 +++++++++ 5 files changed, 24 insertions(+) diff --git a/README.md b/README.md index 1186081..ef11bb4 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,10 @@ If it is a function, it will be executed on every request, and passed the pathna Turn **off** directory listings with `opts.showDir === false`. Defaults to **true**. +### `opts.showDotfiles` + +Exclude dotfiles from directory listings with `opts.showDotfiles === false`. Defaults to **true**. + ### `opts.humanReadable` If showDir is enabled, add human-readable file sizes. Defaults to **true**. diff --git a/lib/ecstatic/aliases.json b/lib/ecstatic/aliases.json index 4b0ac66..0ef56a1 100644 --- a/lib/ecstatic/aliases.json +++ b/lib/ecstatic/aliases.json @@ -1,6 +1,7 @@ { "autoIndex": [ "autoIndex", "autoindex" ], "showDir": [ "showDir", "showdir" ], + "showDotfiles": ["showDotfiles", "showdotfiles"], "humanReadable": [ "humanReadable", "humanreadable", "human-readable" ], "si": [ "si", "index" ], "handleError": [ "handleError", "handleerror" ], diff --git a/lib/ecstatic/defaults.json b/lib/ecstatic/defaults.json index d26b553..501f31a 100644 --- a/lib/ecstatic/defaults.json +++ b/lib/ecstatic/defaults.json @@ -1,6 +1,7 @@ { "autoIndex": true, "showDir": true, + "showDotfiles": true, "humanReadable": true, "si": false, "cache": "max-age=3600", diff --git a/lib/ecstatic/opts.js b/lib/ecstatic/opts.js index dc33bd4..ea080a6 100644 --- a/lib/ecstatic/opts.js +++ b/lib/ecstatic/opts.js @@ -6,6 +6,7 @@ var aliases = require('./aliases.json') module.exports = function (opts) { var autoIndex = defaults.autoIndex, showDir = defaults.showDir, + showDotfiles = defaults.showDotfiles, humanReadable = defaults.humanReadable, si = defaults.si, cache = defaults.cache, @@ -39,6 +40,13 @@ module.exports = function (opts) { } }); + aliases.showDotfiles.some(function (k) { + if (isDeclared(k)) { + showDotfiles = opts[k]; + return true; + } + }); + aliases.humanReadable.some(function (k) { if (isDeclared(k)) { humanReadable = opts[k]; @@ -154,6 +162,7 @@ module.exports = function (opts) { cache: cache, autoIndex: autoIndex, showDir: showDir, + showDotfiles: showDotfiles, humanReadable: humanReadable, si: si, defaultExt: defaultExt, diff --git a/lib/ecstatic/showdir.js b/lib/ecstatic/showdir.js index f9fa5f9..dbe0feb 100644 --- a/lib/ecstatic/showdir.js +++ b/lib/ecstatic/showdir.js @@ -13,6 +13,7 @@ module.exports = function (opts, stat) { baseDir = opts.baseDir, humanReadable = opts.humanReadable, handleError = opts.handleError, + showDotfiles = opts.showDotfiles, si = opts.si, weakEtags = opts.weakEtags; @@ -40,6 +41,14 @@ module.exports = function (opts, stat) { if (err) { return handleError ? status[500](res, next, { error: err }) : next(); } + + // Optionally exclude dotfiles from directory listing. + if (!showDotfiles) { + files = files.filter(function(filename){ + return filename.slice(0,1) !== '.'; + }); + } + res.setHeader('content-type', 'text/html'); res.setHeader('etag', etag(stat, weakEtags)); res.setHeader('last-modified', (new Date(stat.mtime)).toUTCString());