scan.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  5. * @author Lukas Reschke <lukas@owncloud.com>
  6. * @author Robin Appelman <icewind@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. set_time_limit(0); //scanning can take ages
  25. \OCP\JSON::checkLoggedIn();
  26. \OCP\JSON::callCheck();
  27. \OC::$server->getSession()->close();
  28. $force = (isset($_GET['force']) and ($_GET['force'] === 'true'));
  29. $dir = isset($_GET['dir']) ? (string)$_GET['dir'] : '';
  30. if (isset($_GET['users'])) {
  31. \OCP\JSON::checkAdminUser();
  32. if ($_GET['users'] === 'all') {
  33. $users = OC_User::getUsers();
  34. } else {
  35. $users = json_decode($_GET['users']);
  36. }
  37. } else {
  38. $users = array(OC_User::getUser());
  39. }
  40. $eventSource = \OC::$server->createEventSource();
  41. $listener = new ScanListener($eventSource);
  42. foreach ($users as $user) {
  43. $eventSource->send('user', $user);
  44. $scanner = new \OC\Files\Utils\Scanner($user, \OC::$server->getDatabaseConnection());
  45. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', array($listener, 'file'));
  46. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', array($listener, 'folder'));
  47. if ($force) {
  48. $scanner->scan($dir);
  49. } else {
  50. $scanner->backgroundScan($dir);
  51. }
  52. }
  53. $eventSource->send('done', $listener->getCount());
  54. $eventSource->close();
  55. class ScanListener {
  56. private $fileCount = 0;
  57. private $lastCount = 0;
  58. /**
  59. * @var \OCP\IEventSource event source to pass events to
  60. */
  61. private $eventSource;
  62. /**
  63. * @param \OCP\IEventSource $eventSource
  64. */
  65. public function __construct($eventSource) {
  66. $this->eventSource = $eventSource;
  67. }
  68. /**
  69. * @param string $path
  70. */
  71. public function folder($path) {
  72. $this->eventSource->send('folder', $path);
  73. }
  74. public function file() {
  75. $this->fileCount++;
  76. if ($this->fileCount > $this->lastCount + 20) { //send a count update every 20 files
  77. $this->lastCount = $this->fileCount;
  78. $this->eventSource->send('count', $this->fileCount);
  79. }
  80. }
  81. public function getCount() {
  82. return $this->fileCount;
  83. }
  84. }