From c3bd5e0053bcc08010775577c02dfff5f33c52e6 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 3 Dec 2013 07:39:02 -0800 Subject: [PATCH] Upgraded Filesize.js to 2.0.0 Fixed: File size/disk space in UI now shows actual size instead of manufacturer size (111 GB instead of 120 GB) --- src/UI/JsLibraries/filesize.js | 256 ++++++++++++++++----------------- src/UI/Shared/FormatHelpers.js | 2 +- 2 files changed, 123 insertions(+), 135 deletions(-) diff --git a/src/UI/JsLibraries/filesize.js b/src/UI/JsLibraries/filesize.js index b6f7b24ab..74f600064 100644 --- a/src/UI/JsLibraries/filesize.js +++ b/src/UI/JsLibraries/filesize.js @@ -6,148 +6,136 @@ * @license BSD-3 * @link http://filesizejs.com * @module filesize - * @version 1.10.0 + * @version 2.0.0 */ ( function ( global ) { - "use strict"; +"use strict"; - var base = 10, - right = /\.(.*)/, - bit = /b$/, - bite = /^B$/, - zero = /^0$/, - options; +var bit = /b$/, + bite = /^B$/, + radix = 10, + right = /\.(.*)/, + zero = /^0$/; - options = { - all : { - increments : [["B", 1], ["kb", 125], ["kB", 1000], ["Mb", 125000], ["MB", 1000000], ["Gb", 125000000], ["GB", 1000000000], ["Tb", 125000000000], ["TB", 1000000000000], ["Pb", 125000000000000], ["PB", 1000000000000000]], - nth : 11 - }, - bitless : { - increments : [["B", 1], ["kB", 1000], ["MB", 1000000], ["GB", 1000000000], ["TB", 1000000000000], ["PB", 1000000000000000]], - nth : 6 - } - }; +/** + * filesize + * + * @method filesize + * @param {Mixed} arg String, Int or Float to transform + * @param {Object} descriptor [Optional] Flags + * @return {String} Readable file size String + */ +function filesize ( arg, descriptor ) { + var result = "", + skip = false, + i = 6, + base, bits, neg, num, round, size, sizes, unix, spacer, suffix, z; - /** - * filesize - * - * @param {Mixed} arg String, Int or Float to transform - * @param {Mixed} pos [Optional] Position to round to, defaults to 2 if shrt is ommitted, or `true` for shrthand output - * @param {Boolean} bits [Optional] Determines if `bit` sizes are used for result calculation, default is true - * @return {String} Readable file size String - */ - function filesize ( arg) { - var result = "", - bits = true, - skip = false, - i, neg, num, pos, shrt, size, sizes, suffix, z; - - // Determining arguments - if (arguments[3] !== undefined) { - pos = arguments[1]; - shrt = arguments[2]; - bits = arguments[3]; - } - else { - typeof arguments[1] === "boolean" ? shrt = arguments[1] : pos = arguments[1]; - - if ( typeof arguments[2] === "boolean" ) { - bits = arguments[2]; - } - } - - if ( isNaN( arg ) || ( pos !== undefined && isNaN( pos ) ) ) { - throw new Error("Invalid arguments"); - } - - shrt = ( shrt === true ); - bits = ( bits === true ); - pos = shrt ? 1 : ( pos === undefined ? 2 : parseInt( pos, base ) ); - num = Number( arg ); - neg = ( num < 0 ); - - // Flipping a negative number to determine the size - if ( neg ) { - num = -num; - } - - // Zero is now a special case because bytes divide by 1 - if ( num === 0 ) { - if ( shrt ) { - result = "0"; - } - else { - result = "0 B"; - } - } - else { - if ( bits ) { - sizes = options.all.increments; - i = options.all.nth; - } - else { - sizes = options.bitless.increments; - i = options.bitless.nth; - } - - while ( i-- ) { - size = sizes[i][1]; - suffix = sizes[i][0]; - - if ( num >= size ) { - // Treating bytes as cardinal - if ( bite.test( suffix ) ) { - skip = true; - pos = 0; - } - - result = ( num / size ).toFixed( pos ); - - if ( !skip && shrt ) { - if ( bits && bit.test( suffix ) ) { - suffix = suffix.toLowerCase(); - } - - suffix = suffix.charAt( 0 ); - z = right.exec( result ); - - if ( suffix === "k" ) { - suffix = "K"; - } - - if ( z !== null && z[1] !== undefined && zero.test( z[1] ) ) { - result = parseInt( result, base ); - } - - result += suffix; - } - else if ( !shrt ) { - result += " " + suffix; - } - break; - } - } - } - - // Decorating a 'diff' - if ( neg ) { - result = "-" + result; - } - - return result; + if ( isNaN( arg ) ) { + throw new Error( "Invalid arguments" ); } - // CommonJS, AMD, script tag - if ( typeof exports !== "undefined" ) { - module.exports = filesize; + descriptor = descriptor || {}; + bits = ( descriptor.bits === true ); + unix = ( descriptor.unix === true ); + base = descriptor.base !== undefined ? descriptor.base : unix ? 2 : 10; + round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2; + spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " "; + num = Number( arg ); + neg = ( num < 0 ); + + // Flipping a negative number to determine the size + if ( neg ) { + num = -num; } - else if ( typeof define === "function" ) { - define( function () { - return filesize; - }); + + // Zero is now a special case because bytes divide by 1 + if ( num === 0 ) { + if ( unix ) { + result = "0"; + } + else { + result = "0" + spacer + "B"; + } } else { - global.filesize = filesize; + sizes = options[base][bits ? "bits" : "bytes"]; + + while ( i-- ) { + size = sizes[i][1]; + suffix = sizes[i][0]; + + if ( num >= size ) { + // Treating bytes as cardinal + if ( bite.test( suffix ) ) { + skip = true; + round = 0; + } + + result = ( num / size ).toFixed( round ); + + if ( !skip && unix ) { + if ( bits && bit.test( suffix ) ) { + suffix = suffix.toLowerCase(); + } + + suffix = suffix.charAt( 0 ); + z = right.exec( result ); + + if ( !bits && suffix === "k" ) { + suffix = "K"; + } + + if ( z !== null && z[1] !== undefined && zero.test( z[1] ) ) { + result = parseInt( result, radix ); + } + + result += spacer + suffix; + } + else if ( !unix ) { + result += spacer + suffix; + } + + break; + } + } } -})( this ); + + // Decorating a 'diff' + if ( neg ) { + result = "-" + result; + } + + return result; +} + +/** + * Size options + * + * @type {Object} + */ +var options = { + 2 : { + bits : [["B", 1], ["kb", 128], ["Mb", 131072], ["Gb", 134217728], ["Tb", 137438953472], ["Pb", 140737488355328]], + bytes : [["B", 1], ["kB", 1024], ["MB", 1048576], ["GB", 1073741824], ["TB", 1099511627776], ["PB", 1125899906842624]] + }, + 10 : { + bits : [["B", 1], ["kb", 125], ["Mb", 125000], ["Gb", 125000000], ["Tb", 125000000000], ["Pb", 125000000000000]], + bytes : [["B", 1], ["kB", 1000], ["MB", 1000000], ["GB", 1000000000], ["TB", 1000000000000], ["PB", 1000000000000000]] + } +}; + +// CommonJS, AMD, script tag +if ( typeof exports !== "undefined" ) { + module.exports = filesize; +} +else if ( typeof define === "function" ) { + define( function () { + return filesize; + } ); +} +else { + global.filesize = filesize; +} + +} )( this ); diff --git a/src/UI/Shared/FormatHelpers.js b/src/UI/Shared/FormatHelpers.js index badfc18f3..ac23b473f 100644 --- a/src/UI/Shared/FormatHelpers.js +++ b/src/UI/Shared/FormatHelpers.js @@ -10,7 +10,7 @@ define( bytes: function (sourceSize) { var size = Number(sourceSize); - return Filesize(size, 1, false); + return Filesize(size, { base: 2, round: 1 }); }, dateHelper: function (sourceDate) {