Skip to content
Merged
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
30 changes: 19 additions & 11 deletions core/js/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,12 @@ function relative_modified_date(timestamp) {
OC.Util = {
// TODO: remove original functions from global namespace
humanFileSize: humanFileSize,

/**
* regular expression to parse size in bytes from a humanly readable string
* see computerFileSize(string)
*/
_computerFileSizeRegexp: /^[\s+]?([0-9]*)(\.([0-9]+))?( +)?([kmgtp]?b?)$/i,

/**
* Returns a file size in bytes from a humanly readable string
Expand All @@ -1745,17 +1751,13 @@ OC.Util = {
*
*/
computerFileSize: function (string) {
if (typeof string != 'string') {
if (typeof string !== 'string') {
return null;
}

var s = string.toLowerCase();
var bytes = parseFloat(s)

if (!isNaN(bytes) && isFinite(s)) {
return bytes;
}

var s = string.toLowerCase().trim();
var bytes = null;

var bytesArray = {
'b': 1,
'k': 1024,
Expand All @@ -1770,12 +1772,18 @@ OC.Util = {
'p': 1024 * 1024 * 1024 * 1024 * 1024
};

var matches = s.match(/([kmgtp]?b?)$/i);
if (matches[1]) {
bytes = bytes * bytesArray[matches[1]];
var matches = s.match(this._computerFileSizeRegexp);
if (matches !== null) {
bytes = parseFloat(s);
if (!isFinite(bytes)) {
return null;
}
} else {
return null;
}
if (matches[5]) {
bytes = bytes * bytesArray[matches[5]];
}

bytes = Math.round(bytes);
return bytes;
Expand Down
24 changes: 22 additions & 2 deletions core/js/tests/specs/coreSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,14 @@ describe('Core base tests', function() {
it('correctly parses file sizes from a human readable formated string', function() {
var data = [
['125', 125],
['125.25', 125.25],
['125.25', 125],
['125.25B', 125],
['125.25 B', 125],
['0 B', 0],
['99999999999999999999999999999999999999999999 B', 99999999999999999999999999999999999999999999],
['0 MB', 0],
['0 kB', 0],
['0kB', 0],
['125 B', 125],
['125b', 125],
['125 KB', 128000],
Expand All @@ -605,7 +611,21 @@ describe('Core base tests', function() {
['119.2 GB', 127990025421],
['119.2gb', 127990025421],
['116.4 TB', 127983153473126],
['116.4tb', 127983153473126]
['116.4tb', 127983153473126],
['8776656778888777655.4tb', 9.650036181387265e+30],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we're already ready for the next century's storage requirements 😆

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

living in the foothills of the Himalayas I know about huge clouds, so better be prepared, who knows what the climate change will bring ;-)

[1234, null],
[-1234, null],
['-1234 B', null],
['B', null],
['40/0', null],
['40,30 kb', null],
[' 122.1 MB ', 128031130],
['122.1 MB ', 128031130],
[' 122.1 MB ', 128031130],
[' 122.1 MB ', 128031130],
['122.1 MB ', 128031130],
[' 125', 125],
[' 125 ', 125],
];
for (var i = 0; i < data.length; i++) {
expect(OC.Util.computerFileSize(data[i][0])).toEqual(data[i][1]);
Expand Down