From 9d502fefba7a8eeb121104c9aa149dda4c3e5585 Mon Sep 17 00:00:00 2001 From: Daniil Isakov <12859907+oplexz@users.noreply.github.com> Date: Sat, 13 Jan 2024 01:15:42 +0300 Subject: [PATCH] Fix pretty bytes --- webui/src/App.vue | 8 ++++---- webui/src/components/ClientTransfer.vue | 19 ++++++++++++------- webui/src/composables/useBytes.js | 25 +++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 webui/src/composables/useBytes.js diff --git a/webui/src/App.vue b/webui/src/App.vue index 356f950f..2148341b 100644 --- a/webui/src/App.vue +++ b/webui/src/App.vue @@ -231,6 +231,7 @@ export default { const { dateTime } = useDateTime(); const { timeago } = useTimeAgo(); + const { bytes } = useBytes(); return { authenticated, @@ -258,6 +259,7 @@ export default { dateTime, timeago, + bytes, api, }; @@ -596,21 +598,19 @@ export default { /> - - diff --git a/webui/src/components/ClientTransfer.vue b/webui/src/components/ClientTransfer.vue index c4c6f0b5..31944028 100644 --- a/webui/src/components/ClientTransfer.vue +++ b/webui/src/components/ClientTransfer.vue @@ -1,13 +1,18 @@ + + - - diff --git a/webui/src/composables/useBytes.js b/webui/src/composables/useBytes.js new file mode 100644 index 00000000..496613f1 --- /dev/null +++ b/webui/src/composables/useBytes.js @@ -0,0 +1,25 @@ +export function useBytes() { + const bytes = (bytes, decimals, kib, maxunit) => { + kib = kib || false; + if (bytes === 0) return '0 B'; + if (Number.isNaN(parseFloat(bytes)) && !Number.isFinite(bytes)) + return 'NaN'; + const k = kib ? 1024 : 1000; + const dm = + decimals != null && !Number.isNaN(decimals) && decimals >= 0 + ? decimals + : 2; + const sizes = kib + ? ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB', 'BiB'] + : ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'BB']; + let i = Math.floor(Math.log(bytes) / Math.log(k)); + if (maxunit !== undefined) { + const index = sizes.indexOf(maxunit); + if (index !== -1) i = index; + } + // eslint-disable-next-line no-restricted-properties + return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`; + }; + + return { bytes }; +}