diff --git a/.gitattributes b/.gitattributes index 9119de6..49f6e38 100644 --- a/.gitattributes +++ b/.gitattributes @@ -13,3 +13,4 @@ /phpunit.xml.dist export-ignore /docs export-ignore /tests export-ignore +/google-drive-service-account.json.example export-ignore diff --git a/.gitignore b/.gitignore index 2fef677..5133170 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ google-drive-service-account.json build vendor .phpunit.result.cache +.php_cs.cache diff --git a/google-drive-service-account.json.example b/google-drive-service-account.json.example new file mode 100644 index 0000000..845bb8f --- /dev/null +++ b/google-drive-service-account.json.example @@ -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 +} diff --git a/src/GoogleDriveAdapter.php b/src/GoogleDriveAdapter.php index 1778054..a6f9a8e 100644 --- a/src/GoogleDriveAdapter.php +++ b/src/GoogleDriveAdapter.php @@ -87,7 +87,7 @@ class GoogleDriveAdapter extends AbstractAdapter 'parameters' => [], - 'teamDriveId' => null, + 'driveId' => null, 'sanitize_chars' => [ // sanitize filename @@ -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; @@ -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) { @@ -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); } @@ -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 ); } @@ -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; } } diff --git a/tests/GoogleDriveAdapterTests.php b/tests/GoogleDriveAdapterTests.php index f3b4b49..869497d 100644 --- a/tests/GoogleDriveAdapterTests.php +++ b/tests/GoogleDriveAdapterTests.php @@ -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']);