Skip to content

Commit ee22e28

Browse files
Add GoogleDriveQuota helper for retrieving Google Drive storage usage
1 parent c8896d9 commit ee22e28

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

.env.example

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ COMPOSER_HOME = "../../.cache/composer"
9595
## SERVER RESOURCE LIMITATION
9696
DISK_SPACE_LIMIT=107374182400
9797
DISK_INODE_LIMIT=400000
98-
DISK_BACKUP_LIMIT=15728640
9998

10099
## APPLICATION UPDATE
101100
AUTO_UPDATE=true

app/Helpers/GoogleDriveQuota.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace App\Helpers;
4+
5+
use Google\Client;
6+
use Google\Service\Drive;
7+
8+
class GoogleDriveQuota
9+
{
10+
public static function getQuota(): array
11+
{
12+
$client = new Client();
13+
$client->setClientId(env('GOOGLE_DRIVE_CLIENT_ID'));
14+
$client->setClientSecret(env('GOOGLE_DRIVE_CLIENT_SECRET'));
15+
$client->setRedirectUri(env('APP_URL') . '/google/oauth/callback');
16+
$client->setAccessType('offline');
17+
$client->setScopes(['https://www.googleapis.com/auth/drive.metadata.readonly']);
18+
19+
// refresh token -> access token
20+
$client->fetchAccessTokenWithRefreshToken(env('GOOGLE_DRIVE_REFRESH_TOKEN'));
21+
22+
$service = new Drive($client);
23+
$about = $service->about->get(['fields' => 'storageQuota']);
24+
$q = $about->getStorageQuota();
25+
26+
$limit = $q->getLimit(); // total bytes
27+
$usage = $q->getUsage(); // used bytes
28+
29+
return [
30+
'used' => self::toGB($usage),
31+
'total' => self::toGB($limit),
32+
];
33+
}
34+
35+
private static function toGB($bytes): ?float
36+
{
37+
if ($bytes === null) return null;
38+
return round($bytes / (1024 ** 3), 2); // convert to GB (2 decimal)
39+
}
40+
}

app/Nova/Metrics/ServerResource.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
namespace App\Nova\Metrics;
44

55
use DateTimeInterface;
6-
use Fidum\LaravelNovaMetricsPolling\Concerns\SupportsPolling;
7-
use Illuminate\Support\Facades\Cache;
8-
use Laravel\Nova\Http\Requests\NovaRequest;
6+
use App\Helpers\GoogleDriveQuota;
97
use Laravel\Nova\Metrics\Partition;
8+
use Illuminate\Support\Facades\Cache;
9+
use Symfony\Component\Process\Process;
1010
use Laravel\Nova\Metrics\PartitionResult;
11+
use Laravel\Nova\Http\Requests\NovaRequest;
1112
use Spatie\Backup\Tasks\Monitor\BackupDestinationStatus;
13+
use Fidum\LaravelNovaMetricsPolling\Concerns\SupportsPolling;
1214
use Spatie\Backup\Tasks\Monitor\BackupDestinationStatusFactory;
13-
use Symfony\Component\Process\Process;
1415

1516
class ServerResource extends Partition
1617
{
@@ -42,6 +43,10 @@ public function calculate(NovaRequest $request): PartitionResult
4243
$command = [];
4344
if ($this->type === 'backup') {
4445
// Get used storage from backup info
46+
$quota = GoogleDriveQuota::getQuota();
47+
48+
$used = $quota['used']; // dalam GB
49+
$total = $quota['total']; // dalam GB
4550
$backupInfo = $this->getBackupInfo();
4651
$used = isset($backupInfo[0]['usedStorage']) ? round($backupInfo[0]['usedStorage'] / 1024 / 1024 / 1024, 2) : 0;
4752
$value = round($used, 2);

config/app.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@
153153

154154
'disk_space_limit' => env('DISK_SPACE_LIMIT', 107374182400),
155155
'disk_inode_limit' => env('DISK_INODE_LIMIT', 400000),
156-
'disk_backup_limit' => env('DISK_BACKUP_LIMIT', 15728640),
157156

158157
/*
159158
|--------------------------------------------------------------------------

0 commit comments

Comments
 (0)