You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Scanner.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Olivier Paroz <github@oparoz.com>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Vincent Petry <pvince81@owncloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Files_Sharing\External;
  28. use OC\ForbiddenException;
  29. use OCP\Files\NotFoundException;
  30. use OCP\Files\StorageInvalidException;
  31. use OCP\Files\StorageNotAvailableException;
  32. class Scanner extends \OC\Files\Cache\Scanner {
  33. /** @var \OCA\Files_Sharing\External\Storage */
  34. protected $storage;
  35. /** {@inheritDoc} */
  36. public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $lock = true) {
  37. if (!$this->storage->remoteIsOwnCloud()) {
  38. return parent::scan($path, $recursive, $recursive, $lock);
  39. }
  40. $this->scanAll();
  41. }
  42. /**
  43. * Scan a single file and store it in the cache.
  44. * If an exception happened while accessing the external storage,
  45. * the storage will be checked for availability and removed
  46. * if it is not available any more.
  47. *
  48. * @param string $file file to scan
  49. * @param int $reuseExisting
  50. * @param int $parentId
  51. * @param array | null $cacheData existing data in the cache for the file to be scanned
  52. * @param bool $lock set to false to disable getting an additional read lock during scanning
  53. * @return array an array of metadata of the scanned file
  54. */
  55. public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = null, $lock = true, $data = null) {
  56. try {
  57. return parent::scanFile($file, $reuseExisting);
  58. } catch (ForbiddenException $e) {
  59. $this->storage->checkStorageAvailability();
  60. } catch (NotFoundException $e) {
  61. // if the storage isn't found, the call to
  62. // checkStorageAvailable() will verify it and remove it
  63. // if appropriate
  64. $this->storage->checkStorageAvailability();
  65. } catch (StorageInvalidException $e) {
  66. $this->storage->checkStorageAvailability();
  67. } catch (StorageNotAvailableException $e) {
  68. $this->storage->checkStorageAvailability();
  69. }
  70. }
  71. /**
  72. * Checks the remote share for changes.
  73. * If changes are available, scan them and update
  74. * the cache.
  75. * @throws NotFoundException
  76. * @throws StorageInvalidException
  77. * @throws \Exception
  78. */
  79. public function scanAll() {
  80. try {
  81. $data = $this->storage->getShareInfo();
  82. } catch (\Exception $e) {
  83. $this->storage->checkStorageAvailability();
  84. throw new \Exception(
  85. 'Error while scanning remote share: "' .
  86. $this->storage->getRemote() . '" ' .
  87. $e->getMessage()
  88. );
  89. }
  90. if ($data['status'] === 'success') {
  91. $this->addResult($data['data'], '');
  92. } else {
  93. throw new \Exception(
  94. 'Error while scanning remote share: "' .
  95. $this->storage->getRemote() . '"'
  96. );
  97. }
  98. }
  99. /**
  100. * @param array $data
  101. * @param string $path
  102. */
  103. private function addResult($data, $path) {
  104. $id = $this->cache->put($path, $data);
  105. if (isset($data['children'])) {
  106. $children = [];
  107. foreach ($data['children'] as $child) {
  108. $children[$child['name']] = true;
  109. $this->addResult($child, ltrim($path . '/' . $child['name'], '/'));
  110. }
  111. $existingCache = $this->cache->getFolderContentsById($id);
  112. foreach ($existingCache as $existingChild) {
  113. // if an existing child is not in the new data, remove it
  114. if (!isset($children[$existingChild['name']])) {
  115. $this->cache->remove(ltrim($path . '/' . $existingChild['name'], '/'));
  116. }
  117. }
  118. }
  119. }
  120. }