Blame view

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