Skip to content

Add simplifyFileSize option #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 12 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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);
});
});
};
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 = '<ul id="files" class="view-' + escapeHtml(view) + '">'
+ (view === 'details' ? (
'<li class="header">'
Expand Down Expand Up @@ -301,6 +304,10 @@ function createHtmlFileList(files, dir, useIcons, view) {
? file.stat.size
: '';

if(simplifyFileSize && size !== ''){
size = bytes(size, {unitSeparator: ' '})
}

return '<li><a href="'
+ escapeHtml(normalizeSlashes(normalize(path.join('/'))))
+ '" class="' + escapeHtml(classes.join(' ')) + '"'
Expand Down Expand Up @@ -328,7 +335,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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dependencies": {
"accepts": "~1.3.7",
"batch": "0.6.1",
"bytes": "~3.1.2",
"debug": "2.6.9",
"escape-html": "~1.0.3",
"http-errors": "~1.8.0",
Expand Down Expand Up @@ -37,4 +38,4 @@
"test-ci": "nyc --reporter=lcov --reporter=text npm test",
"test-cov": "nyc --reporter=html --reporter=text npm test"
}
}
}
1 change: 1 addition & 0 deletions test/fixtures/collect/sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
27 changes: 27 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,33 @@ describe('serveIndex(root)', function () {
});
});

describe('with "simplifyFileSize" option', function () {
it('should simplify file sizes', function (done) {
var server = createServer(fixtures, {'simplifyFileSize': true})

request(server)
.get('/collect')
.expect(/1\.07 KB/)
.expect(200, done)
});
it('should not simplify file sizes', function (done) {
var server = createServer(fixtures, {'simplifyFileSize': false})

request(server)
.get('/collect')
.expect(/1100/)
.expect(200, done)
});
it('should not simplify file sizes by default', function (done) {
var server = createServer(fixtures)

request(server)
.get('/collect')
.expect(/1100/)
.expect(200, done)
});
});

describe('with "template" option', function () {
describe('when setting a custom template file', function () {
var server;
Expand Down
Loading