Skip to content

Commit

Permalink
Merge pull request #62 from erikn69/deprecated
Browse files Browse the repository at this point in the history
Fix google deprecations, better support for shared drives
  • Loading branch information
masbug authored Apr 19, 2022
2 parents 81afc0d + 1da5ec3 commit 7451734
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
/phpunit.xml.dist export-ignore
/docs export-ignore
/tests export-ignore
/google-drive-service-account.json.example export-ignore
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ google-drive-service-account.json
build
vendor
.phpunit.result.cache
.php_cs.cache
6 changes: 6 additions & 0 deletions google-drive-service-account.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
28 changes: 21 additions & 7 deletions src/GoogleDriveAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class GoogleDriveAdapter extends AbstractAdapter

'parameters' => [],

'teamDriveId' => null,
'driveId' => null,

'sanitize_chars' => [
// sanitize filename
Expand Down Expand Up @@ -979,6 +979,12 @@ protected function getRawVisibility($path)
$file = $this->getFileObject($path);
$permissions = $file->getPermissions();
$visibility = AdapterInterface::VISIBILITY_PRIVATE;

if (! count($permissions)) {
$permissions = $this->service->permissions->listPermissions($file->getId(), $this->applyDefaultParams([], 'permissions.list'));
$file->setPermissions($permissions);
}

foreach ($permissions as $permission) {
if ($permission->type === $this->publishPermission['type'] && $permission->role === $this->publishPermission['role']) {
$visibility = AdapterInterface::VISIBILITY_PUBLIC;
Expand Down Expand Up @@ -1025,6 +1031,9 @@ protected function unPublish($path)
{
$this->refreshToken();
if (($file = $this->getFileObject($path))) {
if ($this->getRawVisibility($path) !== AdapterInterface::VISIBILITY_PUBLIC) {
return true;
}
$permissions = $file->getPermissions();
try {
foreach ($permissions as $permission) {
Expand Down Expand Up @@ -1284,6 +1293,11 @@ protected function getDownloadUrl($file)
{
if (strpos($file->mimeType, 'application/vnd.google-apps') !== 0) {
$params = $this->applyDefaultParams(['alt' => 'media'], 'files.get');
foreach ($params as $key => $value) {
if (is_bool($value)) {
$params[$key] = $value ? 'true' : 'false';
}
}
return 'https://www.googleapis.com/drive/v3/files/'.$file->getId().'?'.http_build_query($params);
}

Expand Down Expand Up @@ -2109,8 +2123,8 @@ public function enableTeamDriveSupport()
array_fill_keys([
'files.copy', 'files.create', 'files.delete',
'files.trash', 'files.get', 'files.list', 'files.update',
'files.watch'
], ['supportsTeamDrives' => true]),
'files.watch', 'permissions.list'
], ['supportsAllDrives' => true]),
$this->optParams
);
}
Expand All @@ -2126,19 +2140,19 @@ public function enableTeamDriveSupport()
* @see https://developers.google.com/drive/v3/reference/files/list
* @see \Google_Service_Drive_Resource_Files
*/
public function setTeamDriveId($teamDriveId, $corpora = 'teamDrive')
public function setTeamDriveId($teamDriveId, $corpora = 'drive')
{
$this->enableTeamDriveSupport();
$this->optParams = array_merge_recursive($this->optParams, [
'files.list' => [
'corpora' => $corpora,
'includeTeamDriveItems' => true,
'teamDriveId' => $teamDriveId
'includeItemsFromAllDrives' => true,
'driveId' => $teamDriveId
]
]);

if ($this->root === 'root' || $this->root === null) {
$this->setPathPrefix($teamDriveId);
$this->setPathPrefix('');
$this->root = $teamDriveId;
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/GoogleDriveAdapterTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ protected static function createFilesystemAdapter()
self::markTestSkipped("No google service config found in {$file}.");
}
$options = ['usePermanentDelete' => true];
if (!empty($config['teamDriveId'] ?? null)) {
$options['teamDriveId'] = $config['teamDriveId'];
if (!empty($config['GOOGLE_DRIVE_TEAM_DRIVE_ID'] ?? null)) {
$options['teamDriveId'] = $config['GOOGLE_DRIVE_TEAM_DRIVE_ID'];
}
$client = new \Google\Client();
$client->setClientId($config['GOOGLE_DRIVE_CLIENT_ID']);
Expand Down

0 comments on commit 7451734

Please sign in to comment.