summaryrefslogtreecommitdiffstats
path: root/core/js
diff options
context:
space:
mode:
authorPatrick Paysant <patrick.paysant@linagora.com>2016-12-07 10:38:54 +0100
committerLukas Reschke <lukas@statuscode.ch>2016-12-19 17:28:58 +0100
commit6217393d6a713aada02961e10e3824bb1ccfe035 (patch)
tree690c7d6f4110ec0a9d5706496545bdc3d2fdc6d7 /core/js
parent1afdce9ef84bebfc4b4a902c1f0311e5e6562387 (diff)
downloadnextcloud-server-6217393d6a713aada02961e10e3824bb1ccfe035.tar.gz
nextcloud-server-6217393d6a713aada02961e10e3824bb1ccfe035.zip
Adding computerFileSize in OC.Util
Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
Diffstat (limited to 'core/js')
-rw-r--r--core/js/js.js46
-rw-r--r--core/js/tests/specs/coreSpec.js20
2 files changed, 66 insertions, 0 deletions
diff --git a/core/js/js.js b/core/js/js.js
index 972f0e63144..2b14ded87d7 100644
--- a/core/js/js.js
+++ b/core/js/js.js
@@ -1626,6 +1626,51 @@ function humanFileSize(size, skipSmallSizes) {
}
/**
+ * Returns a file size in bytes from a humanly readable string
+ * @param {string} string file size in human readable format
+ * @return {number}
+ *
+ * Makes 2kB to 2048.
+ *
+ * Inspired by computerFileSize in helper.php
+ */
+function computerFileSize(string) {
+ var s = string.toLowerCase();
+
+ if (!isNaN(parseFloat(s)) && isFinite(s)) {
+ return parseFloat(s);
+ }
+
+ var bytes_array = {
+ 'b' : 1,
+ 'k' : 1024,
+ 'kb': 1024,
+ 'mb': 1024 * 1024,
+ 'm' : 1024 * 1024,
+ 'gb': 1024 * 1024 * 1024,
+ 'g' : 1024 * 1024 * 1024,
+ 'tb': 1024 * 1024 * 1024 * 1024,
+ 't' : 1024 * 1024 * 1024 * 1024,
+ 'pb': 1024 * 1024 * 1024 * 1024 * 1024,
+ 'p' : 1024 * 1024 * 1024 * 1024 * 1024
+ };
+
+ var bytes = parseFloat(s);
+
+ var matches = s.match(/([kmgtp]?b?)$/i);
+ if (matches[1]) {
+ bytes = bytes * bytes_array[matches[1]];
+ }
+ else {
+ return false;
+ }
+
+ bytes = Math.round(bytes);
+ console.log(bytes);
+ return bytes;
+}
+
+/**
* Format an UNIX timestamp to a human understandable format
* @param {number} timestamp UNIX timestamp
* @return {string} Human readable format
@@ -1667,6 +1712,7 @@ function relative_modified_date(timestamp) {
OC.Util = {
// TODO: remove original functions from global namespace
humanFileSize: humanFileSize,
+ computerFileSize: computerFileSize,
/**
* @param timestamp
diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js
index d1734a9f3d1..8ece9344b5b 100644
--- a/core/js/tests/specs/coreSpec.js
+++ b/core/js/tests/specs/coreSpec.js
@@ -590,6 +590,26 @@ describe('Core base tests', function() {
}
});
});
+ describe('computerFileSize', function() {
+ it('correctly parses file sizes from a human readable formated string', function() {
+ var data = [
+ ['0 B', 0],
+ ['125 B', 125],
+ ['125b', 125],
+ ['125 KB', 128000],
+ ['125kb', 128000],
+ ['122.1 MB', 128031130, ],
+ ['122.1mb', 128031130, ],
+ ['119.2 GB', 127990025421],
+ ['119.2gb', 127990025421],
+ ['116.4 TB', 127983153473126],
+ ['116.4tb', 127983153473126]
+ ];
+ for (var i = 0; i < data.length; i++) {
+ expect(OC.Util.computerFileSize(data[i][0])).toEqual(data[i][1]);
+ }
+ });
+ });
describe('stripTime', function() {
it('strips time from dates', function() {
expect(OC.Util.stripTime(new Date(2014, 2, 24, 15, 4, 45, 24)))