From 371d863870520da9a4f3383d7c52e1720c074d03 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sat, 7 Jun 2025 00:25:25 +0400 Subject: fix: filesize display --- public/index.php | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/public/index.php b/public/index.php index f5ea820..580a186 100644 --- a/public/index.php +++ b/public/index.php @@ -71,22 +71,12 @@ if (FILE_CATALOG_FANCY_VIEW && $file_id) { $file['full_url'] = FILE_UPLOAD_DIRECTORY_PREFIX . "/{$file['id']}.{$file['extension']}"; + // formatting the file size $size = $file['size']; - $size_suffix = 'B'; - $size_i = 0; - do { - $size /= 1024; - $size_suffix = match ($size_i) { - 0 => 'B', - 1 => 'KB', - 2 => 'MB', - 3 => 'GB', - default => 'TB' - }; - $size_i++; - } while ($size > 1024); - - $file['size_formatted'] = sprintf('%.2f%s', $size, $size_suffix); + $units = ['B', 'KB', 'MB', 'GB', 'TB']; + $factor = floor((strlen($size) - 1) / 3); + $file['size_formatted'] = sprintf("%.2f", $size / pow(1024, $factor)) . ' ' . $units[$factor]; + $file['name'] = $file['original_name'] ?? sprintf('%s.%s', $file['id'], $file['extension']); if (!isset($file['uploaded_at'])) { -- cgit v1.2.3