2011-11-10 19:40:09 +04:00
|
|
|
<?php
|
2015-03-26 13:44:34 +03:00
|
|
|
/**
|
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
2015-10-05 21:54:56 +03:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
2012-11-23 03:20:46 +04:00
|
|
|
set_time_limit(0); //scanning can take ages
|
2015-01-20 14:30:16 +03:00
|
|
|
|
|
|
|
\OCP\JSON::checkLoggedIn();
|
|
|
|
\OCP\JSON::callCheck();
|
|
|
|
|
2014-07-16 21:40:22 +04:00
|
|
|
\OC::$server->getSession()->close();
|
2012-11-23 03:20:46 +04:00
|
|
|
|
|
|
|
$force = (isset($_GET['force']) and ($_GET['force'] === 'true'));
|
2015-02-13 15:33:20 +03:00
|
|
|
$dir = isset($_GET['dir']) ? (string)$_GET['dir'] : '';
|
2013-06-19 03:26:08 +04:00
|
|
|
if (isset($_GET['users'])) {
|
2015-01-20 14:30:16 +03:00
|
|
|
\OCP\JSON::checkAdminUser();
|
2013-06-19 03:26:08 +04:00
|
|
|
if ($_GET['users'] === 'all') {
|
|
|
|
$users = OC_User::getUsers();
|
|
|
|
} else {
|
2013-06-19 17:02:18 +04:00
|
|
|
$users = json_decode($_GET['users']);
|
2013-06-19 03:26:08 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$users = array(OC_User::getUser());
|
|
|
|
}
|
2012-11-23 03:20:46 +04:00
|
|
|
|
2014-09-04 03:10:02 +04:00
|
|
|
$eventSource = \OC::$server->createEventSource();
|
2013-07-19 18:44:47 +04:00
|
|
|
$listener = new ScanListener($eventSource);
|
2012-10-27 14:18:01 +04:00
|
|
|
|
2013-06-19 03:26:08 +04:00
|
|
|
foreach ($users as $user) {
|
|
|
|
$eventSource->send('user', $user);
|
2014-09-08 03:34:03 +04:00
|
|
|
$scanner = new \OC\Files\Utils\Scanner($user, \OC::$server->getDatabaseConnection());
|
2013-07-19 18:44:47 +04:00
|
|
|
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', array($listener, 'file'));
|
|
|
|
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', array($listener, 'folder'));
|
2015-08-24 17:42:53 +03:00
|
|
|
try {
|
|
|
|
if ($force) {
|
|
|
|
$scanner->scan($dir);
|
|
|
|
} else {
|
|
|
|
$scanner->backgroundScan($dir);
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$eventSource->send('error', get_class($e) . ': ' . $e->getMessage());
|
2012-11-23 03:20:46 +04:00
|
|
|
}
|
2012-01-31 19:12:38 +04:00
|
|
|
}
|
|
|
|
|
2013-07-19 18:44:47 +04:00
|
|
|
$eventSource->send('done', $listener->getCount());
|
2012-11-23 03:20:46 +04:00
|
|
|
$eventSource->close();
|
2012-01-31 19:12:38 +04:00
|
|
|
|
2012-11-23 03:20:46 +04:00
|
|
|
class ScanListener {
|
2012-08-29 10:42:49 +04:00
|
|
|
|
2013-07-19 18:44:47 +04:00
|
|
|
private $fileCount = 0;
|
|
|
|
private $lastCount = 0;
|
2012-08-29 10:42:49 +04:00
|
|
|
|
2012-11-23 03:20:46 +04:00
|
|
|
/**
|
2014-08-29 19:21:52 +04:00
|
|
|
* @var \OCP\IEventSource event source to pass events to
|
2012-11-23 03:20:46 +04:00
|
|
|
*/
|
2013-07-19 18:44:47 +04:00
|
|
|
private $eventSource;
|
2012-11-23 03:20:46 +04:00
|
|
|
|
|
|
|
/**
|
2014-08-29 19:21:52 +04:00
|
|
|
* @param \OCP\IEventSource $eventSource
|
2012-11-23 03:20:46 +04:00
|
|
|
*/
|
2013-07-19 18:44:47 +04:00
|
|
|
public function __construct($eventSource) {
|
|
|
|
$this->eventSource = $eventSource;
|
|
|
|
}
|
2012-11-23 03:20:46 +04:00
|
|
|
|
|
|
|
/**
|
2013-07-19 18:44:47 +04:00
|
|
|
* @param string $path
|
2012-11-23 03:20:46 +04:00
|
|
|
*/
|
2013-07-19 18:44:47 +04:00
|
|
|
public function folder($path) {
|
|
|
|
$this->eventSource->send('folder', $path);
|
2012-02-05 04:23:04 +04:00
|
|
|
}
|
2012-11-23 03:20:46 +04:00
|
|
|
|
2013-07-19 18:44:47 +04:00
|
|
|
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);
|
2012-11-23 03:20:46 +04:00
|
|
|
}
|
2012-01-31 19:33:16 +04:00
|
|
|
}
|
2013-07-19 18:44:47 +04:00
|
|
|
|
|
|
|
public function getCount() {
|
|
|
|
return $this->fileCount;
|
|
|
|
}
|
2012-01-31 02:32:55 +04:00
|
|
|
}
|