Skip to content

Commit

Permalink
nao-pon/flysystem-google-drive replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Jun 20, 2024
1 parent e034a2a commit d93c343
Show file tree
Hide file tree
Showing 8 changed files with 906 additions and 11 deletions.
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,10 @@ AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

VITE_APP_NAME="${APP_NAME}"

#FILESYSTEM_DRIVER=google
GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com
GOOGLE_DRIVE_CLIENT_SECRET=xxx
GOOGLE_DRIVE_REFRESH_TOKEN=xxx
GOOGLE_DRIVE_FOLDER_ID=
#GOOGLE_DRIVE_TEAM_DRIVE_ID=
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ I have included [GoogleDriveServiceProvider](app/Providers/GoogleDriveServicePro
'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'),
'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
'folder' => env('GOOGLE_DRIVE_FOLDER'),
'folderId' => env('GOOGLE_DRIVE_FOLDER_ID'),
// 'teamDriveId' => env('GOOGLE_DRIVE_TEAM_DRIVE_ID'),
],

Expand All @@ -60,7 +60,7 @@ FILESYSTEM_DRIVER=google
GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com
GOOGLE_DRIVE_CLIENT_SECRET=xxx
GOOGLE_DRIVE_REFRESH_TOKEN=xxx
GOOGLE_DRIVE_FOLDER=folder_name
GOOGLE_DRIVE_FOLDER_ID=folder-id-xxx
#GOOGLE_DRIVE_TEAM_DRIVE_ID=xxx
```

Expand All @@ -72,12 +72,12 @@ If you want to use multiple Google Drive accounts in a single Laravel app, you n
MAIN_GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com
MAIN_GOOGLE_DRIVE_CLIENT_SECRET=xxx
MAIN_GOOGLE_DRIVE_REFRESH_TOKEN=xxx
MAIN_GOOGLE_DRIVE_FOLDER=
MAIN_GOOGLE_DRIVE_FOLDER_ID=
BACKUP_GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com
BACKUP_GOOGLE_DRIVE_CLIENT_SECRET=xxx
BACKUP_GOOGLE_DRIVE_REFRESH_TOKEN=xxx
BACKUP_GOOGLE_DRIVE_FOLDER=
BACKUP_GOOGLE_DRIVE_FOLDER_ID=
```

Then you should add a disk in `config/filesystems.php` for each account using the `google` driver and the account specific keys:
Expand All @@ -88,15 +88,15 @@ Then you should add a disk in `config/filesystems.php` for each account using th
'clientId' => env('MAIN_GOOGLE_DRIVE_CLIENT_ID'),
'clientSecret' => env('MAIN_GOOGLE_DRIVE_CLIENT_SECRET'),
'refreshToken' => env('MAIN_GOOGLE_DRIVE_REFRESH_TOKEN'),
'folder' => env('MAIN_GOOGLE_DRIVE_FOLDER'),
'folderId' => env('MAIN_GOOGLE_DRIVE_FOLDER_ID'),
],

'backup_google' => [
'driver' => 'google',
'clientId' => env('BACKUP_GOOGLE_DRIVE_CLIENT_ID'),
'clientSecret' => env('BACKUP_GOOGLE_DRIVE_CLIENT_SECRET'),
'refreshToken' => env('BACKUP_GOOGLE_DRIVE_REFRESH_TOKEN'),
'folder' => env('BACKUP_GOOGLE_DRIVE_FOLDER'),
'folderId' => env('BACKUP_GOOGLE_DRIVE_FOLDER_ID'),
],
```

Expand Down
49 changes: 49 additions & 0 deletions app/Providers/GoogleDriveServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Providers;

use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Storage;
use Masbug\Flysystem\GoogleDriveAdapter;
use League\Flysystem\Filesystem;

class GoogleDriveServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
Storage::extend('google', function($app, $config) {
$client = new \Google\Client();
$client->setClientId($config['clientId']);
$client->setClientSecret($config['clientSecret']);
$client->refreshToken($config['refreshToken']);
$service = new \Google\Service\Drive($client);

$options = [
'useDisplayPaths' => false, // as nao-pon/flysystem-google-drive replacement
];
if(isset($config['teamDriveId'])) {
$options['teamDriveId'] = $config['teamDriveId'];
}

$adapter = new GoogleDriveAdapter($service, $config['folderId'] ?? null, $options);
$driver = new Filesystem($adapter);
return new FilesystemAdapter($driver, $adapter);
});
}

/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
1 change: 1 addition & 0 deletions bootstrap/providers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

return [
App\Providers\AppServiceProvider::class,
App\Providers\GoogleDriveServiceProvider::class,
];
14 changes: 10 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
"license": "MIT",
"require": {
"php": "^8.2",
"laravel/framework": "^11.9"
"laravel/framework": "^11.9",
"masbug/flysystem-google-drive-ext": "^2.0"
},
"require-dev": {
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"google/apiclient-services": [
"Drive"
]
},
"autoload": {
"psr-4": {
Expand All @@ -31,10 +35,12 @@
"@php artisan package:discover --ansi"
],
"post-update-cmd": [
"@php artisan vendor:publish --tag=laravel-assets --ansi --force"
"@php artisan vendor:publish --tag=laravel-assets --ansi --force",
"Google\\Task\\Composer::cleanup"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\"",
"Google\\Task\\Composer::cleanup"
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi",
Expand Down
Loading

0 comments on commit d93c343

Please sign in to comment.