From 00a56398fbdb6a2e1042146faf83981206d643e1 Mon Sep 17 00:00:00 2001 From: Boy132 Date: Thu, 25 Jun 2026 16:41:51 +0200 Subject: [PATCH] Add more byte suffixes and make sure we don't overflow (#2417) --- app/helpers.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/helpers.php b/app/helpers.php index 908657950..0ce2bb01a 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -37,16 +37,23 @@ if (!function_exists('is_ipv6')) { if (!function_exists('convert_bytes_to_readable')) { function convert_bytes_to_readable(int $bytes, int $decimals = 2, ?int $base = null): string { - $conversionUnit = config('panel.use_binary_prefix') ? 1024 : 1000; - $suffix = config('panel.use_binary_prefix') ? ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB'] : ['Bytes', 'KB', 'MB', 'GB', 'TB']; + $binarySuffixes = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']; + $decimalSuffixes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + $suffix = config('panel.use_binary_prefix') ? $binarySuffixes : $decimalSuffixes; if ($bytes <= 0) { return '0 ' . $suffix[0]; } + $conversionUnit = config('panel.use_binary_prefix') ? 1024 : 1000; + $fromBase = log($bytes) / log($conversionUnit); $base ??= floor($fromBase); + if ($base > count($suffix) - 1) { + $base = count($suffix) - 1; + } + return format_number(pow($conversionUnit, $fromBase - $base), precision: $decimals) . ' ' . $suffix[$base]; } }