Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/treetrek.git

Adds units to file sizes

AuthorDave Jarvis <email>
Date2026-02-14 14:23:58 GMT-0800
Commitbaa360563a81813bf94e1e47122599065116bcef
Parent544e3d7
File.php
}
+ public function renderSize(FileRenderer $renderer): void {
+ $renderer->renderSize($this->size);
+ }
+
public function renderMedia(string $url): bool {
$rendered = false;
return $rendered;
- }
-
- public function renderSize(FileRenderer $renderer): void {
- $renderer->renderSize($this->size);
}
}
}
+
render/FileRenderer.php
string $iconClass,
int $timestamp,
- string $size = ''
+ int $size = 0
): void;
public function renderTime( int $timestamp ): void;
+
+ public function renderSize( int $bytes ): void;
}
string $iconClass,
int $timestamp,
- string $size = ''
+ int $size = 0
): void {
$fullPath = ($this->currentPath===''?'':$this->currentPath.'/') . $name;
echo '</span>';
- if( $size ) {
- echo '<span class="file-size">' . $size . '</span>';
+ if( $size > 0 ) {
+ echo '<span class="file-size">' . $this->formatSize($size) . '</span>';
}
echo 'just now';
+ }
+
+ public function renderSize( int $bytes ): void {
+ echo $this->formatSize($bytes);
+ }
+
+ private function formatSize( int $bytes ): string {
+ if( $bytes === 0 ) {
+ return '0 B';
+ }
+
+ $units = ['B', 'KB', 'MB', 'GB', 'TB'];
+ $i = 0;
+
+ while( $bytes >= 1024 && $i < count($units) - 1 ) {
+ $bytes /= 1024;
+ $i++;
+ }
+
+ return round($bytes, 2) . ' ' . $units[$i];
}
}
Delta31 lines added, 8 lines removed, 23-line increase