diff options
author | Robin Appelman <robin@icewind.nl> | 2014-11-26 12:17:57 +0100 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2014-11-26 12:17:57 +0100 |
commit | a2457b5fb9f1ef9fda82afc4cf7dc712d14488c9 (patch) | |
tree | db04cfd1e201c6ce8fac96fecd78b3c902e5f2f6 /apps/files_external/lib | |
parent | 4ed0a418a0527e60c5a4dc4df059630fd4880adb (diff) | |
parent | c237acb3953791f77b52f89efb7229e59606fdc0 (diff) | |
download | nextcloud-server-a2457b5fb9f1ef9fda82afc4cf7dc712d14488c9.tar.gz nextcloud-server-a2457b5fb9f1ef9fda82afc4cf7dc712d14488c9.zip |
Merge pull request #6989 from AdamWill/google-1
Migrate Google Drive external storage app to v1.0.6-beta of the google-api-php-client library
Diffstat (limited to 'apps/files_external/lib')
-rw-r--r-- | apps/files_external/lib/google.php | 56 |
1 files changed, 32 insertions, 24 deletions
diff --git a/apps/files_external/lib/google.php b/apps/files_external/lib/google.php index a4337bc937b..c414e34fad4 100644 --- a/apps/files_external/lib/google.php +++ b/apps/files_external/lib/google.php @@ -23,11 +23,12 @@ namespace OC\Files\Storage; set_include_path(get_include_path().PATH_SEPARATOR. \OC_App::getAppPath('files_external').'/3rdparty/google-api-php-client/src'); -require_once 'Google_Client.php'; -require_once 'contrib/Google_DriveService.php'; +require_once 'Google/Client.php'; +require_once 'Google/Service/Drive.php'; class Google extends \OC\Files\Storage\Common { + private $client; private $id; private $service; private $driveFiles; @@ -46,14 +47,19 @@ class Google extends \OC\Files\Storage\Common { && isset($params['client_id']) && isset($params['client_secret']) && isset($params['token']) ) { - $client = new \Google_Client(); - $client->setClientId($params['client_id']); - $client->setClientSecret($params['client_secret']); - $client->setScopes(array('https://www.googleapis.com/auth/drive')); - $client->setUseObjects(true); - $client->setAccessToken($params['token']); + $this->client = new \Google_Client(); + $this->client->setClientId($params['client_id']); + $this->client->setClientSecret($params['client_secret']); + $this->client->setScopes(array('https://www.googleapis.com/auth/drive')); + $this->client->setAccessToken($params['token']); + // if curl isn't available we're likely to run into + // https://github.com/google/google-api-php-client/issues/59 + // - disable gzip to avoid it. + if (!function_exists('curl_version') || !function_exists('curl_exec')) { + $this->client->setClassConfig("Google_Http_Request", "disable_gzip", true); + } // note: API connection is lazy - $this->service = new \Google_DriveService($client); + $this->service = new \Google_Service_Drive($this->client); $token = json_decode($params['token'], true); $this->id = 'google::'.substr($params['client_id'], 0, 30).$token['created']; } else { @@ -66,9 +72,10 @@ class Google extends \OC\Files\Storage\Common { } /** - * Get the Google_DriveFile object for the specified path + * Get the Google_Service_Drive_DriveFile object for the specified path. + * Returns false on failure. * @param string $path - * @return string + * @return \Google_Service_Drive_DriveFile|false */ private function getDriveFile($path) { // Remove leading and trailing slashes @@ -115,7 +122,7 @@ class Google extends \OC\Files\Storage\Common { $pathWithoutExt = substr($path, 0, $pos); $file = $this->getDriveFile($pathWithoutExt); if ($file) { - // Switch cached Google_DriveFile to the correct index + // Switch cached Google_Service_Drive_DriveFile to the correct index unset($this->driveFiles[$pathWithoutExt]); $this->driveFiles[$path] = $file; $parentId = $file->getId(); @@ -133,9 +140,9 @@ class Google extends \OC\Files\Storage\Common { } /** - * Set the Google_DriveFile object in the cache + * Set the Google_Service_Drive_DriveFile object in the cache * @param string $path - * @param Google_DriveFile|false $file + * @param Google_Service_Drive_DriveFile|false $file */ private function setDriveFile($path, $file) { $path = trim($path, '/'); @@ -188,10 +195,10 @@ class Google extends \OC\Files\Storage\Common { if (!$this->is_dir($path)) { $parentFolder = $this->getDriveFile(dirname($path)); if ($parentFolder) { - $folder = new \Google_DriveFile(); + $folder = new \Google_Service_Drive_DriveFile(); $folder->setTitle(basename($path)); $folder->setMimeType(self::FOLDER); - $parent = new \Google_ParentReference(); + $parent = new \Google_Service_Drive_ParentReference(); $parent->setId($parentFolder->getId()); $folder->setParents(array($parent)); $result = $this->service->files->insert($folder); @@ -266,7 +273,7 @@ class Google extends \OC\Files\Storage\Common { $this->onDuplicateFileDetected($filepath); } } else { - // Cache the Google_DriveFile for future use + // Cache the Google_Service_Drive_DriveFile for future use $this->setDriveFile($filepath, $child); $files[] = $name; } @@ -356,7 +363,7 @@ class Google extends \OC\Files\Storage\Common { // Change file parent $parentFolder2 = $this->getDriveFile(dirname($path2)); if ($parentFolder2) { - $parent = new \Google_ParentReference(); + $parent = new \Google_Service_Drive_ParentReference(); $parent->setId($parentFolder2->getId()); $file->setParents(array($parent)); } else { @@ -395,8 +402,8 @@ class Google extends \OC\Files\Storage\Common { $downloadUrl = $file->getDownloadUrl(); } if (isset($downloadUrl)) { - $request = new \Google_HttpRequest($downloadUrl, 'GET', null, null); - $httpRequest = \Google_Client::$io->authenticatedRequest($request); + $request = new \Google_Http_Request($downloadUrl, 'GET', null, null); + $httpRequest = $this->client->getAuth()->authenticatedRequest($request); if ($httpRequest->getResponseHttpCode() == 200) { $tmpFile = \OC_Helper::tmpFile($ext); $data = $httpRequest->getResponseBody(); @@ -440,16 +447,17 @@ class Google extends \OC\Files\Storage\Common { $params = array( 'data' => $data, 'mimeType' => $mimetype, + 'uploadType' => 'media' ); $result = false; if ($this->file_exists($path)) { $file = $this->getDriveFile($path); $result = $this->service->files->update($file->getId(), $file, $params); } else { - $file = new \Google_DriveFile(); + $file = new \Google_Service_Drive_DriveFile(); $file->setTitle(basename($path)); $file->setMimeType($mimetype); - $parent = new \Google_ParentReference(); + $parent = new \Google_Service_Drive_ParentReference(); $parent->setId($parentFolder->getId()); $file->setParents(array($parent)); $result = $this->service->files->insert($file, $params); @@ -509,9 +517,9 @@ class Google extends \OC\Files\Storage\Common { } else { $parentFolder = $this->getDriveFile(dirname($path)); if ($parentFolder) { - $file = new \Google_DriveFile(); + $file = new \Google_Service_Drive_DriveFile(); $file->setTitle(basename($path)); - $parent = new \Google_ParentReference(); + $parent = new \Google_Service_Drive_ParentReference(); $parent->setId($parentFolder->getId()); $file->setParents(array($parent)); $result = $this->service->files->insert($file); |