diff options
author | Robin Appelman <icewind@owncloud.com> | 2013-07-19 16:44:47 +0200 |
---|---|---|
committer | Robin Appelman <icewind@owncloud.com> | 2013-07-19 16:44:47 +0200 |
commit | 2b89b7c88046f8eb8d2676d0a455ca0d0ba50eeb (patch) | |
tree | 67543ebf039f224dcf2304e9b7589ca21659d7d0 /lib/files | |
parent | b397df202290fa29f506e3394a80164725e2e8e1 (diff) | |
download | nextcloud-server-2b89b7c88046f8eb8d2676d0a455ca0d0ba50eeb.tar.gz nextcloud-server-2b89b7c88046f8eb8d2676d0a455ca0d0ba50eeb.zip |
Split scanning logic from ajax file
Diffstat (limited to 'lib/files')
-rw-r--r-- | lib/files/utils/scanner.php | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/lib/files/utils/scanner.php b/lib/files/utils/scanner.php new file mode 100644 index 00000000000..800bb649934 --- /dev/null +++ b/lib/files/utils/scanner.php @@ -0,0 +1,89 @@ +<?php +/** + * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Files\Utils; + +use OC\Hooks\BasicEmitter; +use OC\Files\Filesystem; + +/** + * Class Scanner + * + * Hooks available in scope \OC\Utils\Scanner + * - scanFile(string $absolutePath) + * - scanFolder(string $absolutePath) + * + * @package OC\Files\Utils + */ +class Scanner extends BasicEmitter { + /** + * @var string $user + */ + private $user; + + /** + * @param string $user + */ + public function __construct($user) { + $this->user = $user; + } + + /** + * get all storages for $dir + * + * @param string $dir + * @return \OC\Files\Mount\Mount[] + */ + protected function getMounts($dir) { + //TODO: move to the node based fileapi once that's done + \OC_Util::tearDownFS(); + \OC_Util::setupFS($this->user); + $absolutePath = Filesystem::getView()->getAbsolutePath($dir); + + $mountManager = Filesystem::getMountManager(); + $mounts = $mountManager->findIn($absolutePath); + $mounts[] = $mountManager->find($absolutePath); + $mounts = array_reverse($mounts); //start with the mount of $dir + + return $mounts; + } + + /** + * attach listeners to the scanner + * + * @param \OC\Files\Mount\Mount $mount + */ + protected function attachListener($mount) { + $scanner = $mount->getStorage()->getScanner(); + $scanner->listen('\OC\Files\Cache\Scanner', 'scanFile', function ($path) use ($mount) { + $this->emit('\OC\Files\Utils\Scanner', 'scanFile', array($mount->getMountPoint() . $path)); + }); + $scanner->listen('\OC\Files\Cache\Scanner', 'scanFolder', function ($path) use ($mount) { + $this->emit('\OC\Files\Utils\Scanner', 'scanFolder', array($mount->getMountPoint() . $path)); + }); + } + + public function backgroundScan($dir) { + $mounts = $this->getMounts($dir); + foreach ($mounts as $mount) { + $scanner = $mount->getStorage()->getScanner(); + $this->attachListener($mount); + $scanner->backgroundScan(); + } + } + + public function scan($dir) { + $mounts = $this->getMounts($dir); + foreach ($mounts as $mount) { + $scanner = $mount->getStorage()->getScanner(); + $this->attachListener($mount); + $scanner->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG); + } + } +} + |