scanner.php 3.7 KB

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