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

Refresh token after expire #34

Merged
merged 1 commit into from
Aug 14, 2021
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
34 changes: 33 additions & 1 deletion src/GoogleDriveAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ public function __construct(Google_Service_Drive $service, $root = null, $option
*/
public function getService()
{
$this->refreshToken();
return $this->service;
}

Expand All @@ -281,6 +282,23 @@ public function clearCache()
$this->cacheHasDirs = [];
}

/**
* Allow to refresh tokens to enable long running process
*
* @return void
*/
public function refreshToken()
{
$client = $this->service->getClient();
if ($client->isAccessTokenExpired()) {
if ($client->isUsingApplicationDefaultCredentials()) {
$client->refreshTokenWithAssertion();
} else {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
}
}
}

protected function cleanOptParameters($parameters)
{
$operations = ['files.copy', 'files.create', 'files.delete',
Expand Down Expand Up @@ -380,6 +398,7 @@ public function updateStream($path, $resource, Config $config)
*/
public function rename($path, $newpath)
{
$this->refreshToken();
if ($this->useDisplayPaths) {
$path = $this->toVirtualPath($path, true, true);
$newpathDir = self::dirname($newpath);
Expand Down Expand Up @@ -438,6 +457,7 @@ public function rename($path, $newpath)
*/
public function copy($path, $newpath)
{
$this->refreshToken();
if ($this->useDisplayPaths) {
$srcId = $this->toVirtualPath($path, false, true);
$newpathDir = self::dirname($newpath);
Expand Down Expand Up @@ -491,6 +511,7 @@ public function copy($path, $newpath)
*/
protected function delete_by_id($ids)
{
$this->refreshToken();
$deleted = false;
if (!is_array($ids)) {
$ids = [$ids];
Expand Down Expand Up @@ -623,6 +644,7 @@ public function has($path)
*/
public function read($path)
{
$this->refreshToken();
if ($this->useDisplayPaths) {
$fileId = $this->toVirtualPath($path, false, true);
} else {
Expand All @@ -643,6 +665,7 @@ public function read($path)
*/
public function readStream($path)
{
$this->refreshToken();
if ($this->useDisplayPaths) {
$path = $this->toVirtualPath($path, false, true);
}
Expand Down Expand Up @@ -755,6 +778,7 @@ public function readStream($path)
*/
public function listContents($directory = '', $recursive = false)
{
$this->refreshToken();
if ($this->useDisplayPaths) {
$time = microtime(true);
$vp = [];
Expand Down Expand Up @@ -908,6 +932,7 @@ public function hasDir($path)
*/
protected function setHasDir($targets, $object)
{
$this->refreshToken();
$service = $this->service;
$client = $service->getClient();
$gFiles = $service->files;
Expand Down Expand Up @@ -970,6 +995,7 @@ protected function getRawVisibility($path)
*/
protected function publish($path)
{
$this->refreshToken();
if (($file = $this->getFileObject($path))) {
if ($this->getRawVisibility($path) === AdapterInterface::VISIBILITY_PUBLIC) {
return true;
Expand Down Expand Up @@ -997,6 +1023,7 @@ protected function publish($path)
*/
protected function unPublish($path)
{
$this->refreshToken();
if (($file = $this->getFileObject($path))) {
$permissions = $file->getPermissions();
try {
Expand Down Expand Up @@ -1125,6 +1152,7 @@ protected function normaliseObject(Google_Service_Drive_DriveFile $object, $dirn
*/
protected function getItems($dirname, $recursive = false, $maxResults = 0, $query = '')
{
$this->refreshToken();
[, $itemId] = $this->splitPath($dirname);

$maxResults = min($maxResults, 1000);
Expand Down Expand Up @@ -1198,7 +1226,7 @@ public function getFileObject($path, $checkDir = false)
if (isset($this->cacheFileObjects[$itemId])) {
return $this->cacheFileObjects[$itemId];
}

$this->refreshToken();
$service = $this->service;
$client = $service->getClient();

Expand Down Expand Up @@ -1285,6 +1313,7 @@ protected function getDownloadUrl($file)
*/
protected function createDirectory($name, $parentId)
{
$this->refreshToken();
$file = new Google_Service_Drive_DriveFile();
$file->setName($name);
$file->setParents([
Expand All @@ -1311,6 +1340,7 @@ protected function createDirectory($name, $parentId)
*/
protected function upload($path, $contents, Config $config, $updating = null)
{
$this->refreshToken();
[$parentId, $fileName] = $this->splitPath($path);
$mime = $config->get('mimetype');
$file = new Google_Service_Drive_DriveFile();
Expand Down Expand Up @@ -1423,6 +1453,7 @@ protected function getObjects($ids, $checkDir = false)
}
}
if (!empty($fetch) || $checkDir) {
$this->refreshToken();
$service = $this->service;
$client = $service->getClient();

Expand Down Expand Up @@ -2059,6 +2090,7 @@ protected function applyDefaultParams($params, $cmdName)
*/
public function emptyTrash(array $params = [])
{
$this->refreshToken();
$this->service->files->emptyTrash($this->applyDefaultParams($params, 'files.emptyTrash'));
}

Expand Down
6 changes: 3 additions & 3 deletions tests/GoogleDriveAdapterTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function testCopy()
$this->assertTrue($adapter->has($destination));
// copy of content
$contents = $adapter->read($destination);
$this->assertEquals('content', is_array($contents) && isset($contents['contents']) ? $contents['contents'] : '', "The content of file {$destination} is wrong");
$this->assertEquals('content', $contents['contents'] ?? '', "The content of file {$destination} is wrong");
// copy of visibility
$public = $adapter->getVisibility($destination);
$this->assertIsArray($public);
Expand Down Expand Up @@ -171,7 +171,7 @@ public function testRename(): void
$this->assertEquals('public', $object['visibility']);
// content
$contents = $adapter->read($destination);
$this->assertEquals('content', is_array($contents) && isset($contents['contents']) ? $contents['contents'] : '', "The content of file {$destination} is wrong");
$this->assertEquals('content', $contents['contents'] ?? '', "The content of file {$destination} is wrong");
}

/**
Expand Down Expand Up @@ -435,7 +435,7 @@ public function testWritingReadingFilesWithSpecialPath()
$adapter->write($path, 'contents', new Config());
$contents = $adapter->read($path);

$this->assertEquals('contents', isset($contents['contents']) ? $contents['contents'] : '', $msg);
$this->assertEquals('contents', $contents['contents'] ?? '', $msg);
}
}

Expand Down