Skip to content

Commit

Permalink
Merge pull request #63 from erikn69/deprecated_2
Browse files Browse the repository at this point in the history
[V2] Fix google deprecations, better support for shared drives
  • Loading branch information
masbug authored Apr 19, 2022
2 parents 8875c1a + 0f82e0d commit cc351c8
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 45 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
}
97 changes: 54 additions & 43 deletions src/GoogleDriveAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class GoogleDriveAdapter implements FilesystemAdapter

'parameters' => [],

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

'sanitize_chars' => [
// sanitize filename
Expand Down Expand Up @@ -494,18 +494,8 @@ public function copy(string $location, string $destination, Config $config): voi
}

$srcFile = $this->cacheFileObjects[$srcId];
$permissions = $srcFile->getPermissions();
$visibility = Visibility::PRIVATE;
try {
foreach ($permissions as $permission) {
if ($permission->type === $this->publishPermission['type'] && $permission->role === $this->publishPermission['role']) {
$visibility = Visibility::PUBLIC;
break;
}
}
} catch (Exception $e) {
// unnecesary
}
$visibility = $this->getRawVisibility($srcFile);

if ($config->get('visibility') === Visibility::PUBLIC || $visibility === Visibility::PUBLIC) {
$this->publish($id);
} else {
Expand All @@ -525,7 +515,7 @@ public function copy(string $location, string $destination, Config $config): voi
public function move(string $source, string $destination, Config $config): void
{
if (!$this->fileExists($source)) {
throw UnableToMoveFile::fromLocationTo($source, $destination, new \Exception("File {$source} not exist."));
throw UnableToMoveFile::fromLocationTo($source, $destination, new Exception("File {$source} not exist."));
}
try {
$this->copy($source, $destination, $config);
Expand Down Expand Up @@ -844,7 +834,7 @@ public function getMetadata(string $path)

private function fileAttributes(string $path, string $type = ''): FileAttributes
{
$exception = new \Exception('Unable to get metadata');
$exception = new Exception('Unable to get metadata');
$prefixedPath = $this->prefixer->prefixPath($path);

try {
Expand Down Expand Up @@ -927,16 +917,11 @@ public function visibility(string $location): FileAttributes
// Unnecesary
}
if (!isset($file) || !$file) {
throw UnableToRetrieveMetadata::visibility($location, '', new \Exception('Error finding the file'));
}
$permissions = $file->getPermissions();
$visibility = Visibility::PRIVATE;
foreach ($permissions as $permission) {
if ($permission->type === $this->publishPermission['type'] && $permission->role === $this->publishPermission['role']) {
$visibility = Visibility::PUBLIC;
break;
}
throw UnableToRetrieveMetadata::visibility($location, '', new Exception('Error finding the file'));
}

$visibility = $this->getRawVisibility($file);

return new FileAttributes(/** @scrutinizer ignore-type */ $path, null, $visibility);
}

Expand Down Expand Up @@ -1020,6 +1005,31 @@ protected function setHasDir($targets, $object)
return $object;
}

/**
* Get the object permissions presented as a visibility.
*
* @param string $path itemId path
* @return string
*/
private function getRawVisibility($file)
{
$permissions = $file->getPermissions();
$visibility = 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 = Visibility::PUBLIC;
break;
}
}
return $visibility;
}

/**
* Publish specified path item
*
Expand All @@ -1030,23 +1040,16 @@ protected function publish($path)
{
$this->refreshToken();
if (($file = $this->getFileObject($path))) {
$permissions = $file->getPermissions();
try {
foreach ($permissions as $permission) {
if ($permission->type === $this->publishPermission['type'] && $permission->role === $this->publishPermission['role']) {
return true;
}
}
} catch (Exception $e) {
// unnecesary
if ($this->getRawVisibility($file) === Visibility::PUBLIC) {
return true;
}
try {
$new_permission = new Permission($this->publishPermission);
if ($permission = $this->service->permissions->create($file->getId(), $new_permission, $this->applyDefaultParams([], 'files.create'))) {
$file->setPermissions([$permission]);
return true;
}
} catch (\Exception $e) {
} catch (Throwable $e) {
return false;
}
}
Expand All @@ -1064,6 +1067,9 @@ protected function unPublish($path)
{
$this->refreshToken();
if (($file = $this->getFileObject($path))) {
if ($this->getRawVisibility($file) !== Visibility::PUBLIC) {
return true;
}
$permissions = $file->getPermissions();
try {
foreach ($permissions as $permission) {
Expand All @@ -1073,7 +1079,7 @@ protected function unPublish($path)
}
$file->setPermissions([]);
return true;
} catch (\Exception $e) {
} catch (Throwable $e) {
return false;
}
}
Expand Down Expand Up @@ -1148,7 +1154,7 @@ protected function normaliseObject(DriveFile $object, $dirname)
break;
}
}
} catch (\Exception $e) {
} catch (Throwable $e) {
// Unnecesary
}
if ($this->useDisplayPaths) {
Expand Down Expand Up @@ -1232,7 +1238,7 @@ protected function getItems($dirname, $recursive = false, $maxResults = 0, $quer
} else {
$pageToken = null;
}
} catch (\Exception $e) {
} catch (Throwable $e) {
$pageToken = null;
}
} while ($pageToken && $maxResults === 0);
Expand Down Expand Up @@ -1317,6 +1323,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 @@ -2149,8 +2160,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 @@ -2166,19 +2177,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 @@ -46,8 +46,8 @@ protected static function createFilesystemAdapter(): FilesystemAdapter
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 cc351c8

Please sign in to comment.