From 8ae6e5e4867f940512b8dfd61492c45f05d31adc Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Mon, 21 Jul 2025 18:26:09 +0200 Subject: [PATCH] WebUI: improve fileSizeIEC function and make it more readable Fixes #1974 Signed-off-by: Nicola Murino --- templates/common/base.html | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/templates/common/base.html b/templates/common/base.html index c8d7f9bc..81e09f97 100644 --- a/templates/common/base.html +++ b/templates/common/base.html @@ -76,9 +76,17 @@ explicit grant from the SFTPGo Team (support@sftpgo.com). return div.innerHTML; } - function fileSizeIEC(a,b,c,d,e){ - return (b=Math,c=b.log,d=1024,e=c(a)/c(d)|0,a/b.pow(d,e)).toFixed(1) - +' '+(e?'KMGTPEZY'[--e]+'iB':'Bytes') + function fileSizeIEC(bytes){ + const thresh = 1024; + if (bytes < thresh){ + return bytes + ' Bytes'; + } + + const units = ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']; + let i = Math.floor(Math.log(bytes) / Math.log(thresh)); + let size = bytes / Math.pow(thresh, i); + + return size.toFixed(1) + ' ' + units[i - 1]; } function humanizeSpeed(a,b,c,d,e){