|
29 | 29 | use OCP\Files\NotFoundException; |
30 | 30 | use OCP\Files\StorageInvalidException; |
31 | 31 | use OCP\Files\StorageNotAvailableException; |
| 32 | +use OCP\Http\Client\LocalServerException; |
| 33 | +use Psr\Log\LoggerInterface; |
32 | 34 |
|
33 | 35 | class Scanner extends \OC\Files\Cache\Scanner { |
34 | 36 | /** @var \OCA\Files_Sharing\External\Storage */ |
35 | 37 | protected $storage; |
36 | 38 |
|
| 39 | + /** {@inheritDoc} */ |
| 40 | + public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $lock = true) { |
| 41 | + try { |
| 42 | + if (!$this->storage->remoteIsOwnCloud()) { |
| 43 | + return parent::scan($path, $recursive, $reuse, $lock); |
| 44 | + } |
| 45 | + } catch (LocalServerException $e) { |
| 46 | + // Scanner doesn't have dependency injection |
| 47 | + \OC::$server->get(LoggerInterface::class) |
| 48 | + ->warning('Trying to scan files inside invalid external storage: ' . $this->storage->getRemote() . ' for mountpoint ' . $this->storage->getMountPoint() . ' and id ' . $this->storage->getId()); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + $this->scanAll(); |
| 53 | + } |
| 54 | + |
37 | 55 | /** |
38 | 56 | * Scan a single file and store it in the cache. |
39 | 57 | * If an exception happened while accessing the external storage, |
@@ -63,4 +81,56 @@ public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = |
63 | 81 | $this->storage->checkStorageAvailability(); |
64 | 82 | } |
65 | 83 | } |
| 84 | + |
| 85 | + /** |
| 86 | + * Checks the remote share for changes. |
| 87 | + * If changes are available, scan them and update |
| 88 | + * the cache. |
| 89 | + * @throws NotFoundException |
| 90 | + * @throws StorageInvalidException |
| 91 | + * @throws \Exception |
| 92 | + */ |
| 93 | + public function scanAll() { |
| 94 | + try { |
| 95 | + $data = $this->storage->getShareInfo(); |
| 96 | + } catch (\Exception $e) { |
| 97 | + $this->storage->checkStorageAvailability(); |
| 98 | + throw new \Exception( |
| 99 | + 'Error while scanning remote share: "' . |
| 100 | + $this->storage->getRemote() . '" ' . |
| 101 | + $e->getMessage() |
| 102 | + ); |
| 103 | + } |
| 104 | + if ($data['status'] === 'success') { |
| 105 | + $this->addResult($data['data'], ''); |
| 106 | + } else { |
| 107 | + throw new \Exception( |
| 108 | + 'Error while scanning remote share: "' . |
| 109 | + $this->storage->getRemote() . '"' |
| 110 | + ); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @param array $data |
| 116 | + * @param string $path |
| 117 | + */ |
| 118 | + private function addResult($data, $path) { |
| 119 | + $id = $this->cache->put($path, $data); |
| 120 | + if (isset($data['children'])) { |
| 121 | + $children = []; |
| 122 | + foreach ($data['children'] as $child) { |
| 123 | + $children[$child['name']] = true; |
| 124 | + $this->addResult($child, ltrim($path . '/' . $child['name'], '/')); |
| 125 | + } |
| 126 | + |
| 127 | + $existingCache = $this->cache->getFolderContentsById($id); |
| 128 | + foreach ($existingCache as $existingChild) { |
| 129 | + // if an existing child is not in the new data, remove it |
| 130 | + if (!isset($children[$existingChild['name']])) { |
| 131 | + $this->cache->remove(ltrim($path . '/' . $existingChild['name'], '/')); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
66 | 136 | } |
0 commit comments