diff --git a/apps/files/js/filelist.js b/apps/files/js/filelist.js index 1b2a62137e..4229988b17 100644 --- a/apps/files/js/filelist.js +++ b/apps/files/js/filelist.js @@ -613,7 +613,7 @@ // size column if (typeof(fileData.size) !== 'undefined' && fileData.size >= 0) { - simpleSize = humanFileSize(parseInt(fileData.size, 10)); + simpleSize = humanFileSize(parseInt(fileData.size, 10), true); sizeColor = Math.round(160-Math.pow((fileData.size/(1024*1024)),2)); } else { simpleSize = t('files', 'Pending'); diff --git a/apps/files/tests/js/filelistSpec.js b/apps/files/tests/js/filelistSpec.js index 855a5c9af5..7d3bc946dd 100644 --- a/apps/files/tests/js/filelistSpec.js +++ b/apps/files/tests/js/filelistSpec.js @@ -252,7 +252,7 @@ describe('OCA.Files.FileList tests', function() { size: '0' }; var $tr = fileList.add(fileData); - expect($tr.find('.filesize').text()).toEqual('0 B'); + expect($tr.find('.filesize').text()).toEqual('0 kB'); }); it('adds new file to the end of the list', function() { var $tr; diff --git a/core/js/js.js b/core/js/js.js index 21a2d4c1b3..a859034ed0 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -1163,9 +1163,10 @@ $.fn.filterAttr = function(attr_name, attr_value) { /** * Returns a human readable file size * @param {number} size Size in bytes + * @param {boolean} skipSmallSizes return '< 1 kB' for small files * @return {string} */ -function humanFileSize(size) { +function humanFileSize(size, skipSmallSizes) { var humanList = ['B', 'kB', 'MB', 'GB', 'TB']; // Calculate Log with base 1024: size = 1024 ** order var order = size?Math.floor(Math.log(size) / Math.log(1024)):0; @@ -1173,6 +1174,13 @@ function humanFileSize(size) { order = Math.min(humanList.length - 1, order); var readableFormat = humanList[order]; var relativeSize = (size / Math.pow(1024, order)).toFixed(1); + if(skipSmallSizes === true && order === 0) { + if(relativeSize !== "0.0"){ + return '< 1 kB'; + } else { + return '0 kB'; + } + } if(order < 2){ relativeSize = parseFloat(relativeSize).toFixed(0); } diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js index 233c4d5a0b..47e5ebfed5 100644 --- a/core/js/tests/specs/coreSpec.js +++ b/core/js/tests/specs/coreSpec.js @@ -500,6 +500,19 @@ describe('Core base tests', function() { expect(OC.Util.humanFileSize(data[i][0])).toEqual(data[i][1]); } }); + it('renders file sizes with the correct unit for small sizes', function() { + var data = [ + [0, '0 kB'], + [125, '< 1 kB'], + [128000, '125 kB'], + [128000000, '122.1 MB'], + [128000000000, '119.2 GB'], + [128000000000000, '116.4 TB'] + ]; + for (var i = 0; i < data.length; i++) { + expect(OC.Util.humanFileSize(data[i][0], true)).toEqual(data[i][1]); + } + }); }); }); });