@@ -29,12 +29,12 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | class Builtin extends AuthMechanism { |
| 31 | 31 | |
| 32 | - public function __construct(IL10N $l) { |
|
| 33 | - $this |
|
| 34 | - ->setIdentifier('builtin::builtin') |
|
| 35 | - ->setScheme(self::SCHEME_BUILTIN) |
|
| 36 | - ->setText($l->t('Builtin')) |
|
| 37 | - ; |
|
| 38 | - } |
|
| 32 | + public function __construct(IL10N $l) { |
|
| 33 | + $this |
|
| 34 | + ->setIdentifier('builtin::builtin') |
|
| 35 | + ->setScheme(self::SCHEME_BUILTIN) |
|
| 36 | + ->setText($l->t('Builtin')) |
|
| 37 | + ; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | 40 | } |
@@ -51,71 +51,71 @@ |
||
| 51 | 51 | */ |
| 52 | 52 | class AuthMechanism implements \JsonSerializable { |
| 53 | 53 | |
| 54 | - /** Standard authentication schemes */ |
|
| 55 | - const SCHEME_NULL = 'null'; |
|
| 56 | - const SCHEME_BUILTIN = 'builtin'; |
|
| 57 | - const SCHEME_PASSWORD = 'password'; |
|
| 58 | - const SCHEME_OAUTH1 = 'oauth1'; |
|
| 59 | - const SCHEME_OAUTH2 = 'oauth2'; |
|
| 60 | - const SCHEME_PUBLICKEY = 'publickey'; |
|
| 61 | - const SCHEME_OPENSTACK = 'openstack'; |
|
| 54 | + /** Standard authentication schemes */ |
|
| 55 | + const SCHEME_NULL = 'null'; |
|
| 56 | + const SCHEME_BUILTIN = 'builtin'; |
|
| 57 | + const SCHEME_PASSWORD = 'password'; |
|
| 58 | + const SCHEME_OAUTH1 = 'oauth1'; |
|
| 59 | + const SCHEME_OAUTH2 = 'oauth2'; |
|
| 60 | + const SCHEME_PUBLICKEY = 'publickey'; |
|
| 61 | + const SCHEME_OPENSTACK = 'openstack'; |
|
| 62 | 62 | |
| 63 | - use VisibilityTrait; |
|
| 64 | - use FrontendDefinitionTrait; |
|
| 65 | - use StorageModifierTrait; |
|
| 66 | - use IdentifierTrait; |
|
| 63 | + use VisibilityTrait; |
|
| 64 | + use FrontendDefinitionTrait; |
|
| 65 | + use StorageModifierTrait; |
|
| 66 | + use IdentifierTrait; |
|
| 67 | 67 | |
| 68 | - /** @var string */ |
|
| 69 | - protected $scheme; |
|
| 68 | + /** @var string */ |
|
| 69 | + protected $scheme; |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Get the authentication scheme implemented |
|
| 73 | - * See self::SCHEME_* constants |
|
| 74 | - * |
|
| 75 | - * @return string |
|
| 76 | - */ |
|
| 77 | - public function getScheme() { |
|
| 78 | - return $this->scheme; |
|
| 79 | - } |
|
| 71 | + /** |
|
| 72 | + * Get the authentication scheme implemented |
|
| 73 | + * See self::SCHEME_* constants |
|
| 74 | + * |
|
| 75 | + * @return string |
|
| 76 | + */ |
|
| 77 | + public function getScheme() { |
|
| 78 | + return $this->scheme; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * @param string $scheme |
|
| 83 | - * @return self |
|
| 84 | - */ |
|
| 85 | - public function setScheme($scheme) { |
|
| 86 | - $this->scheme = $scheme; |
|
| 87 | - return $this; |
|
| 88 | - } |
|
| 81 | + /** |
|
| 82 | + * @param string $scheme |
|
| 83 | + * @return self |
|
| 84 | + */ |
|
| 85 | + public function setScheme($scheme) { |
|
| 86 | + $this->scheme = $scheme; |
|
| 87 | + return $this; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Serialize into JSON for client-side JS |
|
| 92 | - * |
|
| 93 | - * @return array |
|
| 94 | - */ |
|
| 95 | - public function jsonSerialize() { |
|
| 96 | - $data = $this->jsonSerializeDefinition(); |
|
| 97 | - $data += $this->jsonSerializeIdentifier(); |
|
| 90 | + /** |
|
| 91 | + * Serialize into JSON for client-side JS |
|
| 92 | + * |
|
| 93 | + * @return array |
|
| 94 | + */ |
|
| 95 | + public function jsonSerialize() { |
|
| 96 | + $data = $this->jsonSerializeDefinition(); |
|
| 97 | + $data += $this->jsonSerializeIdentifier(); |
|
| 98 | 98 | |
| 99 | - $data['scheme'] = $this->getScheme(); |
|
| 100 | - $data['visibility'] = $this->getVisibility(); |
|
| 99 | + $data['scheme'] = $this->getScheme(); |
|
| 100 | + $data['visibility'] = $this->getVisibility(); |
|
| 101 | 101 | |
| 102 | - return $data; |
|
| 103 | - } |
|
| 102 | + return $data; |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - /** |
|
| 106 | - * Check if parameters are satisfied in a StorageConfig |
|
| 107 | - * |
|
| 108 | - * @param StorageConfig $storage |
|
| 109 | - * @return bool |
|
| 110 | - */ |
|
| 111 | - public function validateStorage(StorageConfig $storage) { |
|
| 112 | - // does the backend actually support this scheme |
|
| 113 | - $supportedSchemes = $storage->getBackend()->getAuthSchemes(); |
|
| 114 | - if (!isset($supportedSchemes[$this->getScheme()])) { |
|
| 115 | - return false; |
|
| 116 | - } |
|
| 105 | + /** |
|
| 106 | + * Check if parameters are satisfied in a StorageConfig |
|
| 107 | + * |
|
| 108 | + * @param StorageConfig $storage |
|
| 109 | + * @return bool |
|
| 110 | + */ |
|
| 111 | + public function validateStorage(StorageConfig $storage) { |
|
| 112 | + // does the backend actually support this scheme |
|
| 113 | + $supportedSchemes = $storage->getBackend()->getAuthSchemes(); |
|
| 114 | + if (!isset($supportedSchemes[$this->getScheme()])) { |
|
| 115 | + return false; |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | - return $this->validateStorageDefinition($storage); |
|
| 119 | - } |
|
| 118 | + return $this->validateStorageDefinition($storage); |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | 121 | } |
@@ -29,12 +29,12 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | class NullMechanism extends AuthMechanism { |
| 31 | 31 | |
| 32 | - public function __construct(IL10N $l) { |
|
| 33 | - $this |
|
| 34 | - ->setIdentifier('null::null') |
|
| 35 | - ->setScheme(self::SCHEME_NULL) |
|
| 36 | - ->setText($l->t('None')) |
|
| 37 | - ; |
|
| 38 | - } |
|
| 32 | + public function __construct(IL10N $l) { |
|
| 33 | + $this |
|
| 34 | + ->setIdentifier('null::null') |
|
| 35 | + ->setScheme(self::SCHEME_NULL) |
|
| 36 | + ->setText($l->t('None')) |
|
| 37 | + ; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | 40 | } |
@@ -29,33 +29,33 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | trait PriorityTrait { |
| 31 | 31 | |
| 32 | - /** @var int initial priority */ |
|
| 33 | - protected $priority = BackendService::PRIORITY_DEFAULT; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @return int |
|
| 37 | - */ |
|
| 38 | - public function getPriority() { |
|
| 39 | - return $this->priority; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @param int $priority |
|
| 44 | - * @return self |
|
| 45 | - */ |
|
| 46 | - public function setPriority($priority) { |
|
| 47 | - $this->priority = $priority; |
|
| 48 | - return $this; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @param PriorityTrait $a |
|
| 53 | - * @param PriorityTrait $b |
|
| 54 | - * @return int |
|
| 55 | - */ |
|
| 56 | - public static function priorityCompare(PriorityTrait $a, PriorityTrait $b) { |
|
| 57 | - return ($a->getPriority() - $b->getPriority()); |
|
| 58 | - } |
|
| 32 | + /** @var int initial priority */ |
|
| 33 | + protected $priority = BackendService::PRIORITY_DEFAULT; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @return int |
|
| 37 | + */ |
|
| 38 | + public function getPriority() { |
|
| 39 | + return $this->priority; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @param int $priority |
|
| 44 | + * @return self |
|
| 45 | + */ |
|
| 46 | + public function setPriority($priority) { |
|
| 47 | + $this->priority = $priority; |
|
| 48 | + return $this; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @param PriorityTrait $a |
|
| 53 | + * @param PriorityTrait $b |
|
| 54 | + * @return int |
|
| 55 | + */ |
|
| 56 | + public static function priorityCompare(PriorityTrait $a, PriorityTrait $b) { |
|
| 57 | + return ($a->getPriority() - $b->getPriority()); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | 60 | } |
| 61 | 61 | |
@@ -30,10 +30,10 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | interface IBackendProvider { |
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @since 9.1.0 |
|
| 35 | - * @return Backend[] |
|
| 36 | - */ |
|
| 37 | - public function getBackends(); |
|
| 33 | + /** |
|
| 34 | + * @since 9.1.0 |
|
| 35 | + * @return Backend[] |
|
| 36 | + */ |
|
| 37 | + public function getBackends(); |
|
| 38 | 38 | |
| 39 | 39 | } |
@@ -30,10 +30,10 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | interface IAuthMechanismProvider { |
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @since 9.1.0 |
|
| 35 | - * @return AuthMechanism[] |
|
| 36 | - */ |
|
| 37 | - public function getAuthMechanisms(); |
|
| 33 | + /** |
|
| 34 | + * @since 9.1.0 |
|
| 35 | + * @return AuthMechanism[] |
|
| 36 | + */ |
|
| 37 | + public function getAuthMechanisms(); |
|
| 38 | 38 | |
| 39 | 39 | } |
@@ -31,14 +31,14 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | class SessionStorageWrapper extends PermissionsMask { |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @param array $arguments ['storage' => $storage] |
|
| 36 | - */ |
|
| 37 | - public function __construct($arguments) { |
|
| 38 | - // disable sharing permission |
|
| 39 | - $arguments['mask'] = Constants::PERMISSION_ALL & ~Constants::PERMISSION_SHARE; |
|
| 40 | - parent::__construct($arguments); |
|
| 41 | - } |
|
| 34 | + /** |
|
| 35 | + * @param array $arguments ['storage' => $storage] |
|
| 36 | + */ |
|
| 37 | + public function __construct($arguments) { |
|
| 38 | + // disable sharing permission |
|
| 39 | + $arguments['mask'] = Constants::PERMISSION_ALL & ~Constants::PERMISSION_SHARE; |
|
| 40 | + parent::__construct($arguments); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | 43 | } |
| 44 | 44 | |
@@ -29,15 +29,15 @@ |
||
| 29 | 29 | * Authentication mechanism or backend has insufficient data |
| 30 | 30 | */ |
| 31 | 31 | class InsufficientDataForMeaningfulAnswerException extends StorageNotAvailableException { |
| 32 | - /** |
|
| 33 | - * StorageNotAvailableException constructor. |
|
| 34 | - * |
|
| 35 | - * @param string $message |
|
| 36 | - * @param int $code |
|
| 37 | - * @param \Exception $previous |
|
| 38 | - * @since 6.0.0 |
|
| 39 | - */ |
|
| 40 | - public function __construct($message = '', $code = self::STATUS_INDETERMINATE, \Exception $previous = null) { |
|
| 41 | - parent::__construct($message, $code, $previous); |
|
| 42 | - } |
|
| 32 | + /** |
|
| 33 | + * StorageNotAvailableException constructor. |
|
| 34 | + * |
|
| 35 | + * @param string $message |
|
| 36 | + * @param int $code |
|
| 37 | + * @param \Exception $previous |
|
| 38 | + * @since 6.0.0 |
|
| 39 | + */ |
|
| 40 | + public function __construct($message = '', $code = self::STATUS_INDETERMINATE, \Exception $previous = null) { |
|
| 41 | + parent::__construct($message, $code, $previous); |
|
| 42 | + } |
|
| 43 | 43 | } |
@@ -41,684 +41,684 @@ |
||
| 41 | 41 | use Icewind\Streams\RetryWrapper; |
| 42 | 42 | |
| 43 | 43 | set_include_path(get_include_path().PATH_SEPARATOR. |
| 44 | - \OC_App::getAppPath('files_external').'/3rdparty/google-api-php-client/src'); |
|
| 44 | + \OC_App::getAppPath('files_external').'/3rdparty/google-api-php-client/src'); |
|
| 45 | 45 | require_once 'Google/autoload.php'; |
| 46 | 46 | |
| 47 | 47 | class Google extends \OC\Files\Storage\Common { |
| 48 | 48 | |
| 49 | - private $client; |
|
| 50 | - private $id; |
|
| 51 | - private $service; |
|
| 52 | - private $driveFiles; |
|
| 53 | - |
|
| 54 | - // Google Doc mimetypes |
|
| 55 | - const FOLDER = 'application/vnd.google-apps.folder'; |
|
| 56 | - const DOCUMENT = 'application/vnd.google-apps.document'; |
|
| 57 | - const SPREADSHEET = 'application/vnd.google-apps.spreadsheet'; |
|
| 58 | - const DRAWING = 'application/vnd.google-apps.drawing'; |
|
| 59 | - const PRESENTATION = 'application/vnd.google-apps.presentation'; |
|
| 60 | - const MAP = 'application/vnd.google-apps.map'; |
|
| 61 | - |
|
| 62 | - public function __construct($params) { |
|
| 63 | - if (isset($params['configured']) && $params['configured'] === 'true' |
|
| 64 | - && isset($params['client_id']) && isset($params['client_secret']) |
|
| 65 | - && isset($params['token']) |
|
| 66 | - ) { |
|
| 67 | - $this->client = new \Google_Client(); |
|
| 68 | - $this->client->setClientId($params['client_id']); |
|
| 69 | - $this->client->setClientSecret($params['client_secret']); |
|
| 70 | - $this->client->setScopes(array('https://www.googleapis.com/auth/drive')); |
|
| 71 | - $this->client->setAccessToken($params['token']); |
|
| 72 | - // if curl isn't available we're likely to run into |
|
| 73 | - // https://github.com/google/google-api-php-client/issues/59 |
|
| 74 | - // - disable gzip to avoid it. |
|
| 75 | - if (!function_exists('curl_version') || !function_exists('curl_exec')) { |
|
| 76 | - $this->client->setClassConfig("Google_Http_Request", "disable_gzip", true); |
|
| 77 | - } |
|
| 78 | - // note: API connection is lazy |
|
| 79 | - $this->service = new \Google_Service_Drive($this->client); |
|
| 80 | - $token = json_decode($params['token'], true); |
|
| 81 | - $this->id = 'google::'.substr($params['client_id'], 0, 30).$token['created']; |
|
| 82 | - } else { |
|
| 83 | - throw new \Exception('Creating Google storage failed'); |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - public function getId() { |
|
| 88 | - return $this->id; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Get the Google_Service_Drive_DriveFile object for the specified path. |
|
| 93 | - * Returns false on failure. |
|
| 94 | - * @param string $path |
|
| 95 | - * @return \Google_Service_Drive_DriveFile|false |
|
| 96 | - */ |
|
| 97 | - private function getDriveFile($path) { |
|
| 98 | - // Remove leading and trailing slashes |
|
| 99 | - $path = trim($path, '/'); |
|
| 100 | - if ($path === '.') { |
|
| 101 | - $path = ''; |
|
| 102 | - } |
|
| 103 | - if (isset($this->driveFiles[$path])) { |
|
| 104 | - return $this->driveFiles[$path]; |
|
| 105 | - } else if ($path === '') { |
|
| 106 | - $root = $this->service->files->get('root'); |
|
| 107 | - $this->driveFiles[$path] = $root; |
|
| 108 | - return $root; |
|
| 109 | - } else { |
|
| 110 | - // Google Drive SDK does not have methods for retrieving files by path |
|
| 111 | - // Instead we must find the id of the parent folder of the file |
|
| 112 | - $parentId = $this->getDriveFile('')->getId(); |
|
| 113 | - $folderNames = explode('/', $path); |
|
| 114 | - $path = ''; |
|
| 115 | - // Loop through each folder of this path to get to the file |
|
| 116 | - foreach ($folderNames as $name) { |
|
| 117 | - // Reconstruct path from beginning |
|
| 118 | - if ($path === '') { |
|
| 119 | - $path .= $name; |
|
| 120 | - } else { |
|
| 121 | - $path .= '/'.$name; |
|
| 122 | - } |
|
| 123 | - if (isset($this->driveFiles[$path])) { |
|
| 124 | - $parentId = $this->driveFiles[$path]->getId(); |
|
| 125 | - } else { |
|
| 126 | - $q = "title='" . str_replace("'","\\'", $name) . "' and '" . str_replace("'","\\'", $parentId) . "' in parents and trashed = false"; |
|
| 127 | - $result = $this->service->files->listFiles(array('q' => $q))->getItems(); |
|
| 128 | - if (!empty($result)) { |
|
| 129 | - // Google Drive allows files with the same name, ownCloud doesn't |
|
| 130 | - if (count($result) > 1) { |
|
| 131 | - $this->onDuplicateFileDetected($path); |
|
| 132 | - return false; |
|
| 133 | - } else { |
|
| 134 | - $file = current($result); |
|
| 135 | - $this->driveFiles[$path] = $file; |
|
| 136 | - $parentId = $file->getId(); |
|
| 137 | - } |
|
| 138 | - } else { |
|
| 139 | - // Google Docs have no extension in their title, so try without extension |
|
| 140 | - $pos = strrpos($path, '.'); |
|
| 141 | - if ($pos !== false) { |
|
| 142 | - $pathWithoutExt = substr($path, 0, $pos); |
|
| 143 | - $file = $this->getDriveFile($pathWithoutExt); |
|
| 144 | - if ($file && $this->isGoogleDocFile($file)) { |
|
| 145 | - // Switch cached Google_Service_Drive_DriveFile to the correct index |
|
| 146 | - unset($this->driveFiles[$pathWithoutExt]); |
|
| 147 | - $this->driveFiles[$path] = $file; |
|
| 148 | - $parentId = $file->getId(); |
|
| 149 | - } else { |
|
| 150 | - return false; |
|
| 151 | - } |
|
| 152 | - } else { |
|
| 153 | - return false; |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - return $this->driveFiles[$path]; |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Set the Google_Service_Drive_DriveFile object in the cache |
|
| 164 | - * @param string $path |
|
| 165 | - * @param \Google_Service_Drive_DriveFile|false $file |
|
| 166 | - */ |
|
| 167 | - private function setDriveFile($path, $file) { |
|
| 168 | - $path = trim($path, '/'); |
|
| 169 | - $this->driveFiles[$path] = $file; |
|
| 170 | - if ($file === false) { |
|
| 171 | - // Remove all children |
|
| 172 | - $len = strlen($path); |
|
| 173 | - foreach ($this->driveFiles as $key => $file) { |
|
| 174 | - if (substr($key, 0, $len) === $path) { |
|
| 175 | - unset($this->driveFiles[$key]); |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - } |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Write a log message to inform about duplicate file names |
|
| 183 | - * @param string $path |
|
| 184 | - */ |
|
| 185 | - private function onDuplicateFileDetected($path) { |
|
| 186 | - $about = $this->service->about->get(); |
|
| 187 | - $user = $about->getName(); |
|
| 188 | - \OCP\Util::writeLog('files_external', |
|
| 189 | - 'Ignoring duplicate file name: '.$path.' on Google Drive for Google user: '.$user, |
|
| 190 | - \OCP\Util::INFO |
|
| 191 | - ); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Generate file extension for a Google Doc, choosing Open Document formats for download |
|
| 196 | - * @param string $mimetype |
|
| 197 | - * @return string |
|
| 198 | - */ |
|
| 199 | - private function getGoogleDocExtension($mimetype) { |
|
| 200 | - if ($mimetype === self::DOCUMENT) { |
|
| 201 | - return 'odt'; |
|
| 202 | - } else if ($mimetype === self::SPREADSHEET) { |
|
| 203 | - return 'ods'; |
|
| 204 | - } else if ($mimetype === self::DRAWING) { |
|
| 205 | - return 'jpg'; |
|
| 206 | - } else if ($mimetype === self::PRESENTATION) { |
|
| 207 | - // Download as .odp is not available |
|
| 208 | - return 'pdf'; |
|
| 209 | - } else { |
|
| 210 | - return ''; |
|
| 211 | - } |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Returns whether the given drive file is a Google Doc file |
|
| 216 | - * |
|
| 217 | - * @param \Google_Service_Drive_DriveFile |
|
| 218 | - * |
|
| 219 | - * @return true if the file is a Google Doc file, false otherwise |
|
| 220 | - */ |
|
| 221 | - private function isGoogleDocFile($file) { |
|
| 222 | - return $this->getGoogleDocExtension($file->getMimeType()) !== ''; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - public function mkdir($path) { |
|
| 226 | - if (!$this->is_dir($path)) { |
|
| 227 | - $parentFolder = $this->getDriveFile(dirname($path)); |
|
| 228 | - if ($parentFolder) { |
|
| 229 | - $folder = new \Google_Service_Drive_DriveFile(); |
|
| 230 | - $folder->setTitle(basename($path)); |
|
| 231 | - $folder->setMimeType(self::FOLDER); |
|
| 232 | - $parent = new \Google_Service_Drive_ParentReference(); |
|
| 233 | - $parent->setId($parentFolder->getId()); |
|
| 234 | - $folder->setParents(array($parent)); |
|
| 235 | - $result = $this->service->files->insert($folder); |
|
| 236 | - if ($result) { |
|
| 237 | - $this->setDriveFile($path, $result); |
|
| 238 | - } |
|
| 239 | - return (bool)$result; |
|
| 240 | - } |
|
| 241 | - } |
|
| 242 | - return false; |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - public function rmdir($path) { |
|
| 246 | - if (!$this->isDeletable($path)) { |
|
| 247 | - return false; |
|
| 248 | - } |
|
| 249 | - if (trim($path, '/') === '') { |
|
| 250 | - $dir = $this->opendir($path); |
|
| 251 | - if(is_resource($dir)) { |
|
| 252 | - while (($file = readdir($dir)) !== false) { |
|
| 253 | - if (!\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
| 254 | - if (!$this->unlink($path.'/'.$file)) { |
|
| 255 | - return false; |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - closedir($dir); |
|
| 260 | - } |
|
| 261 | - $this->driveFiles = array(); |
|
| 262 | - return true; |
|
| 263 | - } else { |
|
| 264 | - return $this->unlink($path); |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - public function opendir($path) { |
|
| 269 | - $folder = $this->getDriveFile($path); |
|
| 270 | - if ($folder) { |
|
| 271 | - $files = array(); |
|
| 272 | - $duplicates = array(); |
|
| 273 | - $pageToken = true; |
|
| 274 | - while ($pageToken) { |
|
| 275 | - $params = array(); |
|
| 276 | - if ($pageToken !== true) { |
|
| 277 | - $params['pageToken'] = $pageToken; |
|
| 278 | - } |
|
| 279 | - $params['q'] = "'" . str_replace("'","\\'", $folder->getId()) . "' in parents and trashed = false"; |
|
| 280 | - $children = $this->service->files->listFiles($params); |
|
| 281 | - foreach ($children->getItems() as $child) { |
|
| 282 | - $name = $child->getTitle(); |
|
| 283 | - // Check if this is a Google Doc i.e. no extension in name |
|
| 284 | - $extension = $child->getFileExtension(); |
|
| 285 | - if (empty($extension)) { |
|
| 286 | - if ($child->getMimeType() === self::MAP) { |
|
| 287 | - continue; // No method known to transfer map files, ignore it |
|
| 288 | - } else if ($child->getMimeType() !== self::FOLDER) { |
|
| 289 | - $name .= '.'.$this->getGoogleDocExtension($child->getMimeType()); |
|
| 290 | - } |
|
| 291 | - } |
|
| 292 | - if ($path === '') { |
|
| 293 | - $filepath = $name; |
|
| 294 | - } else { |
|
| 295 | - $filepath = $path.'/'.$name; |
|
| 296 | - } |
|
| 297 | - // Google Drive allows files with the same name, ownCloud doesn't |
|
| 298 | - // Prevent opendir() from returning any duplicate files |
|
| 299 | - $key = array_search($name, $files); |
|
| 300 | - if ($key !== false || isset($duplicates[$filepath])) { |
|
| 301 | - if (!isset($duplicates[$filepath])) { |
|
| 302 | - $duplicates[$filepath] = true; |
|
| 303 | - $this->setDriveFile($filepath, false); |
|
| 304 | - unset($files[$key]); |
|
| 305 | - $this->onDuplicateFileDetected($filepath); |
|
| 306 | - } |
|
| 307 | - } else { |
|
| 308 | - // Cache the Google_Service_Drive_DriveFile for future use |
|
| 309 | - $this->setDriveFile($filepath, $child); |
|
| 310 | - $files[] = $name; |
|
| 311 | - } |
|
| 312 | - } |
|
| 313 | - $pageToken = $children->getNextPageToken(); |
|
| 314 | - } |
|
| 315 | - return IteratorDirectory::wrap($files); |
|
| 316 | - } else { |
|
| 317 | - return false; |
|
| 318 | - } |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - public function stat($path) { |
|
| 322 | - $file = $this->getDriveFile($path); |
|
| 323 | - if ($file) { |
|
| 324 | - $stat = array(); |
|
| 325 | - if ($this->filetype($path) === 'dir') { |
|
| 326 | - $stat['size'] = 0; |
|
| 327 | - } else { |
|
| 328 | - // Check if this is a Google Doc |
|
| 329 | - if ($this->isGoogleDocFile($file)) { |
|
| 330 | - // Return unknown file size |
|
| 331 | - $stat['size'] = \OCP\Files\FileInfo::SPACE_UNKNOWN; |
|
| 332 | - } else { |
|
| 333 | - $stat['size'] = $file->getFileSize(); |
|
| 334 | - } |
|
| 335 | - } |
|
| 336 | - $stat['atime'] = strtotime($file->getLastViewedByMeDate()); |
|
| 337 | - $stat['mtime'] = strtotime($file->getModifiedDate()); |
|
| 338 | - $stat['ctime'] = strtotime($file->getCreatedDate()); |
|
| 339 | - return $stat; |
|
| 340 | - } else { |
|
| 341 | - return false; |
|
| 342 | - } |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - public function filetype($path) { |
|
| 346 | - if ($path === '') { |
|
| 347 | - return 'dir'; |
|
| 348 | - } else { |
|
| 349 | - $file = $this->getDriveFile($path); |
|
| 350 | - if ($file) { |
|
| 351 | - if ($file->getMimeType() === self::FOLDER) { |
|
| 352 | - return 'dir'; |
|
| 353 | - } else { |
|
| 354 | - return 'file'; |
|
| 355 | - } |
|
| 356 | - } else { |
|
| 357 | - return false; |
|
| 358 | - } |
|
| 359 | - } |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - public function isUpdatable($path) { |
|
| 363 | - $file = $this->getDriveFile($path); |
|
| 364 | - if ($file) { |
|
| 365 | - return $file->getEditable(); |
|
| 366 | - } else { |
|
| 367 | - return false; |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - public function file_exists($path) { |
|
| 372 | - return (bool)$this->getDriveFile($path); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - public function unlink($path) { |
|
| 376 | - $file = $this->getDriveFile($path); |
|
| 377 | - if ($file) { |
|
| 378 | - $result = $this->service->files->trash($file->getId()); |
|
| 379 | - if ($result) { |
|
| 380 | - $this->setDriveFile($path, false); |
|
| 381 | - } |
|
| 382 | - return (bool)$result; |
|
| 383 | - } else { |
|
| 384 | - return false; |
|
| 385 | - } |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - public function rename($path1, $path2) { |
|
| 389 | - $file = $this->getDriveFile($path1); |
|
| 390 | - if ($file) { |
|
| 391 | - $newFile = $this->getDriveFile($path2); |
|
| 392 | - if (dirname($path1) === dirname($path2)) { |
|
| 393 | - if ($newFile) { |
|
| 394 | - // rename to the name of the target file, could be an office file without extension |
|
| 395 | - $file->setTitle($newFile->getTitle()); |
|
| 396 | - } else { |
|
| 397 | - $file->setTitle(basename(($path2))); |
|
| 398 | - } |
|
| 399 | - } else { |
|
| 400 | - // Change file parent |
|
| 401 | - $parentFolder2 = $this->getDriveFile(dirname($path2)); |
|
| 402 | - if ($parentFolder2) { |
|
| 403 | - $parent = new \Google_Service_Drive_ParentReference(); |
|
| 404 | - $parent->setId($parentFolder2->getId()); |
|
| 405 | - $file->setParents(array($parent)); |
|
| 406 | - } else { |
|
| 407 | - return false; |
|
| 408 | - } |
|
| 409 | - } |
|
| 410 | - // We need to get the object for the existing file with the same |
|
| 411 | - // name (if there is one) before we do the patch. If oldfile |
|
| 412 | - // exists and is a directory we have to delete it before we |
|
| 413 | - // do the rename too. |
|
| 414 | - $oldfile = $this->getDriveFile($path2); |
|
| 415 | - if ($oldfile && $this->is_dir($path2)) { |
|
| 416 | - $this->rmdir($path2); |
|
| 417 | - $oldfile = false; |
|
| 418 | - } |
|
| 419 | - $result = $this->service->files->patch($file->getId(), $file); |
|
| 420 | - if ($result) { |
|
| 421 | - $this->setDriveFile($path1, false); |
|
| 422 | - $this->setDriveFile($path2, $result); |
|
| 423 | - if ($oldfile && $newFile) { |
|
| 424 | - // only delete if they have a different id (same id can happen for part files) |
|
| 425 | - if ($newFile->getId() !== $oldfile->getId()) { |
|
| 426 | - $this->service->files->delete($oldfile->getId()); |
|
| 427 | - } |
|
| 428 | - } |
|
| 429 | - } |
|
| 430 | - return (bool)$result; |
|
| 431 | - } else { |
|
| 432 | - return false; |
|
| 433 | - } |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - public function fopen($path, $mode) { |
|
| 437 | - $pos = strrpos($path, '.'); |
|
| 438 | - if ($pos !== false) { |
|
| 439 | - $ext = substr($path, $pos); |
|
| 440 | - } else { |
|
| 441 | - $ext = ''; |
|
| 442 | - } |
|
| 443 | - switch ($mode) { |
|
| 444 | - case 'r': |
|
| 445 | - case 'rb': |
|
| 446 | - $file = $this->getDriveFile($path); |
|
| 447 | - if ($file) { |
|
| 448 | - $exportLinks = $file->getExportLinks(); |
|
| 449 | - $mimetype = $this->getMimeType($path); |
|
| 450 | - $downloadUrl = null; |
|
| 451 | - if ($exportLinks && isset($exportLinks[$mimetype])) { |
|
| 452 | - $downloadUrl = $exportLinks[$mimetype]; |
|
| 453 | - } else { |
|
| 454 | - $downloadUrl = $file->getDownloadUrl(); |
|
| 455 | - } |
|
| 456 | - if (isset($downloadUrl)) { |
|
| 457 | - $request = new \Google_Http_Request($downloadUrl, 'GET', null, null); |
|
| 458 | - $httpRequest = $this->client->getAuth()->sign($request); |
|
| 459 | - // the library's service doesn't support streaming, so we use Guzzle instead |
|
| 460 | - $client = \OC::$server->getHTTPClientService()->newClient(); |
|
| 461 | - try { |
|
| 462 | - $response = $client->get($downloadUrl, [ |
|
| 463 | - 'headers' => $httpRequest->getRequestHeaders(), |
|
| 464 | - 'stream' => true, |
|
| 465 | - 'verify' => realpath(__DIR__ . '/../../../3rdparty/google-api-php-client/src/Google/IO/cacerts.pem'), |
|
| 466 | - ]); |
|
| 467 | - } catch (RequestException $e) { |
|
| 468 | - if(!is_null($e->getResponse())) { |
|
| 469 | - if ($e->getResponse()->getStatusCode() === 404) { |
|
| 470 | - return false; |
|
| 471 | - } else { |
|
| 472 | - throw $e; |
|
| 473 | - } |
|
| 474 | - } else { |
|
| 475 | - throw $e; |
|
| 476 | - } |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - $handle = $response->getBody(); |
|
| 480 | - return RetryWrapper::wrap($handle); |
|
| 481 | - } |
|
| 482 | - } |
|
| 483 | - return false; |
|
| 484 | - case 'w': |
|
| 485 | - case 'wb': |
|
| 486 | - case 'a': |
|
| 487 | - case 'ab': |
|
| 488 | - case 'r+': |
|
| 489 | - case 'w+': |
|
| 490 | - case 'wb+': |
|
| 491 | - case 'a+': |
|
| 492 | - case 'x': |
|
| 493 | - case 'x+': |
|
| 494 | - case 'c': |
|
| 495 | - case 'c+': |
|
| 496 | - $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 497 | - if ($this->file_exists($path)) { |
|
| 498 | - $source = $this->fopen($path, 'rb'); |
|
| 499 | - file_put_contents($tmpFile, $source); |
|
| 500 | - } |
|
| 501 | - $handle = fopen($tmpFile, $mode); |
|
| 502 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 503 | - $this->writeBack($tmpFile, $path); |
|
| 504 | - }); |
|
| 505 | - } |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - public function writeBack($tmpFile, $path) { |
|
| 509 | - $parentFolder = $this->getDriveFile(dirname($path)); |
|
| 510 | - if ($parentFolder) { |
|
| 511 | - $mimetype = \OC::$server->getMimeTypeDetector()->detect($tmpFile); |
|
| 512 | - $params = array( |
|
| 513 | - 'mimeType' => $mimetype, |
|
| 514 | - 'uploadType' => 'media' |
|
| 515 | - ); |
|
| 516 | - $result = false; |
|
| 517 | - |
|
| 518 | - $chunkSizeBytes = 10 * 1024 * 1024; |
|
| 519 | - |
|
| 520 | - $useChunking = false; |
|
| 521 | - $size = filesize($tmpFile); |
|
| 522 | - if ($size > $chunkSizeBytes) { |
|
| 523 | - $useChunking = true; |
|
| 524 | - } else { |
|
| 525 | - $params['data'] = file_get_contents($tmpFile); |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - if ($this->file_exists($path)) { |
|
| 529 | - $file = $this->getDriveFile($path); |
|
| 530 | - $this->client->setDefer($useChunking); |
|
| 531 | - $request = $this->service->files->update($file->getId(), $file, $params); |
|
| 532 | - } else { |
|
| 533 | - $file = new \Google_Service_Drive_DriveFile(); |
|
| 534 | - $file->setTitle(basename($path)); |
|
| 535 | - $file->setMimeType($mimetype); |
|
| 536 | - $parent = new \Google_Service_Drive_ParentReference(); |
|
| 537 | - $parent->setId($parentFolder->getId()); |
|
| 538 | - $file->setParents(array($parent)); |
|
| 539 | - $this->client->setDefer($useChunking); |
|
| 540 | - $request = $this->service->files->insert($file, $params); |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - if ($useChunking) { |
|
| 544 | - // Create a media file upload to represent our upload process. |
|
| 545 | - $media = new \Google_Http_MediaFileUpload( |
|
| 546 | - $this->client, |
|
| 547 | - $request, |
|
| 548 | - 'text/plain', |
|
| 549 | - null, |
|
| 550 | - true, |
|
| 551 | - $chunkSizeBytes |
|
| 552 | - ); |
|
| 553 | - $media->setFileSize($size); |
|
| 554 | - |
|
| 555 | - // Upload the various chunks. $status will be false until the process is |
|
| 556 | - // complete. |
|
| 557 | - $status = false; |
|
| 558 | - $handle = fopen($tmpFile, 'rb'); |
|
| 559 | - while (!$status && !feof($handle)) { |
|
| 560 | - $chunk = fread($handle, $chunkSizeBytes); |
|
| 561 | - $status = $media->nextChunk($chunk); |
|
| 562 | - } |
|
| 563 | - |
|
| 564 | - // The final value of $status will be the data from the API for the object |
|
| 565 | - // that has been uploaded. |
|
| 566 | - $result = false; |
|
| 567 | - if ($status !== false) { |
|
| 568 | - $result = $status; |
|
| 569 | - } |
|
| 570 | - |
|
| 571 | - fclose($handle); |
|
| 572 | - } else { |
|
| 573 | - $result = $request; |
|
| 574 | - } |
|
| 575 | - |
|
| 576 | - // Reset to the client to execute requests immediately in the future. |
|
| 577 | - $this->client->setDefer(false); |
|
| 578 | - |
|
| 579 | - if ($result) { |
|
| 580 | - $this->setDriveFile($path, $result); |
|
| 581 | - } |
|
| 582 | - } |
|
| 583 | - } |
|
| 584 | - |
|
| 585 | - public function getMimeType($path) { |
|
| 586 | - $file = $this->getDriveFile($path); |
|
| 587 | - if ($file) { |
|
| 588 | - $mimetype = $file->getMimeType(); |
|
| 589 | - // Convert Google Doc mimetypes, choosing Open Document formats for download |
|
| 590 | - if ($mimetype === self::FOLDER) { |
|
| 591 | - return 'httpd/unix-directory'; |
|
| 592 | - } else if ($mimetype === self::DOCUMENT) { |
|
| 593 | - return 'application/vnd.oasis.opendocument.text'; |
|
| 594 | - } else if ($mimetype === self::SPREADSHEET) { |
|
| 595 | - return 'application/x-vnd.oasis.opendocument.spreadsheet'; |
|
| 596 | - } else if ($mimetype === self::DRAWING) { |
|
| 597 | - return 'image/jpeg'; |
|
| 598 | - } else if ($mimetype === self::PRESENTATION) { |
|
| 599 | - // Download as .odp is not available |
|
| 600 | - return 'application/pdf'; |
|
| 601 | - } else { |
|
| 602 | - // use extension-based detection, could be an encrypted file |
|
| 603 | - return parent::getMimeType($path); |
|
| 604 | - } |
|
| 605 | - } else { |
|
| 606 | - return false; |
|
| 607 | - } |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - public function free_space($path) { |
|
| 611 | - $about = $this->service->about->get(); |
|
| 612 | - return $about->getQuotaBytesTotal() - $about->getQuotaBytesUsed(); |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - public function touch($path, $mtime = null) { |
|
| 616 | - $file = $this->getDriveFile($path); |
|
| 617 | - $result = false; |
|
| 618 | - if ($file) { |
|
| 619 | - if (isset($mtime)) { |
|
| 620 | - // This is just RFC3339, but frustratingly, GDrive's API *requires* |
|
| 621 | - // the fractions portion be present, while no handy PHP constant |
|
| 622 | - // for RFC3339 or ISO8601 includes it. So we do it ourselves. |
|
| 623 | - $file->setModifiedDate(date('Y-m-d\TH:i:s.uP', $mtime)); |
|
| 624 | - $result = $this->service->files->patch($file->getId(), $file, array( |
|
| 625 | - 'setModifiedDate' => true, |
|
| 626 | - )); |
|
| 627 | - } else { |
|
| 628 | - $result = $this->service->files->touch($file->getId()); |
|
| 629 | - } |
|
| 630 | - } else { |
|
| 631 | - $parentFolder = $this->getDriveFile(dirname($path)); |
|
| 632 | - if ($parentFolder) { |
|
| 633 | - $file = new \Google_Service_Drive_DriveFile(); |
|
| 634 | - $file->setTitle(basename($path)); |
|
| 635 | - $parent = new \Google_Service_Drive_ParentReference(); |
|
| 636 | - $parent->setId($parentFolder->getId()); |
|
| 637 | - $file->setParents(array($parent)); |
|
| 638 | - $result = $this->service->files->insert($file); |
|
| 639 | - } |
|
| 640 | - } |
|
| 641 | - if ($result) { |
|
| 642 | - $this->setDriveFile($path, $result); |
|
| 643 | - } |
|
| 644 | - return (bool)$result; |
|
| 645 | - } |
|
| 646 | - |
|
| 647 | - public function test() { |
|
| 648 | - if ($this->free_space('')) { |
|
| 649 | - return true; |
|
| 650 | - } |
|
| 651 | - return false; |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - public function hasUpdated($path, $time) { |
|
| 655 | - $appConfig = \OC::$server->getAppConfig(); |
|
| 656 | - if ($this->is_file($path)) { |
|
| 657 | - return parent::hasUpdated($path, $time); |
|
| 658 | - } else { |
|
| 659 | - // Google Drive doesn't change modified times of folders when files inside are updated |
|
| 660 | - // Instead we use the Changes API to see if folders have been updated, and it's a pain |
|
| 661 | - $folder = $this->getDriveFile($path); |
|
| 662 | - if ($folder) { |
|
| 663 | - $result = false; |
|
| 664 | - $folderId = $folder->getId(); |
|
| 665 | - $startChangeId = $appConfig->getValue('files_external', $this->getId().'cId'); |
|
| 666 | - $params = array( |
|
| 667 | - 'includeDeleted' => true, |
|
| 668 | - 'includeSubscribed' => true, |
|
| 669 | - ); |
|
| 670 | - if (isset($startChangeId)) { |
|
| 671 | - $startChangeId = (int)$startChangeId; |
|
| 672 | - $largestChangeId = $startChangeId; |
|
| 673 | - $params['startChangeId'] = $startChangeId + 1; |
|
| 674 | - } else { |
|
| 675 | - $largestChangeId = 0; |
|
| 676 | - } |
|
| 677 | - $pageToken = true; |
|
| 678 | - while ($pageToken) { |
|
| 679 | - if ($pageToken !== true) { |
|
| 680 | - $params['pageToken'] = $pageToken; |
|
| 681 | - } |
|
| 682 | - $changes = $this->service->changes->listChanges($params); |
|
| 683 | - if ($largestChangeId === 0 || $largestChangeId === $startChangeId) { |
|
| 684 | - $largestChangeId = $changes->getLargestChangeId(); |
|
| 685 | - } |
|
| 686 | - if (isset($startChangeId)) { |
|
| 687 | - // Check if a file in this folder has been updated |
|
| 688 | - // There is no way to filter by folder at the API level... |
|
| 689 | - foreach ($changes->getItems() as $change) { |
|
| 690 | - $file = $change->getFile(); |
|
| 691 | - if ($file) { |
|
| 692 | - foreach ($file->getParents() as $parent) { |
|
| 693 | - if ($parent->getId() === $folderId) { |
|
| 694 | - $result = true; |
|
| 695 | - // Check if there are changes in different folders |
|
| 696 | - } else if ($change->getId() <= $largestChangeId) { |
|
| 697 | - // Decrement id so this change is fetched when called again |
|
| 698 | - $largestChangeId = $change->getId(); |
|
| 699 | - $largestChangeId--; |
|
| 700 | - } |
|
| 701 | - } |
|
| 702 | - } |
|
| 703 | - } |
|
| 704 | - $pageToken = $changes->getNextPageToken(); |
|
| 705 | - } else { |
|
| 706 | - // Assuming the initial scan just occurred and changes are negligible |
|
| 707 | - break; |
|
| 708 | - } |
|
| 709 | - } |
|
| 710 | - $appConfig->setValue('files_external', $this->getId().'cId', $largestChangeId); |
|
| 711 | - return $result; |
|
| 712 | - } |
|
| 713 | - } |
|
| 714 | - return false; |
|
| 715 | - } |
|
| 716 | - |
|
| 717 | - /** |
|
| 718 | - * check if curl is installed |
|
| 719 | - */ |
|
| 720 | - public static function checkDependencies() { |
|
| 721 | - return true; |
|
| 722 | - } |
|
| 49 | + private $client; |
|
| 50 | + private $id; |
|
| 51 | + private $service; |
|
| 52 | + private $driveFiles; |
|
| 53 | + |
|
| 54 | + // Google Doc mimetypes |
|
| 55 | + const FOLDER = 'application/vnd.google-apps.folder'; |
|
| 56 | + const DOCUMENT = 'application/vnd.google-apps.document'; |
|
| 57 | + const SPREADSHEET = 'application/vnd.google-apps.spreadsheet'; |
|
| 58 | + const DRAWING = 'application/vnd.google-apps.drawing'; |
|
| 59 | + const PRESENTATION = 'application/vnd.google-apps.presentation'; |
|
| 60 | + const MAP = 'application/vnd.google-apps.map'; |
|
| 61 | + |
|
| 62 | + public function __construct($params) { |
|
| 63 | + if (isset($params['configured']) && $params['configured'] === 'true' |
|
| 64 | + && isset($params['client_id']) && isset($params['client_secret']) |
|
| 65 | + && isset($params['token']) |
|
| 66 | + ) { |
|
| 67 | + $this->client = new \Google_Client(); |
|
| 68 | + $this->client->setClientId($params['client_id']); |
|
| 69 | + $this->client->setClientSecret($params['client_secret']); |
|
| 70 | + $this->client->setScopes(array('https://www.googleapis.com/auth/drive')); |
|
| 71 | + $this->client->setAccessToken($params['token']); |
|
| 72 | + // if curl isn't available we're likely to run into |
|
| 73 | + // https://github.com/google/google-api-php-client/issues/59 |
|
| 74 | + // - disable gzip to avoid it. |
|
| 75 | + if (!function_exists('curl_version') || !function_exists('curl_exec')) { |
|
| 76 | + $this->client->setClassConfig("Google_Http_Request", "disable_gzip", true); |
|
| 77 | + } |
|
| 78 | + // note: API connection is lazy |
|
| 79 | + $this->service = new \Google_Service_Drive($this->client); |
|
| 80 | + $token = json_decode($params['token'], true); |
|
| 81 | + $this->id = 'google::'.substr($params['client_id'], 0, 30).$token['created']; |
|
| 82 | + } else { |
|
| 83 | + throw new \Exception('Creating Google storage failed'); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + public function getId() { |
|
| 88 | + return $this->id; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Get the Google_Service_Drive_DriveFile object for the specified path. |
|
| 93 | + * Returns false on failure. |
|
| 94 | + * @param string $path |
|
| 95 | + * @return \Google_Service_Drive_DriveFile|false |
|
| 96 | + */ |
|
| 97 | + private function getDriveFile($path) { |
|
| 98 | + // Remove leading and trailing slashes |
|
| 99 | + $path = trim($path, '/'); |
|
| 100 | + if ($path === '.') { |
|
| 101 | + $path = ''; |
|
| 102 | + } |
|
| 103 | + if (isset($this->driveFiles[$path])) { |
|
| 104 | + return $this->driveFiles[$path]; |
|
| 105 | + } else if ($path === '') { |
|
| 106 | + $root = $this->service->files->get('root'); |
|
| 107 | + $this->driveFiles[$path] = $root; |
|
| 108 | + return $root; |
|
| 109 | + } else { |
|
| 110 | + // Google Drive SDK does not have methods for retrieving files by path |
|
| 111 | + // Instead we must find the id of the parent folder of the file |
|
| 112 | + $parentId = $this->getDriveFile('')->getId(); |
|
| 113 | + $folderNames = explode('/', $path); |
|
| 114 | + $path = ''; |
|
| 115 | + // Loop through each folder of this path to get to the file |
|
| 116 | + foreach ($folderNames as $name) { |
|
| 117 | + // Reconstruct path from beginning |
|
| 118 | + if ($path === '') { |
|
| 119 | + $path .= $name; |
|
| 120 | + } else { |
|
| 121 | + $path .= '/'.$name; |
|
| 122 | + } |
|
| 123 | + if (isset($this->driveFiles[$path])) { |
|
| 124 | + $parentId = $this->driveFiles[$path]->getId(); |
|
| 125 | + } else { |
|
| 126 | + $q = "title='" . str_replace("'","\\'", $name) . "' and '" . str_replace("'","\\'", $parentId) . "' in parents and trashed = false"; |
|
| 127 | + $result = $this->service->files->listFiles(array('q' => $q))->getItems(); |
|
| 128 | + if (!empty($result)) { |
|
| 129 | + // Google Drive allows files with the same name, ownCloud doesn't |
|
| 130 | + if (count($result) > 1) { |
|
| 131 | + $this->onDuplicateFileDetected($path); |
|
| 132 | + return false; |
|
| 133 | + } else { |
|
| 134 | + $file = current($result); |
|
| 135 | + $this->driveFiles[$path] = $file; |
|
| 136 | + $parentId = $file->getId(); |
|
| 137 | + } |
|
| 138 | + } else { |
|
| 139 | + // Google Docs have no extension in their title, so try without extension |
|
| 140 | + $pos = strrpos($path, '.'); |
|
| 141 | + if ($pos !== false) { |
|
| 142 | + $pathWithoutExt = substr($path, 0, $pos); |
|
| 143 | + $file = $this->getDriveFile($pathWithoutExt); |
|
| 144 | + if ($file && $this->isGoogleDocFile($file)) { |
|
| 145 | + // Switch cached Google_Service_Drive_DriveFile to the correct index |
|
| 146 | + unset($this->driveFiles[$pathWithoutExt]); |
|
| 147 | + $this->driveFiles[$path] = $file; |
|
| 148 | + $parentId = $file->getId(); |
|
| 149 | + } else { |
|
| 150 | + return false; |
|
| 151 | + } |
|
| 152 | + } else { |
|
| 153 | + return false; |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + return $this->driveFiles[$path]; |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Set the Google_Service_Drive_DriveFile object in the cache |
|
| 164 | + * @param string $path |
|
| 165 | + * @param \Google_Service_Drive_DriveFile|false $file |
|
| 166 | + */ |
|
| 167 | + private function setDriveFile($path, $file) { |
|
| 168 | + $path = trim($path, '/'); |
|
| 169 | + $this->driveFiles[$path] = $file; |
|
| 170 | + if ($file === false) { |
|
| 171 | + // Remove all children |
|
| 172 | + $len = strlen($path); |
|
| 173 | + foreach ($this->driveFiles as $key => $file) { |
|
| 174 | + if (substr($key, 0, $len) === $path) { |
|
| 175 | + unset($this->driveFiles[$key]); |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + } |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Write a log message to inform about duplicate file names |
|
| 183 | + * @param string $path |
|
| 184 | + */ |
|
| 185 | + private function onDuplicateFileDetected($path) { |
|
| 186 | + $about = $this->service->about->get(); |
|
| 187 | + $user = $about->getName(); |
|
| 188 | + \OCP\Util::writeLog('files_external', |
|
| 189 | + 'Ignoring duplicate file name: '.$path.' on Google Drive for Google user: '.$user, |
|
| 190 | + \OCP\Util::INFO |
|
| 191 | + ); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Generate file extension for a Google Doc, choosing Open Document formats for download |
|
| 196 | + * @param string $mimetype |
|
| 197 | + * @return string |
|
| 198 | + */ |
|
| 199 | + private function getGoogleDocExtension($mimetype) { |
|
| 200 | + if ($mimetype === self::DOCUMENT) { |
|
| 201 | + return 'odt'; |
|
| 202 | + } else if ($mimetype === self::SPREADSHEET) { |
|
| 203 | + return 'ods'; |
|
| 204 | + } else if ($mimetype === self::DRAWING) { |
|
| 205 | + return 'jpg'; |
|
| 206 | + } else if ($mimetype === self::PRESENTATION) { |
|
| 207 | + // Download as .odp is not available |
|
| 208 | + return 'pdf'; |
|
| 209 | + } else { |
|
| 210 | + return ''; |
|
| 211 | + } |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Returns whether the given drive file is a Google Doc file |
|
| 216 | + * |
|
| 217 | + * @param \Google_Service_Drive_DriveFile |
|
| 218 | + * |
|
| 219 | + * @return true if the file is a Google Doc file, false otherwise |
|
| 220 | + */ |
|
| 221 | + private function isGoogleDocFile($file) { |
|
| 222 | + return $this->getGoogleDocExtension($file->getMimeType()) !== ''; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + public function mkdir($path) { |
|
| 226 | + if (!$this->is_dir($path)) { |
|
| 227 | + $parentFolder = $this->getDriveFile(dirname($path)); |
|
| 228 | + if ($parentFolder) { |
|
| 229 | + $folder = new \Google_Service_Drive_DriveFile(); |
|
| 230 | + $folder->setTitle(basename($path)); |
|
| 231 | + $folder->setMimeType(self::FOLDER); |
|
| 232 | + $parent = new \Google_Service_Drive_ParentReference(); |
|
| 233 | + $parent->setId($parentFolder->getId()); |
|
| 234 | + $folder->setParents(array($parent)); |
|
| 235 | + $result = $this->service->files->insert($folder); |
|
| 236 | + if ($result) { |
|
| 237 | + $this->setDriveFile($path, $result); |
|
| 238 | + } |
|
| 239 | + return (bool)$result; |
|
| 240 | + } |
|
| 241 | + } |
|
| 242 | + return false; |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + public function rmdir($path) { |
|
| 246 | + if (!$this->isDeletable($path)) { |
|
| 247 | + return false; |
|
| 248 | + } |
|
| 249 | + if (trim($path, '/') === '') { |
|
| 250 | + $dir = $this->opendir($path); |
|
| 251 | + if(is_resource($dir)) { |
|
| 252 | + while (($file = readdir($dir)) !== false) { |
|
| 253 | + if (!\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
| 254 | + if (!$this->unlink($path.'/'.$file)) { |
|
| 255 | + return false; |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + closedir($dir); |
|
| 260 | + } |
|
| 261 | + $this->driveFiles = array(); |
|
| 262 | + return true; |
|
| 263 | + } else { |
|
| 264 | + return $this->unlink($path); |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + public function opendir($path) { |
|
| 269 | + $folder = $this->getDriveFile($path); |
|
| 270 | + if ($folder) { |
|
| 271 | + $files = array(); |
|
| 272 | + $duplicates = array(); |
|
| 273 | + $pageToken = true; |
|
| 274 | + while ($pageToken) { |
|
| 275 | + $params = array(); |
|
| 276 | + if ($pageToken !== true) { |
|
| 277 | + $params['pageToken'] = $pageToken; |
|
| 278 | + } |
|
| 279 | + $params['q'] = "'" . str_replace("'","\\'", $folder->getId()) . "' in parents and trashed = false"; |
|
| 280 | + $children = $this->service->files->listFiles($params); |
|
| 281 | + foreach ($children->getItems() as $child) { |
|
| 282 | + $name = $child->getTitle(); |
|
| 283 | + // Check if this is a Google Doc i.e. no extension in name |
|
| 284 | + $extension = $child->getFileExtension(); |
|
| 285 | + if (empty($extension)) { |
|
| 286 | + if ($child->getMimeType() === self::MAP) { |
|
| 287 | + continue; // No method known to transfer map files, ignore it |
|
| 288 | + } else if ($child->getMimeType() !== self::FOLDER) { |
|
| 289 | + $name .= '.'.$this->getGoogleDocExtension($child->getMimeType()); |
|
| 290 | + } |
|
| 291 | + } |
|
| 292 | + if ($path === '') { |
|
| 293 | + $filepath = $name; |
|
| 294 | + } else { |
|
| 295 | + $filepath = $path.'/'.$name; |
|
| 296 | + } |
|
| 297 | + // Google Drive allows files with the same name, ownCloud doesn't |
|
| 298 | + // Prevent opendir() from returning any duplicate files |
|
| 299 | + $key = array_search($name, $files); |
|
| 300 | + if ($key !== false || isset($duplicates[$filepath])) { |
|
| 301 | + if (!isset($duplicates[$filepath])) { |
|
| 302 | + $duplicates[$filepath] = true; |
|
| 303 | + $this->setDriveFile($filepath, false); |
|
| 304 | + unset($files[$key]); |
|
| 305 | + $this->onDuplicateFileDetected($filepath); |
|
| 306 | + } |
|
| 307 | + } else { |
|
| 308 | + // Cache the Google_Service_Drive_DriveFile for future use |
|
| 309 | + $this->setDriveFile($filepath, $child); |
|
| 310 | + $files[] = $name; |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | + $pageToken = $children->getNextPageToken(); |
|
| 314 | + } |
|
| 315 | + return IteratorDirectory::wrap($files); |
|
| 316 | + } else { |
|
| 317 | + return false; |
|
| 318 | + } |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + public function stat($path) { |
|
| 322 | + $file = $this->getDriveFile($path); |
|
| 323 | + if ($file) { |
|
| 324 | + $stat = array(); |
|
| 325 | + if ($this->filetype($path) === 'dir') { |
|
| 326 | + $stat['size'] = 0; |
|
| 327 | + } else { |
|
| 328 | + // Check if this is a Google Doc |
|
| 329 | + if ($this->isGoogleDocFile($file)) { |
|
| 330 | + // Return unknown file size |
|
| 331 | + $stat['size'] = \OCP\Files\FileInfo::SPACE_UNKNOWN; |
|
| 332 | + } else { |
|
| 333 | + $stat['size'] = $file->getFileSize(); |
|
| 334 | + } |
|
| 335 | + } |
|
| 336 | + $stat['atime'] = strtotime($file->getLastViewedByMeDate()); |
|
| 337 | + $stat['mtime'] = strtotime($file->getModifiedDate()); |
|
| 338 | + $stat['ctime'] = strtotime($file->getCreatedDate()); |
|
| 339 | + return $stat; |
|
| 340 | + } else { |
|
| 341 | + return false; |
|
| 342 | + } |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + public function filetype($path) { |
|
| 346 | + if ($path === '') { |
|
| 347 | + return 'dir'; |
|
| 348 | + } else { |
|
| 349 | + $file = $this->getDriveFile($path); |
|
| 350 | + if ($file) { |
|
| 351 | + if ($file->getMimeType() === self::FOLDER) { |
|
| 352 | + return 'dir'; |
|
| 353 | + } else { |
|
| 354 | + return 'file'; |
|
| 355 | + } |
|
| 356 | + } else { |
|
| 357 | + return false; |
|
| 358 | + } |
|
| 359 | + } |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + public function isUpdatable($path) { |
|
| 363 | + $file = $this->getDriveFile($path); |
|
| 364 | + if ($file) { |
|
| 365 | + return $file->getEditable(); |
|
| 366 | + } else { |
|
| 367 | + return false; |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + public function file_exists($path) { |
|
| 372 | + return (bool)$this->getDriveFile($path); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + public function unlink($path) { |
|
| 376 | + $file = $this->getDriveFile($path); |
|
| 377 | + if ($file) { |
|
| 378 | + $result = $this->service->files->trash($file->getId()); |
|
| 379 | + if ($result) { |
|
| 380 | + $this->setDriveFile($path, false); |
|
| 381 | + } |
|
| 382 | + return (bool)$result; |
|
| 383 | + } else { |
|
| 384 | + return false; |
|
| 385 | + } |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + public function rename($path1, $path2) { |
|
| 389 | + $file = $this->getDriveFile($path1); |
|
| 390 | + if ($file) { |
|
| 391 | + $newFile = $this->getDriveFile($path2); |
|
| 392 | + if (dirname($path1) === dirname($path2)) { |
|
| 393 | + if ($newFile) { |
|
| 394 | + // rename to the name of the target file, could be an office file without extension |
|
| 395 | + $file->setTitle($newFile->getTitle()); |
|
| 396 | + } else { |
|
| 397 | + $file->setTitle(basename(($path2))); |
|
| 398 | + } |
|
| 399 | + } else { |
|
| 400 | + // Change file parent |
|
| 401 | + $parentFolder2 = $this->getDriveFile(dirname($path2)); |
|
| 402 | + if ($parentFolder2) { |
|
| 403 | + $parent = new \Google_Service_Drive_ParentReference(); |
|
| 404 | + $parent->setId($parentFolder2->getId()); |
|
| 405 | + $file->setParents(array($parent)); |
|
| 406 | + } else { |
|
| 407 | + return false; |
|
| 408 | + } |
|
| 409 | + } |
|
| 410 | + // We need to get the object for the existing file with the same |
|
| 411 | + // name (if there is one) before we do the patch. If oldfile |
|
| 412 | + // exists and is a directory we have to delete it before we |
|
| 413 | + // do the rename too. |
|
| 414 | + $oldfile = $this->getDriveFile($path2); |
|
| 415 | + if ($oldfile && $this->is_dir($path2)) { |
|
| 416 | + $this->rmdir($path2); |
|
| 417 | + $oldfile = false; |
|
| 418 | + } |
|
| 419 | + $result = $this->service->files->patch($file->getId(), $file); |
|
| 420 | + if ($result) { |
|
| 421 | + $this->setDriveFile($path1, false); |
|
| 422 | + $this->setDriveFile($path2, $result); |
|
| 423 | + if ($oldfile && $newFile) { |
|
| 424 | + // only delete if they have a different id (same id can happen for part files) |
|
| 425 | + if ($newFile->getId() !== $oldfile->getId()) { |
|
| 426 | + $this->service->files->delete($oldfile->getId()); |
|
| 427 | + } |
|
| 428 | + } |
|
| 429 | + } |
|
| 430 | + return (bool)$result; |
|
| 431 | + } else { |
|
| 432 | + return false; |
|
| 433 | + } |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + public function fopen($path, $mode) { |
|
| 437 | + $pos = strrpos($path, '.'); |
|
| 438 | + if ($pos !== false) { |
|
| 439 | + $ext = substr($path, $pos); |
|
| 440 | + } else { |
|
| 441 | + $ext = ''; |
|
| 442 | + } |
|
| 443 | + switch ($mode) { |
|
| 444 | + case 'r': |
|
| 445 | + case 'rb': |
|
| 446 | + $file = $this->getDriveFile($path); |
|
| 447 | + if ($file) { |
|
| 448 | + $exportLinks = $file->getExportLinks(); |
|
| 449 | + $mimetype = $this->getMimeType($path); |
|
| 450 | + $downloadUrl = null; |
|
| 451 | + if ($exportLinks && isset($exportLinks[$mimetype])) { |
|
| 452 | + $downloadUrl = $exportLinks[$mimetype]; |
|
| 453 | + } else { |
|
| 454 | + $downloadUrl = $file->getDownloadUrl(); |
|
| 455 | + } |
|
| 456 | + if (isset($downloadUrl)) { |
|
| 457 | + $request = new \Google_Http_Request($downloadUrl, 'GET', null, null); |
|
| 458 | + $httpRequest = $this->client->getAuth()->sign($request); |
|
| 459 | + // the library's service doesn't support streaming, so we use Guzzle instead |
|
| 460 | + $client = \OC::$server->getHTTPClientService()->newClient(); |
|
| 461 | + try { |
|
| 462 | + $response = $client->get($downloadUrl, [ |
|
| 463 | + 'headers' => $httpRequest->getRequestHeaders(), |
|
| 464 | + 'stream' => true, |
|
| 465 | + 'verify' => realpath(__DIR__ . '/../../../3rdparty/google-api-php-client/src/Google/IO/cacerts.pem'), |
|
| 466 | + ]); |
|
| 467 | + } catch (RequestException $e) { |
|
| 468 | + if(!is_null($e->getResponse())) { |
|
| 469 | + if ($e->getResponse()->getStatusCode() === 404) { |
|
| 470 | + return false; |
|
| 471 | + } else { |
|
| 472 | + throw $e; |
|
| 473 | + } |
|
| 474 | + } else { |
|
| 475 | + throw $e; |
|
| 476 | + } |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + $handle = $response->getBody(); |
|
| 480 | + return RetryWrapper::wrap($handle); |
|
| 481 | + } |
|
| 482 | + } |
|
| 483 | + return false; |
|
| 484 | + case 'w': |
|
| 485 | + case 'wb': |
|
| 486 | + case 'a': |
|
| 487 | + case 'ab': |
|
| 488 | + case 'r+': |
|
| 489 | + case 'w+': |
|
| 490 | + case 'wb+': |
|
| 491 | + case 'a+': |
|
| 492 | + case 'x': |
|
| 493 | + case 'x+': |
|
| 494 | + case 'c': |
|
| 495 | + case 'c+': |
|
| 496 | + $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 497 | + if ($this->file_exists($path)) { |
|
| 498 | + $source = $this->fopen($path, 'rb'); |
|
| 499 | + file_put_contents($tmpFile, $source); |
|
| 500 | + } |
|
| 501 | + $handle = fopen($tmpFile, $mode); |
|
| 502 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 503 | + $this->writeBack($tmpFile, $path); |
|
| 504 | + }); |
|
| 505 | + } |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + public function writeBack($tmpFile, $path) { |
|
| 509 | + $parentFolder = $this->getDriveFile(dirname($path)); |
|
| 510 | + if ($parentFolder) { |
|
| 511 | + $mimetype = \OC::$server->getMimeTypeDetector()->detect($tmpFile); |
|
| 512 | + $params = array( |
|
| 513 | + 'mimeType' => $mimetype, |
|
| 514 | + 'uploadType' => 'media' |
|
| 515 | + ); |
|
| 516 | + $result = false; |
|
| 517 | + |
|
| 518 | + $chunkSizeBytes = 10 * 1024 * 1024; |
|
| 519 | + |
|
| 520 | + $useChunking = false; |
|
| 521 | + $size = filesize($tmpFile); |
|
| 522 | + if ($size > $chunkSizeBytes) { |
|
| 523 | + $useChunking = true; |
|
| 524 | + } else { |
|
| 525 | + $params['data'] = file_get_contents($tmpFile); |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + if ($this->file_exists($path)) { |
|
| 529 | + $file = $this->getDriveFile($path); |
|
| 530 | + $this->client->setDefer($useChunking); |
|
| 531 | + $request = $this->service->files->update($file->getId(), $file, $params); |
|
| 532 | + } else { |
|
| 533 | + $file = new \Google_Service_Drive_DriveFile(); |
|
| 534 | + $file->setTitle(basename($path)); |
|
| 535 | + $file->setMimeType($mimetype); |
|
| 536 | + $parent = new \Google_Service_Drive_ParentReference(); |
|
| 537 | + $parent->setId($parentFolder->getId()); |
|
| 538 | + $file->setParents(array($parent)); |
|
| 539 | + $this->client->setDefer($useChunking); |
|
| 540 | + $request = $this->service->files->insert($file, $params); |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + if ($useChunking) { |
|
| 544 | + // Create a media file upload to represent our upload process. |
|
| 545 | + $media = new \Google_Http_MediaFileUpload( |
|
| 546 | + $this->client, |
|
| 547 | + $request, |
|
| 548 | + 'text/plain', |
|
| 549 | + null, |
|
| 550 | + true, |
|
| 551 | + $chunkSizeBytes |
|
| 552 | + ); |
|
| 553 | + $media->setFileSize($size); |
|
| 554 | + |
|
| 555 | + // Upload the various chunks. $status will be false until the process is |
|
| 556 | + // complete. |
|
| 557 | + $status = false; |
|
| 558 | + $handle = fopen($tmpFile, 'rb'); |
|
| 559 | + while (!$status && !feof($handle)) { |
|
| 560 | + $chunk = fread($handle, $chunkSizeBytes); |
|
| 561 | + $status = $media->nextChunk($chunk); |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + // The final value of $status will be the data from the API for the object |
|
| 565 | + // that has been uploaded. |
|
| 566 | + $result = false; |
|
| 567 | + if ($status !== false) { |
|
| 568 | + $result = $status; |
|
| 569 | + } |
|
| 570 | + |
|
| 571 | + fclose($handle); |
|
| 572 | + } else { |
|
| 573 | + $result = $request; |
|
| 574 | + } |
|
| 575 | + |
|
| 576 | + // Reset to the client to execute requests immediately in the future. |
|
| 577 | + $this->client->setDefer(false); |
|
| 578 | + |
|
| 579 | + if ($result) { |
|
| 580 | + $this->setDriveFile($path, $result); |
|
| 581 | + } |
|
| 582 | + } |
|
| 583 | + } |
|
| 584 | + |
|
| 585 | + public function getMimeType($path) { |
|
| 586 | + $file = $this->getDriveFile($path); |
|
| 587 | + if ($file) { |
|
| 588 | + $mimetype = $file->getMimeType(); |
|
| 589 | + // Convert Google Doc mimetypes, choosing Open Document formats for download |
|
| 590 | + if ($mimetype === self::FOLDER) { |
|
| 591 | + return 'httpd/unix-directory'; |
|
| 592 | + } else if ($mimetype === self::DOCUMENT) { |
|
| 593 | + return 'application/vnd.oasis.opendocument.text'; |
|
| 594 | + } else if ($mimetype === self::SPREADSHEET) { |
|
| 595 | + return 'application/x-vnd.oasis.opendocument.spreadsheet'; |
|
| 596 | + } else if ($mimetype === self::DRAWING) { |
|
| 597 | + return 'image/jpeg'; |
|
| 598 | + } else if ($mimetype === self::PRESENTATION) { |
|
| 599 | + // Download as .odp is not available |
|
| 600 | + return 'application/pdf'; |
|
| 601 | + } else { |
|
| 602 | + // use extension-based detection, could be an encrypted file |
|
| 603 | + return parent::getMimeType($path); |
|
| 604 | + } |
|
| 605 | + } else { |
|
| 606 | + return false; |
|
| 607 | + } |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + public function free_space($path) { |
|
| 611 | + $about = $this->service->about->get(); |
|
| 612 | + return $about->getQuotaBytesTotal() - $about->getQuotaBytesUsed(); |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + public function touch($path, $mtime = null) { |
|
| 616 | + $file = $this->getDriveFile($path); |
|
| 617 | + $result = false; |
|
| 618 | + if ($file) { |
|
| 619 | + if (isset($mtime)) { |
|
| 620 | + // This is just RFC3339, but frustratingly, GDrive's API *requires* |
|
| 621 | + // the fractions portion be present, while no handy PHP constant |
|
| 622 | + // for RFC3339 or ISO8601 includes it. So we do it ourselves. |
|
| 623 | + $file->setModifiedDate(date('Y-m-d\TH:i:s.uP', $mtime)); |
|
| 624 | + $result = $this->service->files->patch($file->getId(), $file, array( |
|
| 625 | + 'setModifiedDate' => true, |
|
| 626 | + )); |
|
| 627 | + } else { |
|
| 628 | + $result = $this->service->files->touch($file->getId()); |
|
| 629 | + } |
|
| 630 | + } else { |
|
| 631 | + $parentFolder = $this->getDriveFile(dirname($path)); |
|
| 632 | + if ($parentFolder) { |
|
| 633 | + $file = new \Google_Service_Drive_DriveFile(); |
|
| 634 | + $file->setTitle(basename($path)); |
|
| 635 | + $parent = new \Google_Service_Drive_ParentReference(); |
|
| 636 | + $parent->setId($parentFolder->getId()); |
|
| 637 | + $file->setParents(array($parent)); |
|
| 638 | + $result = $this->service->files->insert($file); |
|
| 639 | + } |
|
| 640 | + } |
|
| 641 | + if ($result) { |
|
| 642 | + $this->setDriveFile($path, $result); |
|
| 643 | + } |
|
| 644 | + return (bool)$result; |
|
| 645 | + } |
|
| 646 | + |
|
| 647 | + public function test() { |
|
| 648 | + if ($this->free_space('')) { |
|
| 649 | + return true; |
|
| 650 | + } |
|
| 651 | + return false; |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + public function hasUpdated($path, $time) { |
|
| 655 | + $appConfig = \OC::$server->getAppConfig(); |
|
| 656 | + if ($this->is_file($path)) { |
|
| 657 | + return parent::hasUpdated($path, $time); |
|
| 658 | + } else { |
|
| 659 | + // Google Drive doesn't change modified times of folders when files inside are updated |
|
| 660 | + // Instead we use the Changes API to see if folders have been updated, and it's a pain |
|
| 661 | + $folder = $this->getDriveFile($path); |
|
| 662 | + if ($folder) { |
|
| 663 | + $result = false; |
|
| 664 | + $folderId = $folder->getId(); |
|
| 665 | + $startChangeId = $appConfig->getValue('files_external', $this->getId().'cId'); |
|
| 666 | + $params = array( |
|
| 667 | + 'includeDeleted' => true, |
|
| 668 | + 'includeSubscribed' => true, |
|
| 669 | + ); |
|
| 670 | + if (isset($startChangeId)) { |
|
| 671 | + $startChangeId = (int)$startChangeId; |
|
| 672 | + $largestChangeId = $startChangeId; |
|
| 673 | + $params['startChangeId'] = $startChangeId + 1; |
|
| 674 | + } else { |
|
| 675 | + $largestChangeId = 0; |
|
| 676 | + } |
|
| 677 | + $pageToken = true; |
|
| 678 | + while ($pageToken) { |
|
| 679 | + if ($pageToken !== true) { |
|
| 680 | + $params['pageToken'] = $pageToken; |
|
| 681 | + } |
|
| 682 | + $changes = $this->service->changes->listChanges($params); |
|
| 683 | + if ($largestChangeId === 0 || $largestChangeId === $startChangeId) { |
|
| 684 | + $largestChangeId = $changes->getLargestChangeId(); |
|
| 685 | + } |
|
| 686 | + if (isset($startChangeId)) { |
|
| 687 | + // Check if a file in this folder has been updated |
|
| 688 | + // There is no way to filter by folder at the API level... |
|
| 689 | + foreach ($changes->getItems() as $change) { |
|
| 690 | + $file = $change->getFile(); |
|
| 691 | + if ($file) { |
|
| 692 | + foreach ($file->getParents() as $parent) { |
|
| 693 | + if ($parent->getId() === $folderId) { |
|
| 694 | + $result = true; |
|
| 695 | + // Check if there are changes in different folders |
|
| 696 | + } else if ($change->getId() <= $largestChangeId) { |
|
| 697 | + // Decrement id so this change is fetched when called again |
|
| 698 | + $largestChangeId = $change->getId(); |
|
| 699 | + $largestChangeId--; |
|
| 700 | + } |
|
| 701 | + } |
|
| 702 | + } |
|
| 703 | + } |
|
| 704 | + $pageToken = $changes->getNextPageToken(); |
|
| 705 | + } else { |
|
| 706 | + // Assuming the initial scan just occurred and changes are negligible |
|
| 707 | + break; |
|
| 708 | + } |
|
| 709 | + } |
|
| 710 | + $appConfig->setValue('files_external', $this->getId().'cId', $largestChangeId); |
|
| 711 | + return $result; |
|
| 712 | + } |
|
| 713 | + } |
|
| 714 | + return false; |
|
| 715 | + } |
|
| 716 | + |
|
| 717 | + /** |
|
| 718 | + * check if curl is installed |
|
| 719 | + */ |
|
| 720 | + public static function checkDependencies() { |
|
| 721 | + return true; |
|
| 722 | + } |
|
| 723 | 723 | |
| 724 | 724 | } |