diff --git a/README.md b/README.md index 2d25149..931548c 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,15 @@ $adapter3 = new \Masbug\Flysystem\GoogleDriveAdapter( ] ); +// variant 4: connect to a folder shared with you +$adapter4 = new \Masbug\Flysystem\GoogleDriveAdapter( + $service, + 'My_App_Root', + [ + 'sharedFolderId' => '0GF9IioKDqJsRGk9PVA' + ] +); + $fs = new \League\Flysystem\Filesystem($adapter, new \League\Flysystem\Config([\League\Flysystem\Config::OPTION_VISIBILITY => \League\Flysystem\Visibility::PRIVATE])); ``` @@ -159,6 +168,7 @@ GOOGLE_DRIVE_CLIENT_SECRET=xxx GOOGLE_DRIVE_REFRESH_TOKEN=xxx GOOGLE_DRIVE_FOLDER= #GOOGLE_DRIVE_TEAM_DRIVE_ID=xxx +#GOOGLE_DRIVE_SHARED_FOLDER_ID=xxx # you can use more accounts, only add more configs #SECOND_GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com @@ -166,6 +176,7 @@ GOOGLE_DRIVE_FOLDER= #SECOND_GOOGLE_DRIVE_REFRESH_TOKEN=xxx #SECOND_GOOGLE_DRIVE_FOLDER=backups #SECOND_DRIVE_TEAM_DRIVE_ID=xxx +#SECOND_DRIVE_SHARED_FOLDER_ID=xxx ``` ##### Add disks on `config/filesystems.php` @@ -180,6 +191,7 @@ GOOGLE_DRIVE_FOLDER= 'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'), 'folder' => env('GOOGLE_DRIVE_FOLDER'), // without folder is root of drive or team drive //'teamDriveId' => env('GOOGLE_DRIVE_TEAM_DRIVE_ID'), + //'sharedFolderId' => env('GOOGLE_DRIVE_SHARED_FOLDER_ID'), ], // you can use more accounts, only add more disks and configs on .env // also you can use the same account and point to a diferent folders for each disk @@ -216,6 +228,10 @@ class AppServiceProvider extends ServiceProvider { // can be a custom ServicePro $options['teamDriveId'] = $config['teamDriveId']; } + if (!empty($config['sharedFolderId'] ?? null)) { + $options['sharedFolderId'] = $config['sharedFolderId']; + } + $client = new \Google\Client(); $client->setClientId($config['clientId']); $client->setClientSecret($config['clientSecret']); diff --git a/google-drive-service-account.json.example b/google-drive-service-account.json.example index 845bb8f..7f7be6d 100644 --- a/google-drive-service-account.json.example +++ b/google-drive-service-account.json.example @@ -2,5 +2,6 @@ "GOOGLE_DRIVE_CLIENT_ID":"xxxxxx.apps.googleusercontent.com", "GOOGLE_DRIVE_CLIENT_SECRET":"xxxxxx", "GOOGLE_DRIVE_REFRESH_TOKEN":"xxxxxx", - "GOOGLE_DRIVE_TEAM_DRIVE_ID":null + "GOOGLE_DRIVE_TEAM_DRIVE_ID":null, + "GOOGLE_DRIVE_SHARED_FOLDER_ID":null } diff --git a/src/GoogleDriveAdapter.php b/src/GoogleDriveAdapter.php index 25dad6f..2668c88 100644 --- a/src/GoogleDriveAdapter.php +++ b/src/GoogleDriveAdapter.php @@ -281,6 +281,20 @@ public function __construct($service, $root = null, $options = []) // get real root id $this->root = $this->toSingleVirtualPath($root, false, true, true, true); + // reset cache + $this->rootId = $this->root; + $this->clearCache(); + } + } else if (isset($this->options['sharedFolderId'])) { + $this->root = (!$this->useDisplayPaths && $root !== null) + ? $root + : $this->options['sharedFolderId']; + + $this->setPathPrefix(''); + + if ($this->useDisplayPaths && $root !== null) { + // get real root id + $this->root = $this->toSingleVirtualPath($root, false, true, true, true); // reset cache $this->rootId = $this->root; $this->clearCache(); diff --git a/tests/GoogleDriveAdapterTests.php b/tests/GoogleDriveAdapterTests.php index c4fa8ab..425fd56 100644 --- a/tests/GoogleDriveAdapterTests.php +++ b/tests/GoogleDriveAdapterTests.php @@ -49,6 +49,9 @@ protected static function createFilesystemAdapter(): FilesystemAdapter if (!empty($config['GOOGLE_DRIVE_TEAM_DRIVE_ID'] ?? null)) { $options['teamDriveId'] = $config['GOOGLE_DRIVE_TEAM_DRIVE_ID']; } + if (!empty($config['GOOGLE_DRIVE_SHARED_FOLDER_ID'] ?? null)) { + $options['sharedFolderId'] = $config['GOOGLE_DRIVE_SHARED_FOLDER_ID']; + } $client = new \Google\Client(); $client->setClientId($config['GOOGLE_DRIVE_CLIENT_ID']); $client->setClientSecret($config['GOOGLE_DRIVE_CLIENT_SECRET']);