use oc_filecache and oc_eventsource for music scanning

remotes/origin/stable4
Robin Appelman 2012-02-08 17:30:16 +07:00
parent bcebfbfbe2
commit c4a6b99814
3 changed files with 38 additions and 114 deletions

@ -70,10 +70,10 @@ if($arguments['action']){
case 'scan':
OC_DB::beginTransaction();
set_time_limit(0); //recursive scan can take a while
$path=$arguments['path'];
echo OC_MEDIA_SCANNER::scanFolder($path);
$eventSource=new OC_EventSource();
OC_MEDIA_SCANNER::scanCollection($eventSource);
$eventSource->close();
OC_DB::commit();
flush();
break;
case 'scanFile':
echo (OC_MEDIA_SCANNER::scanFile($arguments['path']))?'true':'false';
@ -127,29 +127,9 @@ if($arguments['action']){
OC_Filesystem::readfile($arguments['path']);
exit;
case 'find_music':
OC_JSON::encodedPrint(findMusic());
OC_JSON::encodedPrint(OC_FileCache::searchByMime('audio'));
exit;
}
}
function findMusic($path=''){
$music=array();
$dh=OC_Filesystem::opendir($path);
if($dh){
while($filename=readdir($dh)){
if($filename[0]!='.'){
$file=$path.'/'.$filename;
if(OC_Filesystem::is_dir($file)){
$music=array_merge($music,findMusic($file));
}else{
if(OC_MEDIA_SCANNER::isMusic($filename)){
$music[]=$file;
}
}
}
}
}
return $music;
}
?>

@ -1,90 +1,44 @@
Scanner={
songsFound:0,
eventSource:null,
songsScanned:0,
songsChecked:0,
startTime:null,
endTime:null,
stopScanning:false,
currentIndex:0,
songs:[],
findSongs:function(ready){
$.getJSON(OC.linkTo('media','ajax/api.php')+'?action=find_music',function(songs){
Scanner.songsFound=songs.length;
Scanner.currentIndex=-1
if(ready){
ready(songs)
}
});
},
scanFile:function(path,ready){
path=encodeURIComponent(path);
$.getJSON(OC.linkTo('media','ajax/api.php')+'?action=get_path_info&path='+path,function(song){
Scanner.songsChecked++;
if(ready){
ready(song);
}
if(song){//do this after the ready call so we dont hold up the next ajax call
var artistId=song.song_artist;
Scanner.songsScanned++;
$('#scan span.songCount').text(Scanner.songsScanned);
var progress=(Scanner.songsChecked/Scanner.songsFound)*100;
$('#scanprogressbar').progressbar('value',progress)
Collection.addSong(song);
}
});
},
scanCollection:function(ready){
$('#scanprogressbar').progressbar({
value:0,
});
$('#scanprogressbar').show();
Scanner.songsChecked=0;
Scanner.currentIndex=0;
Scanner.songsScanned=0;
Scanner.startTime=new Date().getTime()/1000;
Scanner.findSongs(function(songs){
Scanner.songs=songs;
Scanner.start(function(){
$('#scan input.start').show();
$('#scan input.stop').hide();
$('#scanprogressbar').hide();
Collection.display();
if(ready){
ready();
}
});
Scanner.eventSource=new OC.EventSource(OC.linkTo('media','ajax/api.php'),{action:'scan'});
Scanner.eventSource.listen('count',function(total){
Scanner.songsFound=total;
});
Scanner.eventSource.listen('scanned',function(data){
Scanner.songsScanned=data.count;
$('#scan span.songCount').text(Scanner.songsScanned);
var progress=(Scanner.songsScanned/Scanner.songsFound)*100;
$('#scanprogressbar').progressbar('value',progress)
});
Scanner.eventSource.listen('done',function(count){
$('#scan input.start').show();
$('#scan input.stop').hide();
$('#scanprogressbar').hide();
Collection.load(Collection.display)
if(ready){
ready();
}
});
$('#scancount').show();
},
stop:function(){
Scanner.stopScanning=true;
},
start:function(ready){
Scanner.stopScanning=false;
$('#scancount').show();
var scanSong=function(){
if(!Scanner.stopScanning && Scanner.currentIndex<=Scanner.songs.length){
Scanner.scanFile(Scanner.songs[Scanner.currentIndex],scanSong)
}else if(!Scanner.stopScanning){
Scanner.endTime=new Date().getTime()/1000;
if(ready){
ready();
ready=null;//only call ready once
}
}
Scanner.currentIndex++;
}
scanSong();
scanSong();
Scanner.close();
},
toggle:function(){
if(Scanner.stopScanning){
Scanner.start();
$('#scan input.stop').val(t('media','Pause'));
}else{
Scanner.stop();
$('#scan input.stop').val(t('media','Resume'));
}
}
}

@ -33,32 +33,22 @@ class OC_MEDIA_SCANNER{
/**
* scan a folder for music
* @param string $path
* @param OC_EventSource eventSource (optional)
* @return int the number of songs found
*/
public static function scanFolder($path){
if (OC_Filesystem::is_dir($path)) {
$songs=0;
if ($dh = OC_Filesystem::opendir($path)) {
while (($filename = readdir($dh)) !== false) {
if($filename<>'.' and $filename<>'..' and substr($filename,0,1)!='.'){
$file=$path.'/'.$filename;
if(OC_Filesystem::is_dir($file)){
$songs+=self::scanFolder($file);
}elseif(OC_Filesystem::is_file($file)){
$data=self::scanFile($file);
if($data){
$songs++;
}
}
}
}
public static function scanCollection($eventSource=null){
$music=OC_FileCache::searchByMime('audio');
$eventSource->send('count',count($music));
$songs=0;
foreach($music as $file){
self::scanFile($file);
$songs++;
if($eventSource){
$eventSource->send('scanned',array('file'=>$file,'count'=>$songs));
}
}elseif(OC_Filesystem::is_file($path)){
$songs=1;
self::scanFile($path);
}else{
$songs=0;
}
if($eventSource){
$eventSource->send('done',$songs);
}
return $songs;
}