diff --git a/HISTORY.md b/HISTORY.md index 3f5c295..106a36c 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,7 @@ unreleased ========== - + * Added `simplifyFileSize` option + * deps: bytes@~3.1.2 * Fix icons for MIME types with `+` * Fix text and json responses to match html sorting * Show font icon for more font types diff --git a/README.md b/README.md index f720b0d..9b0a266 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,16 @@ provided locals: Display mode. `tiles` and `details` are available. Defaults to `tiles`. +##### simplifyFileSize + +Configures the unit in the size. Defaults to `false`. + +By default, the size is display only in bytes. + +Set it to `true` and it will try to automatically simplify the size to +the highest possible unit from the following to two decimal places. +- KB, MB, GB, TB, PB + ## Examples ### Serve directory indexes with vanilla node.js http server diff --git a/index.js b/index.js index e95390e..d5285da 100644 --- a/index.js +++ b/index.js @@ -27,6 +27,7 @@ var Batch = require('batch'); var mime = require('mime-types'); var parseUrl = require('parseurl'); var resolve = require('path').resolve; +var bytes = require('bytes'); /** * Module exports. @@ -97,6 +98,7 @@ function serveIndex(root, options) { var stylesheet = opts.stylesheet || defaultStylesheet; var template = opts.template || defaultTemplate; var view = opts.view || 'tiles'; + var simplifyFileSize = opts.simplifyFileSize; return function (req, res, next) { if (req.method !== 'GET' && req.method !== 'HEAD') { @@ -164,7 +166,7 @@ function serveIndex(root, options) { // not acceptable if (!type) return next(createError(406)); - serveIndex[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, view, template, stylesheet); + serveIndex[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, view, template, stylesheet, simplifyFileSize); }); }); }; @@ -174,7 +176,7 @@ 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, files, next, dir, showUp, icons, path, view, template, stylesheet, simplifyFileSize) { var render = typeof template !== 'function' ? createHtmlRender(template) : template @@ -201,7 +203,8 @@ serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path fileList: fileList, path: path, style: style, - viewName: view + viewName: view, + simplifyFileSize:Boolean(simplifyFileSize) }; // render html @@ -260,7 +263,7 @@ serveIndex.plain = function _plain (req, res, files, next, dir, showUp, icons, p * @private */ -function createHtmlFileList(files, dir, useIcons, view) { +function createHtmlFileList(files, dir, useIcons, view, simplifyFileSize) { var html = '