summaryrefslogtreecommitdiff
path: root/frontend/app/api/storage/route.ts
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/app/api/storage/route.ts')
-rw-r--r--frontend/app/api/storage/route.ts67
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 @@
1import { NextResponse } from 'next/server' 1import { NextResponse } from 'next/server'
2import { exec } from 'child_process' 2import { fetchStorageData } from '@/lib/storage'
3import { promisify } from 'util'
4
5const execAsync = promisify(exec)
6
7// Cache for 10 seconds
8let cacheTimestamp = 0
9let cachedData: StorageData | null = null
10
11interface StorageData {
12 activeDriveUsage: number
13 totalDiskCapacity: number
14 totalDiskUsed: number
15 availableDisk: number
16}
17
18async 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
69export async function GET() { 4export async function GET() {
70 try { 5 try {