Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for shared folders #116

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
```

Expand Down Expand Up @@ -159,13 +168,15 @@ 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
#SECOND_GOOGLE_DRIVE_CLIENT_SECRET=xxx
#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`
Expand All @@ -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
Expand Down Expand Up @@ -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']);
Expand Down
3 changes: 2 additions & 1 deletion google-drive-service-account.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 14 additions & 0 deletions src/GoogleDriveAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions tests/GoogleDriveAdapterTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down