summaryrefslogtreecommitdiffstats
path: root/apps/files/ajax/scan.php
blob: 6659cd459c89f6dfbe208294fb40a85c6938faed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?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'] : '';

$eventSource = new OC_EventSource();
ScanListener::$eventSource = $eventSource;
ScanListener::$view = \OC\Files\Filesystem::getView();

OC_Hook::connect('\OC\Files\Cache\Scanner', 'scan_folder', 'ScanListener', 'folder');
OC_Hook::connect('\OC\Files\Cache\Scanner', 'scan_file', 'ScanListener', 'file');

$absolutePath = \OC\Files\Filesystem::getView()->getAbsolutePath($dir);

$mountPoints = \OC\Files\Filesystem::getMountPoints($absolutePath);
$mountPoints[] = \OC\Files\Filesystem::getMountPoint($absolutePath);
$mountPoints = array_reverse($mountPoints); //start with the mount point of $dir

foreach ($mountPoints as $mountPoint) {
	$storage = \OC\Files\Filesystem::getStorage($mountPoint);
	if ($storage) {
		ScanListener::$mountPoints[$storage->getId()] = $mountPoint;
		$scanner = $storage->getScanner();
		if ($force) {
			$scanner->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG);
		} else {
			$scanner->backgroundScan();
		}
	}
}

$eventSource->send('done', ScanListener::$fileCount);
$eventSource->close();

class ScanListener {

	static public $fileCount = 0;
	static public $lastCount = 0;

	/**
	 * @var \OC\Files\View $view
	 */
	static public $view;

	/**
	 * @var array $mountPoints map storage ids to mountpoints
	 */
	static public $mountPoints = array();

	/**
	 * @var \OC_EventSource event source to pass events to
	 */
	static public $eventSource;

	static function folder($params) {
		$internalPath = $params['path'];
		$mountPoint = self::$mountPoints[$params['storage']];
		$path = self::$view->getRelativePath($mountPoint . $internalPath);
		self::$eventSource->send('folder', $path);
	}

	static function file() {
		self::$fileCount++;
		if (self::$fileCount > self::$lastCount + 20) { //send a count update every 20 files
			self::$lastCount = self::$fileCount;
			self::$eventSource->send('count', self::$fileCount);
		}
	}
}