From 80b66131b793c7d3b9a5df9168906e29e296a057 Mon Sep 17 00:00:00 2001 From: diogo464 Date: Wed, 13 Aug 2025 14:02:28 +0100 Subject: Refactor storage API to use shared utility MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Import fetchStorageData from lib/storage instead of duplicating logic - Simplify API route by removing duplicate implementation - Maintain same API response format 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- frontend/app/api/storage/route.ts | 67 +-------------------------------------- 1 file changed, 1 insertion(+), 66 deletions(-) diff --git a/frontend/app/api/storage/route.ts b/frontend/app/api/storage/route.ts index 66f2817..6e07993 100644 --- a/frontend/app/api/storage/route.ts +++ b/frontend/app/api/storage/route.ts @@ -1,70 +1,5 @@ import { NextResponse } from 'next/server' -import { exec } from 'child_process' -import { promisify } from 'util' - -const execAsync = promisify(exec) - -// Cache for 10 seconds -let cacheTimestamp = 0 -let cachedData: StorageData | null = null - -interface StorageData { - activeDriveUsage: number - totalDiskCapacity: number - totalDiskUsed: number - availableDisk: number -} - -async function fetchStorageData(): Promise { - const now = Date.now() - - // Return cached data if less than 10 seconds old - if (cachedData && now - cacheTimestamp < 10000) { - return cachedData - } - - try { - // Get active drive usage in bytes - const { stdout: driveSize } = await execAsync('fctdrive drive-size') - const activeDriveUsage = parseInt(driveSize.trim()) || 0 - - // Get disk usage in bytes - const fctdrivePath = process.env.FCTDRIVE_PATH || '/home/diogo464/dev/fctdrive' - const { stdout: dfOutput } = await execAsync(`df -B1 "${fctdrivePath}"`) - - // Parse df output - second line contains the data - const lines = dfOutput.trim().split('\n') - const dataLine = lines[1] // Skip header line - const columns = dataLine.split(/\s+/) - - const totalDiskCapacity = parseInt(columns[1]) || 0 - const totalDiskUsed = parseInt(columns[2]) || 0 - const availableDisk = parseInt(columns[3]) || 0 - - const data: StorageData = { - activeDriveUsage, - totalDiskCapacity, - totalDiskUsed, - availableDisk - } - - // Update cache - cachedData = data - cacheTimestamp = now - - return data - } catch (error) { - console.error('Failed to fetch storage data:', error) - - // Return zeros on error as requested - return { - activeDriveUsage: 0, - totalDiskCapacity: 0, - totalDiskUsed: 0, - availableDisk: 0 - } - } -} +import { fetchStorageData } from '@/lib/storage' export async function GET() { try { -- cgit