Skip to content

Commit 80ec74d

Browse files
committed
Refactor GoogleDriveQuota usage in ServerResource to simplify quota retrieval
1 parent aa78168 commit 80ec74d

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

app/Helpers/GoogleDriveQuota.php

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,34 @@ public static function getQuota(): array
1212
$clientSecret = config('app.google.client_secret');
1313
$refreshToken = config('app.google.refresh_token');
1414

15-
// Step 1: Refresh token -> Access token
15+
$used = 0;
16+
$total = 0;
17+
1618
$response = Http::asForm()->post('https://oauth2.googleapis.com/token', [
1719
'client_id' => $clientId,
1820
'client_secret' => $clientSecret,
1921
'refresh_token' => $refreshToken,
2022
'grant_type' => 'refresh_token',
2123
]);
2224

23-
if ($response->failed()) {
24-
return [
25-
'used_gb' => null,
26-
'total_gb' => null,
27-
'error' => $response->json(),
28-
];
29-
}
30-
31-
$accessToken = $response->json()['access_token'];
25+
if ($response->ok()) {
26+
$accessToken = $response->json()['access_token'] ?? null;
27+
if ($accessToken) {
28+
$about = Http::withToken($accessToken)
29+
->get('https://www.googleapis.com/drive/v3/about?fields=storageQuota')
30+
->json();
3231

33-
// Step 2: Get storage info
34-
$about = Http::withToken($accessToken)
35-
->get('https://www.googleapis.com/drive/v3/about?fields=storageQuota')
36-
->json();
32+
$limit = $about['storageQuota']['limit'] ?? 0;
33+
$usage = $about['storageQuota']['usage'] ?? 0;
3734

38-
$limit = $about['storageQuota']['limit'] ?? 0;
39-
$usage = $about['storageQuota']['usage'] ?? 0;
40-
41-
// Convert bytes to GB
42-
$limitGb = $limit ? round($limit / (1024 ** 3), 2) : 0;
43-
$usageGb = $usage ? round($usage / (1024 ** 3), 2) : 0;
35+
$total = $limit ? round($limit / (1024 ** 3), 2) : 0;
36+
$used = $usage ? round($usage / (1024 ** 3), 2) : 0;
37+
}
38+
}
4439

4540
return [
46-
'used' => $usageGb,
47-
'total' => $limitGb,
41+
'used' => $used,
42+
'total' => $total,
4843
];
4944
}
5045
}

app/Nova/Metrics/ServerResource.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public function calculate(NovaRequest $request): PartitionResult
4545
// Get used storage from backup info
4646
$quota = GoogleDriveQuota::getQuota();
4747

48-
$value = isset($quota['used']) ? $quota['used'] : 0; // dalam GB
49-
$total = isset($quota['total']) ? $quota['total'] : 0; // dalam GB
48+
$value = $quota['used']; // dalam GB
49+
$total = $quota['total']; // dalam GB
5050
} else {
5151
$command = $this->type === 'inode'
5252
? ['du', '--inodes', '-s']

0 commit comments

Comments
 (0)