scan.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. set_time_limit(0); //scanning can take ages
  3. \OC::$session->close();
  4. $force = (isset($_GET['force']) and ($_GET['force'] === 'true'));
  5. $dir = isset($_GET['dir']) ? $_GET['dir'] : '';
  6. if (isset($_GET['users'])) {
  7. OC_JSON::checkAdminUser();
  8. if ($_GET['users'] === 'all') {
  9. $users = OC_User::getUsers();
  10. } else {
  11. $users = json_decode($_GET['users']);
  12. }
  13. } else {
  14. $users = array(OC_User::getUser());
  15. }
  16. $eventSource = new OC_EventSource();
  17. $listener = new ScanListener($eventSource);
  18. foreach ($users as $user) {
  19. $eventSource->send('user', $user);
  20. $scanner = new \OC\Files\Utils\Scanner($user);
  21. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', array($listener, 'file'));
  22. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', array($listener, 'folder'));
  23. if ($force) {
  24. $scanner->scan($dir);
  25. } else {
  26. $scanner->backgroundScan($dir);
  27. }
  28. }
  29. $eventSource->send('done', $listener->getCount());
  30. $eventSource->close();
  31. class ScanListener {
  32. private $fileCount = 0;
  33. private $lastCount = 0;
  34. /**
  35. * @var \OC_EventSource event source to pass events to
  36. */
  37. private $eventSource;
  38. /**
  39. * @param \OC_EventSource $eventSource
  40. */
  41. public function __construct($eventSource) {
  42. $this->eventSource = $eventSource;
  43. }
  44. /**
  45. * @param string $path
  46. */
  47. public function folder($path) {
  48. $this->eventSource->send('folder', $path);
  49. }
  50. public function file() {
  51. $this->fileCount++;
  52. if ($this->fileCount > $this->lastCount + 20) { //send a count update every 20 files
  53. $this->lastCount = $this->fileCount;
  54. $this->eventSource->send('count', $this->fileCount);
  55. }
  56. }
  57. public function getCount() {
  58. return $this->fileCount;
  59. }
  60. }