diff options
author | Robin Appelman <icewind@owncloud.com> | 2012-02-08 17:30:16 +0100 |
---|---|---|
committer | Robin Appelman <icewind@owncloud.com> | 2012-02-08 17:30:16 +0100 |
commit | c4a6b998146c29289794c1e15b71f1cfcfb4229a (patch) | |
tree | c1f1f6932798b7b4a8f6817240b1bf80064d2da1 /apps/media | |
parent | bcebfbfbe229b5fdb1cfc1c058e11e78550ff088 (diff) | |
download | nextcloud-server-c4a6b998146c29289794c1e15b71f1cfcfb4229a.tar.gz nextcloud-server-c4a6b998146c29289794c1e15b71f1cfcfb4229a.zip |
use oc_filecache and oc_eventsource for music scanning
Diffstat (limited to 'apps/media')
-rw-r--r-- | apps/media/ajax/api.php | 28 | ||||
-rw-r--r-- | apps/media/js/scanner.js | 88 | ||||
-rw-r--r-- | apps/media/lib_scanner.php | 36 |
3 files changed, 38 insertions, 114 deletions
diff --git a/apps/media/ajax/api.php b/apps/media/ajax/api.php index 4a93e84910e..ac6739a1386 100644 --- a/apps/media/ajax/api.php +++ b/apps/media/ajax/api.php @@ -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; -} - ?> diff --git a/apps/media/js/scanner.js b/apps/media/js/scanner.js index ed2046dd7a6..0baa9db419a 100644 --- a/apps/media/js/scanner.js +++ b/apps/media/js/scanner.js @@ -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')); - } - } } diff --git a/apps/media/lib_scanner.php b/apps/media/lib_scanner.php index 8b659c73b6e..ea594ee8e3c 100644 --- a/apps/media/lib_scanner.php +++ b/apps/media/lib_scanner.php @@ -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; } |