mirror of
https://github.com/notf0und/SGS
synced 2026-07-17 18:21:06 +00:00
0993bfa666
Fixes #3: Error on DBI when files or folders contains special characters. ### Additional changes * Added unit and feature tests * Fix file size display * Run tests on CI/CD * Publish docker image on release
28 lines
659 B
PHP
28 lines
659 B
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
class FileSize
|
|
{
|
|
private const UNITS = ['', 'K', 'M', 'G', 'T'];
|
|
private const THRESHOLD = 1024;
|
|
|
|
/**
|
|
* Format bytes into human-readable file size.
|
|
*/
|
|
public static function format(int $bytes): string
|
|
{
|
|
if ($bytes === 0) {
|
|
return '0';
|
|
}
|
|
|
|
$unitIndex = (int) floor(log($bytes, self::THRESHOLD));
|
|
$unitIndex = min($unitIndex, count(self::UNITS) - 1);
|
|
|
|
$size = $bytes / pow(self::THRESHOLD, $unitIndex);
|
|
$decimals = $unitIndex === 0 ? 0 : 2;
|
|
|
|
return number_format($size, $decimals, '.', '') . self::UNITS[$unitIndex];
|
|
}
|
|
}
|