@@ -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}
0 commit comments