Skip to content

Commit d699837

Browse files
individual-itVincent Petry
authored andcommitted
fix computerFileSize to parse correctly, prerapation to fix #25358 (#27257)
* fix computerFileSize to be mory picky * more tests for computerFileSize * codestyle fix * fix double initialization * pre-compile and store regexp in a hidden variable as suggested by @PVince81 in Review #27257 (comment) * test strings with whitespaces * trim string before parsing
1 parent c978e14 commit d699837

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

core/js/js.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,12 @@ function relative_modified_date(timestamp) {
17421742
OC.Util = {
17431743
// TODO: remove original functions from global namespace
17441744
humanFileSize: humanFileSize,
1745+
1746+
/**
1747+
* regular expression to parse size in bytes from a humanly readable string
1748+
* see computerFileSize(string)
1749+
*/
1750+
_computerFileSizeRegexp: /^[\s+]?([0-9]*)(\.([0-9]+))?( +)?([kmgtp]?b?)$/i,
17451751

17461752
/**
17471753
* Returns a file size in bytes from a humanly readable string
@@ -1753,17 +1759,13 @@ OC.Util = {
17531759
*
17541760
*/
17551761
computerFileSize: function (string) {
1756-
if (typeof string != 'string') {
1762+
if (typeof string !== 'string') {
17571763
return null;
17581764
}
17591765

1760-
var s = string.toLowerCase();
1761-
var bytes = parseFloat(s)
1762-
1763-
if (!isNaN(bytes) && isFinite(s)) {
1764-
return bytes;
1765-
}
1766-
1766+
var s = string.toLowerCase().trim();
1767+
var bytes = null;
1768+
17671769
var bytesArray = {
17681770
'b': 1,
17691771
'k': 1024,
@@ -1778,12 +1780,18 @@ OC.Util = {
17781780
'p': 1024 * 1024 * 1024 * 1024 * 1024
17791781
};
17801782

1781-
var matches = s.match(/([kmgtp]?b?)$/i);
1782-
if (matches[1]) {
1783-
bytes = bytes * bytesArray[matches[1]];
1783+
var matches = s.match(this._computerFileSizeRegexp);
1784+
if (matches !== null) {
1785+
bytes = parseFloat(s);
1786+
if (!isFinite(bytes)) {
1787+
return null;
1788+
}
17841789
} else {
17851790
return null;
17861791
}
1792+
if (matches[5]) {
1793+
bytes = bytes * bytesArray[matches[5]];
1794+
}
17871795

17881796
bytes = Math.round(bytes);
17891797
return bytes;

core/js/tests/specs/coreSpec.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,14 @@ describe('Core base tests', function() {
594594
it('correctly parses file sizes from a human readable formated string', function() {
595595
var data = [
596596
['125', 125],
597-
['125.25', 125.25],
597+
['125.25', 125],
598+
['125.25B', 125],
599+
['125.25 B', 125],
598600
['0 B', 0],
601+
['99999999999999999999999999999999999999999999 B', 99999999999999999999999999999999999999999999],
602+
['0 MB', 0],
603+
['0 kB', 0],
604+
['0kB', 0],
599605
['125 B', 125],
600606
['125b', 125],
601607
['125 KB', 128000],
@@ -605,7 +611,21 @@ describe('Core base tests', function() {
605611
['119.2 GB', 127990025421],
606612
['119.2gb', 127990025421],
607613
['116.4 TB', 127983153473126],
608-
['116.4tb', 127983153473126]
614+
['116.4tb', 127983153473126],
615+
['8776656778888777655.4tb', 9.650036181387265e+30],
616+
[1234, null],
617+
[-1234, null],
618+
['-1234 B', null],
619+
['B', null],
620+
['40/0', null],
621+
['40,30 kb', null],
622+
[' 122.1 MB ', 128031130],
623+
['122.1 MB ', 128031130],
624+
[' 122.1 MB ', 128031130],
625+
[' 122.1 MB ', 128031130],
626+
['122.1 MB ', 128031130],
627+
[' 125', 125],
628+
[' 125 ', 125],
609629
];
610630
for (var i = 0; i < data.length; i++) {
611631
expect(OC.Util.computerFileSize(data[i][0])).toEqual(data[i][1]);

0 commit comments

Comments
 (0)