summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordiogo464 <[email protected]>2025-08-13 14:02:21 +0100
committerdiogo464 <[email protected]>2025-08-13 14:02:21 +0100
commitcb53d7a6689d05036341886a59717ddd9f2e6179 (patch)
treef014cf2f9b5b54cde1872cb1b8ca6ae049193b7c
parenta29a634bcb20cb9d61ea60f44b0243249b9c4b5d (diff)
Create shared storage utility function
- Extract storage data fetching logic into reusable lib/storage.ts - Export StorageData interface and fetchStorageData function - Maintain 10-second caching behavior - Enable direct function calls instead of HTTP requests during SSR 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
-rw-r--r--frontend/lib/storage.ts66
1 files changed, 66 insertions, 0 deletions
diff --git a/frontend/lib/storage.ts b/frontend/lib/storage.ts
new file mode 100644
index 0000000..57b2429
--- /dev/null
+++ b/frontend/lib/storage.ts
@@ -0,0 +1,66 @@
1import { exec } from 'child_process'
2import { promisify } from 'util'
3
4const execAsync = promisify(exec)
5
6// Cache for 10 seconds
7let cacheTimestamp = 0
8let cachedData: StorageData | null = null
9
10export interface StorageData {
11 activeDriveUsage: number
12 totalDiskCapacity: number
13 totalDiskUsed: number
14 availableDisk: number
15}
16
17export async function fetchStorageData(): Promise<StorageData> {
18 const now = Date.now()
19
20 // Return cached data if less than 10 seconds old
21 if (cachedData && now - cacheTimestamp < 10000) {
22 return cachedData
23 }
24
25 try {
26 // Get active drive usage in bytes
27 const { stdout: driveSize } = await execAsync('fctdrive drive-size')
28 const activeDriveUsage = parseInt(driveSize.trim()) || 0
29
30 // Get disk usage in bytes
31 const fctdrivePath = process.env.FCTDRIVE_PATH || '/home/diogo464/dev/fctdrive'
32 const { stdout: dfOutput } = await execAsync(`df -B1 "${fctdrivePath}"`)
33
34 // Parse df output - second line contains the data
35 const lines = dfOutput.trim().split('\n')
36 const dataLine = lines[1] // Skip header line
37 const columns = dataLine.split(/\s+/)
38
39 const totalDiskCapacity = parseInt(columns[1]) || 0
40 const totalDiskUsed = parseInt(columns[2]) || 0
41 const availableDisk = parseInt(columns[3]) || 0
42
43 const data: StorageData = {
44 activeDriveUsage,
45 totalDiskCapacity,
46 totalDiskUsed,
47 availableDisk
48 }
49
50 // Update cache
51 cachedData = data
52 cacheTimestamp = now
53
54 return data
55 } catch (error) {
56 console.error('Failed to fetch storage data:', error)
57
58 // Return zeros on error as requested
59 return {
60 activeDriveUsage: 0,
61 totalDiskCapacity: 0,
62 totalDiskUsed: 0,
63 availableDisk: 0
64 }
65 }
66} \ No newline at end of file