scanner.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Files\Utils;
  9. use OC\Files\Filesystem;
  10. use OC\Hooks\PublicEmitter;
  11. /**
  12. * Class Scanner
  13. *
  14. * Hooks available in scope \OC\Utils\Scanner
  15. * - scanFile(string $absolutePath)
  16. * - scanFolder(string $absolutePath)
  17. *
  18. * @package OC\Files\Utils
  19. */
  20. class Scanner extends PublicEmitter {
  21. /**
  22. * @var string $user
  23. */
  24. private $user;
  25. /**
  26. * @param string $user
  27. */
  28. public function __construct($user) {
  29. $this->user = $user;
  30. }
  31. /**
  32. * get all storages for $dir
  33. *
  34. * @param string $dir
  35. * @return \OC\Files\Mount\Mount[]
  36. */
  37. protected function getMounts($dir) {
  38. //TODO: move to the node based fileapi once that's done
  39. \OC_Util::tearDownFS();
  40. \OC_Util::setupFS($this->user);
  41. $absolutePath = Filesystem::getView()->getAbsolutePath($dir);
  42. $mountManager = Filesystem::getMountManager();
  43. $mounts = $mountManager->findIn($absolutePath);
  44. $mounts[] = $mountManager->find($absolutePath);
  45. $mounts = array_reverse($mounts); //start with the mount of $dir
  46. return $mounts;
  47. }
  48. /**
  49. * attach listeners to the scanner
  50. *
  51. * @param \OC\Files\Mount\Mount $mount
  52. */
  53. protected function attachListener($mount) {
  54. $scanner = $mount->getStorage()->getScanner();
  55. $emitter = $this;
  56. $scanner->listen('\OC\Files\Cache\Scanner', 'scanFile', function ($path) use ($mount, $emitter) {
  57. $emitter->emit('\OC\Files\Utils\Scanner', 'scanFile', array($mount->getMountPoint() . $path));
  58. });
  59. $scanner->listen('\OC\Files\Cache\Scanner', 'scanFolder', function ($path) use ($mount, $emitter) {
  60. $emitter->emit('\OC\Files\Utils\Scanner', 'scanFolder', array($mount->getMountPoint() . $path));
  61. });
  62. }
  63. public function backgroundScan($dir) {
  64. $mounts = $this->getMounts($dir);
  65. foreach ($mounts as $mount) {
  66. if (is_null($mount->getStorage())) {
  67. continue;
  68. }
  69. $scanner = $mount->getStorage()->getScanner();
  70. $this->attachListener($mount);
  71. $scanner->backgroundScan();
  72. }
  73. }
  74. public function scan($dir) {
  75. $mounts = $this->getMounts($dir);
  76. foreach ($mounts as $mount) {
  77. if (is_null($mount->getStorage())) {
  78. continue;
  79. }
  80. $scanner = $mount->getStorage()->getScanner();
  81. $this->attachListener($mount);
  82. $scanner->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG);
  83. }
  84. }
  85. }