Skip to content
26 changes: 21 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,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') {
Expand Down Expand Up @@ -164,7 +165,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);
});
});
};
Expand All @@ -174,7 +175,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
Expand All @@ -201,7 +202,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
Expand Down Expand Up @@ -260,7 +262,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 = '<ul id="files" class="view-' + escapeHtml(view) + '">'
+ (view === 'details' ? (
'<li class="header">'
Expand Down Expand Up @@ -301,6 +303,20 @@ function createHtmlFileList(files, dir, useIcons, view) {
? file.stat.size
: '';

if(simplifyFileSize && size !== ''){

const fileSizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
var index = 0
if (size !== 0){
index = Math.floor(Math.log(size)/Math.log(1000))
if ( index > size.length - 1){
index = 5
}
}

size = size / Math.pow(1000,index) + ' ' + fileSizes[index]
}

return '<li><a href="'
+ escapeHtml(normalizeSlashes(normalize(path.join('/'))))
+ '" class="' + escapeHtml(classes.join(' ')) + '"'
Expand Down Expand Up @@ -328,7 +344,7 @@ function createHtmlRender(template) {

var body = str
.replace(/\{style\}/g, locals.style.concat(iconStyle(locals.fileList, locals.displayIcons)))
.replace(/\{files\}/g, createHtmlFileList(locals.fileList, locals.directory, locals.displayIcons, locals.viewName))
.replace(/\{files\}/g, createHtmlFileList(locals.fileList, locals.directory, locals.displayIcons, locals.viewName, locals.simplifyFileSize))
.replace(/\{directory\}/g, escapeHtml(locals.directory))
.replace(/\{linked-path\}/g, htmlPath(locals.directory));

Expand Down