google.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Michael Gapczynski
  6. * @copyright 2012 Michael Gapczynski mtgap@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. namespace OC\Files\Storage;
  22. set_include_path(get_include_path().PATH_SEPARATOR.
  23. \OC_App::getAppPath('files_external').'/3rdparty/google-api-php-client/src');
  24. require_once 'Google_Client.php';
  25. require_once 'contrib/Google_DriveService.php';
  26. class Google extends \OC\Files\Storage\Common {
  27. private $id;
  28. private $service;
  29. private $driveFiles;
  30. private static $tempFiles = array();
  31. // Google Doc mimetypes
  32. const FOLDER = 'application/vnd.google-apps.folder';
  33. const DOCUMENT = 'application/vnd.google-apps.document';
  34. const SPREADSHEET = 'application/vnd.google-apps.spreadsheet';
  35. const DRAWING = 'application/vnd.google-apps.drawing';
  36. const PRESENTATION = 'application/vnd.google-apps.presentation';
  37. public function __construct($params) {
  38. if (isset($params['configured']) && $params['configured'] === 'true'
  39. && isset($params['client_id']) && isset($params['client_secret'])
  40. && isset($params['token'])
  41. ) {
  42. $client = new \Google_Client();
  43. $client->setClientId($params['client_id']);
  44. $client->setClientSecret($params['client_secret']);
  45. $client->setScopes(array('https://www.googleapis.com/auth/drive'));
  46. $client->setUseObjects(true);
  47. $client->setAccessToken($params['token']);
  48. $this->service = new \Google_DriveService($client);
  49. $token = json_decode($params['token'], true);
  50. $this->id = 'google::'.substr($params['client_id'], 0, 30).$token['created'];
  51. } else {
  52. throw new \Exception('Creating \OC\Files\Storage\Google storage failed');
  53. }
  54. }
  55. public function getId() {
  56. return $this->id;
  57. }
  58. /**
  59. * Get the Google_DriveFile object for the specified path
  60. * @param string $path
  61. * @return Google_DriveFile
  62. */
  63. private function getDriveFile($path) {
  64. // Remove leading and trailing slashes
  65. $path = trim($path, '/');
  66. if (isset($this->driveFiles[$path])) {
  67. return $this->driveFiles[$path];
  68. } else if ($path === '') {
  69. $root = $this->service->files->get('root');
  70. $this->driveFiles[$path] = $root;
  71. return $root;
  72. } else {
  73. // Google Drive SDK does not have methods for retrieving files by path
  74. // Instead we must find the id of the parent folder of the file
  75. $parentId = $this->getDriveFile('')->getId();
  76. $folderNames = explode('/', $path);
  77. $path = '';
  78. // Loop through each folder of this path to get to the file
  79. foreach ($folderNames as $name) {
  80. // Reconstruct path from beginning
  81. if ($path === '') {
  82. $path .= $name;
  83. } else {
  84. $path .= '/'.$name;
  85. }
  86. if (isset($this->driveFiles[$path])) {
  87. $parentId = $this->driveFiles[$path]->getId();
  88. } else {
  89. $q = "title='".$name."' and '".$parentId."' in parents and trashed = false";
  90. $result = $this->service->files->listFiles(array('q' => $q))->getItems();
  91. if (!empty($result)) {
  92. // Google Drive allows files with the same name, ownCloud doesn't
  93. if (count($result) > 1) {
  94. $this->onDuplicateFileDetected($path);
  95. return false;
  96. } else {
  97. $file = current($result);
  98. $this->driveFiles[$path] = $file;
  99. $parentId = $file->getId();
  100. }
  101. } else {
  102. // Google Docs have no extension in their title, so try without extension
  103. $pos = strrpos($path, '.');
  104. if ($pos !== false) {
  105. $pathWithoutExt = substr($path, 0, $pos);
  106. $file = $this->getDriveFile($pathWithoutExt);
  107. if ($file) {
  108. // Switch cached Google_DriveFile to the correct index
  109. unset($this->driveFiles[$pathWithoutExt]);
  110. $this->driveFiles[$path] = $file;
  111. $parentId = $file->getId();
  112. } else {
  113. return false;
  114. }
  115. } else {
  116. return false;
  117. }
  118. }
  119. }
  120. }
  121. return $this->driveFiles[$path];
  122. }
  123. }
  124. /**
  125. * Set the Google_DriveFile object in the cache
  126. * @param string $path
  127. * @param Google_DriveFile|false $file
  128. */
  129. private function setDriveFile($path, $file) {
  130. $path = trim($path, '/');
  131. $this->driveFiles[$path] = $file;
  132. if ($file === false) {
  133. // Set all child paths as false
  134. $len = strlen($path);
  135. foreach ($this->driveFiles as $key => $file) {
  136. if (substr($key, 0, $len) === $path) {
  137. $this->driveFiles[$key] = false;
  138. }
  139. }
  140. }
  141. }
  142. /**
  143. * Write a log message to inform about duplicate file names
  144. * @param string $path
  145. */
  146. private function onDuplicateFileDetected($path) {
  147. $about = $this->service->about->get();
  148. $user = $about->getName();
  149. \OCP\Util::writeLog('files_external',
  150. 'Ignoring duplicate file name: '.$path.' on Google Drive for Google user: '.$user,
  151. \OCP\Util::INFO
  152. );
  153. }
  154. /**
  155. * Generate file extension for a Google Doc, choosing Open Document formats for download
  156. * @param string $mimetype
  157. * @return string
  158. */
  159. private function getGoogleDocExtension($mimetype) {
  160. if ($mimetype === self::DOCUMENT) {
  161. return 'odt';
  162. } else if ($mimetype === self::SPREADSHEET) {
  163. return 'ods';
  164. } else if ($mimetype === self::DRAWING) {
  165. return 'jpg';
  166. } else if ($mimetype === self::PRESENTATION) {
  167. // Download as .odp is not available
  168. return 'pdf';
  169. } else {
  170. return '';
  171. }
  172. }
  173. public function mkdir($path) {
  174. if (!$this->is_dir($path)) {
  175. $parentFolder = $this->getDriveFile(dirname($path));
  176. if ($parentFolder) {
  177. $folder = new \Google_DriveFile();
  178. $folder->setTitle(basename($path));
  179. $folder->setMimeType(self::FOLDER);
  180. $parent = new \Google_ParentReference();
  181. $parent->setId($parentFolder->getId());
  182. $folder->setParents(array($parent));
  183. $result = $this->service->files->insert($folder);
  184. if ($result) {
  185. $this->setDriveFile($path, $result);
  186. }
  187. return (bool)$result;
  188. }
  189. }
  190. return false;
  191. }
  192. public function rmdir($path) {
  193. if (trim($path, '/') === '') {
  194. $dir = $this->opendir($path);
  195. while ($file = readdir($dir)) {
  196. if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
  197. if (!$this->unlink($path.'/'.$file)) {
  198. return false;
  199. }
  200. }
  201. }
  202. closedir($dir);
  203. $this->driveFiles = array();
  204. return true;
  205. } else {
  206. return $this->unlink($path);
  207. }
  208. }
  209. public function opendir($path) {
  210. // Remove leading and trailing slashes
  211. $path = trim($path, '/');
  212. $folder = $this->getDriveFile($path);
  213. if ($folder) {
  214. $files = array();
  215. $duplicates = array();
  216. $pageToken = true;
  217. while ($pageToken) {
  218. $params = array();
  219. if ($pageToken !== true) {
  220. $params['pageToken'] = $pageToken;
  221. }
  222. $params['q'] = "'".$folder->getId()."' in parents and trashed = false";
  223. $children = $this->service->files->listFiles($params);
  224. foreach ($children->getItems() as $child) {
  225. $name = $child->getTitle();
  226. // Check if this is a Google Doc i.e. no extension in name
  227. if ($child->getFileExtension() === ''
  228. && $child->getMimeType() !== self::FOLDER
  229. ) {
  230. $name .= '.'.$this->getGoogleDocExtension($child->getMimeType());
  231. }
  232. if ($path === '') {
  233. $filepath = $name;
  234. } else {
  235. $filepath = $path.'/'.$name;
  236. }
  237. // Google Drive allows files with the same name, ownCloud doesn't
  238. // Prevent opendir() from returning any duplicate files
  239. $key = array_search($name, $files);
  240. if ($key !== false || isset($duplicates[$filepath])) {
  241. if (!isset($duplicates[$filepath])) {
  242. $duplicates[$filepath] = true;
  243. $this->setDriveFile($filepath, false);
  244. unset($files[$key]);
  245. $this->onDuplicateFileDetected($filepath);
  246. }
  247. } else {
  248. // Cache the Google_DriveFile for future use
  249. $this->setDriveFile($filepath, $child);
  250. $files[] = $name;
  251. }
  252. }
  253. $pageToken = $children->getNextPageToken();
  254. }
  255. \OC\Files\Stream\Dir::register('google'.$path, $files);
  256. return opendir('fakedir://google'.$path);
  257. } else {
  258. return false;
  259. }
  260. }
  261. public function stat($path) {
  262. $file = $this->getDriveFile($path);
  263. if ($file) {
  264. $stat = array();
  265. if ($this->filetype($path) === 'dir') {
  266. $stat['size'] = 0;
  267. } else {
  268. // Check if this is a Google Doc
  269. if ($this->getMimeType($path) !== $file->getMimeType()) {
  270. // Return unknown file size
  271. $stat['size'] = \OC\Files\FREE_SPACE_UNKNOWN;
  272. } else {
  273. $stat['size'] = $file->getFileSize();
  274. }
  275. }
  276. $stat['atime'] = strtotime($file->getLastViewedByMeDate());
  277. $stat['mtime'] = strtotime($file->getModifiedDate());
  278. $stat['ctime'] = strtotime($file->getCreatedDate());
  279. return $stat;
  280. } else {
  281. return false;
  282. }
  283. }
  284. public function filetype($path) {
  285. if ($path === '') {
  286. return 'dir';
  287. } else {
  288. $file = $this->getDriveFile($path);
  289. if ($file) {
  290. if ($file->getMimeType() === self::FOLDER) {
  291. return 'dir';
  292. } else {
  293. return 'file';
  294. }
  295. } else {
  296. return false;
  297. }
  298. }
  299. }
  300. public function isReadable($path) {
  301. return $this->file_exists($path);
  302. }
  303. public function isUpdatable($path) {
  304. $file = $this->getDriveFile($path);
  305. if ($file) {
  306. return $file->getEditable();
  307. } else {
  308. return false;
  309. }
  310. }
  311. public function file_exists($path) {
  312. return (bool)$this->getDriveFile($path);
  313. }
  314. public function unlink($path) {
  315. $file = $this->getDriveFile($path);
  316. if ($file) {
  317. $result = $this->service->files->trash($file->getId());
  318. if ($result) {
  319. $this->setDriveFile($path, false);
  320. }
  321. return (bool)$result;
  322. } else {
  323. return false;
  324. }
  325. }
  326. public function rename($path1, $path2) {
  327. $file = $this->getDriveFile($path1);
  328. if ($file) {
  329. if (dirname($path1) === dirname($path2)) {
  330. $file->setTitle(basename(($path2)));
  331. } else {
  332. // Change file parent
  333. $parentFolder2 = $this->getDriveFile(dirname($path2));
  334. if ($parentFolder2) {
  335. $parent = new \Google_ParentReference();
  336. $parent->setId($parentFolder2->getId());
  337. $file->setParents(array($parent));
  338. } else {
  339. return false;
  340. }
  341. }
  342. $result = $this->service->files->patch($file->getId(), $file);
  343. if ($result) {
  344. $this->setDriveFile($path1, false);
  345. $this->setDriveFile($path2, $result);
  346. }
  347. return (bool)$result;
  348. } else {
  349. return false;
  350. }
  351. }
  352. public function fopen($path, $mode) {
  353. $pos = strrpos($path, '.');
  354. if ($pos !== false) {
  355. $ext = substr($path, $pos);
  356. } else {
  357. $ext = '';
  358. }
  359. switch ($mode) {
  360. case 'r':
  361. case 'rb':
  362. $file = $this->getDriveFile($path);
  363. if ($file) {
  364. $exportLinks = $file->getExportLinks();
  365. $mimetype = $this->getMimeType($path);
  366. $downloadUrl = null;
  367. if ($exportLinks && isset($exportLinks[$mimetype])) {
  368. $downloadUrl = $exportLinks[$mimetype];
  369. } else {
  370. $downloadUrl = $file->getDownloadUrl();
  371. }
  372. if (isset($downloadUrl)) {
  373. $request = new \Google_HttpRequest($downloadUrl, 'GET', null, null);
  374. $httpRequest = \Google_Client::$io->authenticatedRequest($request);
  375. if ($httpRequest->getResponseHttpCode() == 200) {
  376. $tmpFile = \OC_Helper::tmpFile($ext);
  377. $data = $httpRequest->getResponseBody();
  378. file_put_contents($tmpFile, $data);
  379. return fopen($tmpFile, $mode);
  380. }
  381. }
  382. }
  383. return false;
  384. case 'w':
  385. case 'wb':
  386. case 'a':
  387. case 'ab':
  388. case 'r+':
  389. case 'w+':
  390. case 'wb+':
  391. case 'a+':
  392. case 'x':
  393. case 'x+':
  394. case 'c':
  395. case 'c+':
  396. $tmpFile = \OC_Helper::tmpFile($ext);
  397. \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
  398. if ($this->file_exists($path)) {
  399. $source = $this->fopen($path, 'rb');
  400. file_put_contents($tmpFile, $source);
  401. }
  402. self::$tempFiles[$tmpFile] = $path;
  403. return fopen('close://'.$tmpFile, $mode);
  404. }
  405. }
  406. public function writeBack($tmpFile) {
  407. if (isset(self::$tempFiles[$tmpFile])) {
  408. $path = self::$tempFiles[$tmpFile];
  409. $parentFolder = $this->getDriveFile(dirname($path));
  410. if ($parentFolder) {
  411. // TODO Research resumable upload
  412. $mimetype = \OC_Helper::getMimeType($tmpFile);
  413. $data = file_get_contents($tmpFile);
  414. $params = array(
  415. 'data' => $data,
  416. 'mimeType' => $mimetype,
  417. );
  418. $result = false;
  419. if ($this->file_exists($path)) {
  420. $file = $this->getDriveFile($path);
  421. $result = $this->service->files->update($file->getId(), $file, $params);
  422. } else {
  423. $file = new \Google_DriveFile();
  424. $file->setTitle(basename($path));
  425. $file->setMimeType($mimetype);
  426. $parent = new \Google_ParentReference();
  427. $parent->setId($parentFolder->getId());
  428. $file->setParents(array($parent));
  429. $result = $this->service->files->insert($file, $params);
  430. }
  431. if ($result) {
  432. $this->setDriveFile($path, $result);
  433. }
  434. }
  435. unlink($tmpFile);
  436. }
  437. }
  438. public function getMimeType($path) {
  439. $file = $this->getDriveFile($path);
  440. if ($file) {
  441. $mimetype = $file->getMimeType();
  442. // Convert Google Doc mimetypes, choosing Open Document formats for download
  443. if ($mimetype === self::FOLDER) {
  444. return 'httpd/unix-directory';
  445. } else if ($mimetype === self::DOCUMENT) {
  446. return 'application/vnd.oasis.opendocument.text';
  447. } else if ($mimetype === self::SPREADSHEET) {
  448. return 'application/x-vnd.oasis.opendocument.spreadsheet';
  449. } else if ($mimetype === self::DRAWING) {
  450. return 'image/jpeg';
  451. } else if ($mimetype === self::PRESENTATION) {
  452. // Download as .odp is not available
  453. return 'application/pdf';
  454. } else {
  455. return $mimetype;
  456. }
  457. } else {
  458. return false;
  459. }
  460. }
  461. public function free_space($path) {
  462. $about = $this->service->about->get();
  463. return $about->getQuotaBytesTotal() - $about->getQuotaBytesUsed();
  464. }
  465. public function touch($path, $mtime = null) {
  466. $file = $this->getDriveFile($path);
  467. $result = false;
  468. if ($file) {
  469. if (isset($mtime)) {
  470. $file->setModifiedDate($mtime);
  471. $result = $this->service->files->patch($file->getId(), $file, array(
  472. 'setModifiedDate' => true,
  473. ));
  474. } else {
  475. $result = $this->service->files->touch($file->getId());
  476. }
  477. } else {
  478. $parentFolder = $this->getDriveFile(dirname($path));
  479. if ($parentFolder) {
  480. $file = new \Google_DriveFile();
  481. $file->setTitle(basename($path));
  482. $parent = new \Google_ParentReference();
  483. $parent->setId($parentFolder->getId());
  484. $file->setParents(array($parent));
  485. $result = $this->service->files->insert($file);
  486. }
  487. }
  488. if ($result) {
  489. $this->setDriveFile($path, $result);
  490. }
  491. return (bool)$result;
  492. }
  493. public function test() {
  494. if ($this->free_space('')) {
  495. return true;
  496. }
  497. return false;
  498. }
  499. public function hasUpdated($path, $time) {
  500. if ($this->is_file($path)) {
  501. return parent::hasUpdated($path, $time);
  502. } else {
  503. // Google Drive doesn't change modified times of folders when files inside are updated
  504. // Instead we use the Changes API to see if folders have been updated, and it's a pain
  505. $folder = $this->getDriveFile($path);
  506. if ($folder) {
  507. $result = false;
  508. $folderId = $folder->getId();
  509. $startChangeId = \OC_Appconfig::getValue('files_external', $this->getId().'cId');
  510. $params = array(
  511. 'includeDeleted' => true,
  512. 'includeSubscribed' => true,
  513. );
  514. if (isset($startChangeId)) {
  515. $startChangeId = (int)$startChangeId;
  516. $largestChangeId = $startChangeId;
  517. $params['startChangeId'] = $startChangeId + 1;
  518. } else {
  519. $largestChangeId = 0;
  520. }
  521. $pageToken = true;
  522. while ($pageToken) {
  523. if ($pageToken !== true) {
  524. $params['pageToken'] = $pageToken;
  525. }
  526. $changes = $this->service->changes->listChanges($params);
  527. if ($largestChangeId === 0 || $largestChangeId === $startChangeId) {
  528. $largestChangeId = $changes->getLargestChangeId();
  529. }
  530. if (isset($startChangeId)) {
  531. // Check if a file in this folder has been updated
  532. // There is no way to filter by folder at the API level...
  533. foreach ($changes->getItems() as $change) {
  534. $file = $change->getFile();
  535. if ($file) {
  536. foreach ($file->getParents() as $parent) {
  537. if ($parent->getId() === $folderId) {
  538. $result = true;
  539. // Check if there are changes in different folders
  540. } else if ($change->getId() <= $largestChangeId) {
  541. // Decrement id so this change is fetched when called again
  542. $largestChangeId = $change->getId();
  543. $largestChangeId--;
  544. }
  545. }
  546. }
  547. }
  548. $pageToken = $changes->getNextPageToken();
  549. } else {
  550. // Assuming the initial scan just occurred and changes are negligible
  551. break;
  552. }
  553. }
  554. \OC_Appconfig::setValue('files_external', $this->getId().'cId', $largestChangeId);
  555. return $result;
  556. }
  557. }
  558. return false;
  559. }
  560. }