Skip to content

A self-hosted media server and file management system

chore: code smells

aminnausin1691d0a24b81
diff --git a/app/Http/Controllers/Api/V1/Server/DiskUsageController.php b/app/Http/Controllers/Api/V1/Server/DiskUsageController.php
index fd2cc9e2..d23f67db 100644
--- a/app/Http/Controllers/Api/V1/Server/DiskUsageController.php
+++ b/app/Http/Controllers/Api/V1/Server/DiskUsageController.php
@@ -21,35 +21,25 @@ class DiskUsageController extends Controller {
public function __construct(private ServerConfigService $config) {}
public function index(Request $request): JsonResponse {
+ $isAdmin = Gate::allows('admin');
$refresh = $request->boolean('refresh');
$directorySizes = [];
$filesystems = [];
$locations = [];
- $isAdmin = Gate::allows('admin');
-
foreach ($this->locations($isAdmin) as $location) {
[$key, $label, $category, $path] = $location;
$bytes = $location[4] ?? null;
$real = is_dir($path) ? realpath($path) : false;
- $stat = $real !== false ? stat($real) : false;
- $fsId = $stat !== false ? (string) $stat['dev'] : null;
-
- if ($fsId !== null && ! isset($filesystems[$fsId])) {
- $total = disk_total_space($real);
- $free = disk_free_space($real);
-
- if ($total !== false && $free !== false) {
- $filesystems[$fsId] = [
- 'id' => $fsId,
- 'totalSpace' => $total,
- 'usedSpace' => $total - $free,
- 'freeSpace' => $free,
- ];
- }
- }
+ $fsId = $this->collectFilesystem($real, $filesystems);
+
+ $bytes = match (true) {
+ $category === 'library' => $bytes ?? 0,
+ $real !== false => $directorySizes[$real] ??= $this->directorySize($real, $refresh),
+ default => null,
+ };
$locations[] = [
'key' => $key,
@@ -58,9 +48,7 @@ class DiskUsageController extends Controller {
'path' => $isAdmin ? $path : '/',
'exists' => (bool) $real,
'filesystem' => $fsId,
- 'bytes' => $category === 'library'
- ? ($bytes ?? 0)
- : ($real ? ($directorySizes[$real] ??= $this->directorySize($real, $refresh)) : null),
+ 'bytes' => $bytes,
];
}
@@ -70,6 +58,32 @@ class DiskUsageController extends Controller {
]);
}
+ private function collectFilesystem(string|false $real, array &$filesystems): ?string {
+ if ($real === false || ($stat = stat($real)) === false) {
+ return null;
+ }
+
+ $fsId = (string) $stat['dev'];
+
+ if (isset($filesystems[$fsId])) {
+ return $fsId;
+ }
+
+ $total = disk_total_space($real);
+ $free = disk_free_space($real);
+
+ if ($total !== false && $free !== false) {
+ $filesystems[$fsId] = [
+ 'id' => $fsId,
+ 'totalSpace' => $total,
+ 'usedSpace' => $total - $free,
+ 'freeSpace' => $free,
+ ];
+ }
+
+ return $fsId;
+ }
+
/** @return array<int, array{string, string, string, string, 4?: int|null}> [key, label, category, path, ?bytes] */
private function locations($hasFullAccess = false): array {
$private = Storage::disk('local');
diff --git a/resources/js/components/cards/server/StorageStatsCard.vue b/resources/js/components/cards/server/StorageStatsCard.vue
index f71a4ee1..25935b61 100644
--- a/resources/js/components/cards/server/StorageStatsCard.vue
+++ b/resources/js/components/cards/server/StorageStatsCard.vue
@@ -92,14 +92,20 @@ const disks = computed(() =>
const top = libs.slice(0, 3).map((l) => l.label);
const rest = libs.length - top.length;
+ const getDiskColour = (percentUsed: number) => {
+ if (percentUsed >= 90) return 'bg-danger-2';
+ if (percentUsed >= 75) return 'bg-warning-2';
+ return 'bg-success';
+ };
+
return {
...fs,
title: all.length > 1 ? `Disk ${index + 1}` : 'Disk',
percentUsed,
segments,
- contents: [appData.length > 0 && 'app data', , ...top, rest > 0 ? `+${rest} more` : ''].filter(Boolean).join(' · '),
+ contents: [appData.length > 0 && 'app data', ...top, rest > 0 ? `+${rest} more` : ''].filter(Boolean).join(' · '),
libraryNames: libs.map((l) => l.label).join(', '),
- colour: percentUsed >= 90 ? 'bg-danger-2' : percentUsed >= 75 ? 'bg-warning-2' : 'bg-success',
+ colour: getDiskColour(percentUsed),
};
}),
);
diff --git a/resources/js/components/cards/server/storage/DiskUsageCard.vue b/resources/js/components/cards/server/storage/DiskUsageCard.vue
index 928b9197..59cfe93a 100644
--- a/resources/js/components/cards/server/storage/DiskUsageCard.vue
+++ b/resources/js/components/cards/server/storage/DiskUsageCard.vue
@@ -54,7 +54,7 @@ withDefaults(
<span class="text-foreground-2 text-xs">free of {{ formatFileSize(disk.totalSpace) }}</span>
</p>
- <div class="flex h-2 w-full gap-px overflow-hidden rounded-full bg-neutral-200 dark:bg-neutral-700" role="img" :aria-label="`${disk.percentUsed}% used`">
+ <div class="flex h-2 w-full gap-px overflow-hidden rounded-full bg-neutral-200 dark:bg-neutral-700" :aria-label="`${disk.percentUsed}% used`">
<div
v-for="segment in disk.segments"
:key="segment.key"
@@ -84,7 +84,7 @@ withDefaults(
<span class="text-foreground-2 text-xs">of {{ formatFileSize(disk.totalSpace) }}</span>
</span>
</div>
- <div class="h-1.5 w-full overflow-hidden rounded-full bg-neutral-200 dark:bg-neutral-700" role="img" :aria-label="`${disk.percentUsed}% used`">
+ <div class="h-1.5 w-full overflow-hidden rounded-full bg-neutral-200 dark:bg-neutral-700" :aria-label="`${disk.percentUsed}% used`">
<div :class="disk.colour" class="h-full" :style="{ width: `${disk.percentUsed}%` }"></div>
</div>
</div>