use background scanner
This commit is contained in:
parent
ad706229f5
commit
8fb4dfd2ea
|
@ -1,45 +1,70 @@
|
||||||
<?php
|
<?php
|
||||||
|
set_time_limit(0); //scanning can take ages
|
||||||
return;
|
|
||||||
|
|
||||||
set_time_limit(0);//scanning can take ages
|
|
||||||
|
|
||||||
$force=isset($_GET['force']) and $_GET['force']=='true';
|
|
||||||
$dir=isset($_GET['dir'])?$_GET['dir']:'';
|
|
||||||
$checkOnly=isset($_GET['checkonly']) and $_GET['checkonly']=='true';
|
|
||||||
|
|
||||||
if(!$checkOnly) {
|
|
||||||
$eventSource=new OC_EventSource();
|
|
||||||
}
|
|
||||||
|
|
||||||
session_write_close();
|
session_write_close();
|
||||||
|
|
||||||
//create the file cache if necesary
|
$force = (isset($_GET['force']) and ($_GET['force'] === 'true'));
|
||||||
if($force or !OC_FileCache::inCache('')) {
|
$dir = isset($_GET['dir']) ? $_GET['dir'] : '';
|
||||||
if(!$checkOnly) {
|
|
||||||
OCP\DB::beginTransaction();
|
|
||||||
|
|
||||||
if(OC_Cache::isFast()) {
|
$eventSource = new OC_EventSource();
|
||||||
OC_Cache::clear('fileid/'); //make sure the old fileid's don't mess things up
|
ScanListener::$eventSource = $eventSource;
|
||||||
}
|
ScanListener::$view = \OC\Files\Filesystem::getView();
|
||||||
|
|
||||||
OC_FileCache::scan($dir, $eventSource);
|
OC_Hook::connect('\OC\Files\Cache\Scanner', 'scan_folder', 'ScanListener', 'folder');
|
||||||
OC_FileCache::clean();
|
OC_Hook::connect('\OC\Files\Cache\Scanner', 'scan_file', 'ScanListener', 'file');
|
||||||
OCP\DB::commit();
|
|
||||||
$eventSource->send('success', true);
|
$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);
|
||||||
|
error_log('scanning mp '.$mountPoint);
|
||||||
|
ScanListener::$mountPoints[$storage->getId()] = $mountPoint;
|
||||||
|
$scanner = $storage->getScanner();
|
||||||
|
if ($force) {
|
||||||
|
$scanner->scan('');
|
||||||
} else {
|
} else {
|
||||||
OCP\JSON::success(array('data'=>array('done'=>true)));
|
$scanner->backgroundScan();
|
||||||
exit;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if($checkOnly) {
|
|
||||||
OCP\JSON::success(array('data'=>array('done'=>false)));
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
if(isset($eventSource)) {
|
|
||||||
$eventSource->send('success', false);
|
|
||||||
} else {
|
|
||||||
exit;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$eventSource->send('done', ScanListener::$fileCount);
|
||||||
$eventSource->close();
|
$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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -602,12 +602,8 @@ $(document).ready(function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
//check if we need to scan the filesystem
|
//do a background scan if needed
|
||||||
$.get(OC.filePath('files','ajax','scan.php'),{checkonly:'true'}, function(response) {
|
scanFiles();
|
||||||
if(response.data.done){
|
|
||||||
scanFiles();
|
|
||||||
}
|
|
||||||
}, "json");
|
|
||||||
|
|
||||||
var lastWidth = 0;
|
var lastWidth = 0;
|
||||||
var breadcrumbs = [];
|
var breadcrumbs = [];
|
||||||
|
@ -676,27 +672,23 @@ $(document).ready(function() {
|
||||||
resizeBreadcrumbs(true);
|
resizeBreadcrumbs(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
function scanFiles(force,dir){
|
function scanFiles(force, dir){
|
||||||
if(!dir){
|
if(!dir){
|
||||||
dir='';
|
dir = '';
|
||||||
}
|
}
|
||||||
force=!!force; //cast to bool
|
force = !!force; //cast to bool
|
||||||
scanFiles.scanning=true;
|
scanFiles.scanning = true;
|
||||||
$('#scanning-message').show();
|
var scannerEventSource = new OC.EventSource(OC.filePath('files','ajax','scan.php'),{force:force,dir:dir});
|
||||||
$('#fileList').remove();
|
scanFiles.cancel = scannerEventSource.close.bind(scannerEventSource);
|
||||||
var scannerEventSource=new OC.EventSource(OC.filePath('files','ajax','scan.php'),{force:force,dir:dir});
|
scannerEventSource.listen('count',function(count){
|
||||||
scanFiles.cancel=scannerEventSource.close.bind(scannerEventSource);
|
console.log(count + 'files scanned')
|
||||||
scannerEventSource.listen('scanning',function(data){
|
|
||||||
$('#scan-count').text(t('files', '{count} files scanned', {count: data.count}));
|
|
||||||
$('#scan-current').text(data.file+'/');
|
|
||||||
});
|
});
|
||||||
scannerEventSource.listen('success',function(success){
|
scannerEventSource.listen('folder',function(path){
|
||||||
|
console.log('now scanning ' + path)
|
||||||
|
});
|
||||||
|
scannerEventSource.listen('done',function(count){
|
||||||
scanFiles.scanning=false;
|
scanFiles.scanning=false;
|
||||||
if(success){
|
console.log('done after ' + count + 'files');
|
||||||
window.location.reload();
|
|
||||||
}else{
|
|
||||||
alert(t('files', 'error while scanning'));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
scanFiles.scanning=false;
|
scanFiles.scanning=false;
|
||||||
|
|
Loading…
Reference in New Issue