summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2012-02-26 14:03:53 +0100
committerRobin Appelman <icewind@owncloud.com>2012-02-26 14:12:50 +0100
commit3d0d47957e65a72e17d33a924c9c4efcd5088ed2 (patch)
tree195089ba8a25bbb0a4bc4826319d38c93f0bbae6 /core
parent62cd89da14608f4b390c42fe732130ffe8a432fc (diff)
downloadnextcloud-server-3d0d47957e65a72e17d33a924c9c4efcd5088ed2.tar.gz
nextcloud-server-3d0d47957e65a72e17d33a924c9c4efcd5088ed2.zip
improved humanFileSize for js
Diffstat (limited to 'core')
-rw-r--r--core/js/js.js26
1 files changed, 11 insertions, 15 deletions
diff --git a/core/js/js.js b/core/js/js.js
index 4b5062eb1a6..0f8122b3d1e 100644
--- a/core/js/js.js
+++ b/core/js/js.js
@@ -450,22 +450,18 @@ $.fn.filterAttr = function(attr_name, attr_value) {
return this.filter(function() { return $(this).attr(attr_name) === attr_value; });
};
-function humanFileSize(bytes){
- if( bytes < 1024 ){
- return bytes+' B';
+function humanFileSize(size) {
+ humanList = ['B', 'kB', 'MB', 'GB', 'TB'];
+ // Calculate Log with base 1024: size = 1024 ** order
+ order = Math.floor(Math.log(size) / Math.log(1024));
+ // Stay in range of the byte sizes that are defined
+ order = Math.min(humanList.length, order);
+ readableFormat = humanList[order];
+ relativeSize = (size / Math.pow(1024, order)).toFixed(1);
+ if(relativeSize.substr(relativeSize.length-2,2)=='.0'){
+ relativeSize=relativeSize.substr(0,relativeSize.length-2);
}
- bytes = Math.round(bytes / 1024, 1 );
- if( bytes < 1024 ){
- return bytes+' kB';
- }
- bytes = Math.round( bytes / 1024, 1 );
- if( bytes < 1024 ){
- return bytes+' MB';
- }
-
- // Wow, heavy duty for owncloud
- bytes = Math.round( bytes / 1024, 1 );
- return bytes+' GB';
+ return relativeSize + ' ' + readableFormat;
}
function simpleFileSize(bytes) {