diff options
Diffstat (limited to 'frontend/app/api/storage/route.ts')
| -rw-r--r-- | frontend/app/api/storage/route.ts | 67 |
1 files changed, 1 insertions, 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 @@ | |||
| 1 | import { NextResponse } from 'next/server' | 1 | import { NextResponse } from 'next/server' |
| 2 | import { exec } from 'child_process' | 2 | import { fetchStorageData } from '@/lib/storage' |
| 3 | import { promisify } from 'util' | ||
| 4 | |||
| 5 | const execAsync = promisify(exec) | ||
| 6 | |||
| 7 | // Cache for 10 seconds | ||
| 8 | let cacheTimestamp = 0 | ||
| 9 | let cachedData: StorageData | null = null | ||
| 10 | |||
| 11 | interface StorageData { | ||
| 12 | activeDriveUsage: number | ||
| 13 | totalDiskCapacity: number | ||
| 14 | totalDiskUsed: number | ||
| 15 | availableDisk: number | ||
| 16 | } | ||
| 17 | |||
| 18 | async function fetchStorageData(): Promise<StorageData> { | ||
| 19 | const now = Date.now() | ||
| 20 | |||
| 21 | // Return cached data if less than 10 seconds old | ||
| 22 | if (cachedData && now - cacheTimestamp < 10000) { | ||
| 23 | return cachedData | ||
| 24 | } | ||
| 25 | |||
| 26 | try { | ||
| 27 | // Get active drive usage in bytes | ||
| 28 | const { stdout: driveSize } = await execAsync('fctdrive drive-size') | ||
| 29 | const activeDriveUsage = parseInt(driveSize.trim()) || 0 | ||
| 30 | |||
| 31 | // Get disk usage in bytes | ||
| 32 | const fctdrivePath = process.env.FCTDRIVE_PATH || '/home/diogo464/dev/fctdrive' | ||
| 33 | const { stdout: dfOutput } = await execAsync(`df -B1 "${fctdrivePath}"`) | ||
| 34 | |||
| 35 | // Parse df output - second line contains the data | ||
| 36 | const lines = dfOutput.trim().split('\n') | ||
| 37 | const dataLine = lines[1] // Skip header line | ||
| 38 | const columns = dataLine.split(/\s+/) | ||
| 39 | |||
| 40 | const totalDiskCapacity = parseInt(columns[1]) || 0 | ||
| 41 | const totalDiskUsed = parseInt(columns[2]) || 0 | ||
| 42 | const availableDisk = parseInt(columns[3]) || 0 | ||
| 43 | |||
| 44 | const data: StorageData = { | ||
| 45 | activeDriveUsage, | ||
| 46 | totalDiskCapacity, | ||
| 47 | totalDiskUsed, | ||
| 48 | availableDisk | ||
| 49 | } | ||
| 50 | |||
| 51 | // Update cache | ||
| 52 | cachedData = data | ||
| 53 | cacheTimestamp = now | ||
| 54 | |||
| 55 | return data | ||
| 56 | } catch (error) { | ||
| 57 | console.error('Failed to fetch storage data:', error) | ||
| 58 | |||
| 59 | // Return zeros on error as requested | ||
| 60 | return { | ||
| 61 | activeDriveUsage: 0, | ||
| 62 | totalDiskCapacity: 0, | ||
| 63 | totalDiskUsed: 0, | ||
| 64 | availableDisk: 0 | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 | 3 | ||
| 69 | export async function GET() { | 4 | export async function GET() { |
| 70 | try { | 5 | try { |
