fance collection scanning wip

This commit is contained in:
Robin Appelman 2011-08-01 23:53:01 +02:00
parent ad45c78b44
commit 9f981de854
6 changed files with 170 additions and 13 deletions

View File

@ -53,6 +53,7 @@ if(!isset($arguments['search'])){
$arguments['search']='';
}
OC_MEDIA_COLLECTION::$uid=OC_User::getUser();
unset($_SESSION['collection']);
if($arguments['action']){
switch($arguments['action']){
case 'delete':
@ -108,16 +109,18 @@ if($arguments['action']){
echo json_encode(OC_MEDIA_COLLECTION::getSongs($arguments['artist'],$arguments['album'],$arguments['search']));
break;
case 'get_path_info':
$songId=OC_MEDIA_COLLECTION::getSongByPath($arguments['path']);
if($songId==0){
unset($_SESSION['collection']);
$songId= OC_MEDIA_SCANNER::scanFile($arguments['path']);
}
if($songId>0){
$song=OC_MEDIA_COLLECTION::getSong($songId);
$song['artist']=OC_MEDIA_COLLECTION::getArtistName($song['song_artist']);
$song['album']=OC_MEDIA_COLLECTION::getAlbumName($song['song_album']);
echo json_encode($song);
if(OC_Filesystem::file_exists($arguments['path'])){
$songId=OC_MEDIA_COLLECTION::getSongByPath($arguments['path']);
if($songId==0){
unset($_SESSION['collection']);
$songId= OC_MEDIA_SCANNER::scanFile($arguments['path']);
}
if($songId>0){
$song=OC_MEDIA_COLLECTION::getSong($songId);
$song['artist']=OC_MEDIA_COLLECTION::getArtistName($song['song_artist']);
$song['album']=OC_MEDIA_COLLECTION::getAlbumName($song['song_album']);
echo json_encode($song);
}
}
break;
case 'play':
@ -137,7 +140,30 @@ if($arguments['action']){
OC_Filesystem::readfile($arguments['path']);
exit;
case 'find_music':
echo json_encode(findMusic());
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;
}
?>

View File

@ -37,6 +37,7 @@ OC_Util::addScript('media','player');
OC_Util::addScript('media','music');
OC_Util::addScript('media','playlist');
OC_Util::addScript('media','collection');
OC_Util::addScript('media','scanner');
OC_Util::addScript('media','jquery.jplayer.min');
OC_Util::addStyle('media','player');
OC_Util::addStyle('media','music');

View File

@ -32,6 +32,10 @@ Collection={
for(var i=0;i<Collection.loadedListeners.length;i++){
Collection.loadedListeners[i]();
}
if(collection.length==0){
$('#scan input.start').val('Scan');
$('#plugins a[href="#collection"]').trigger('click');
}
}
});
@ -159,6 +163,35 @@ Collection={
}
}
}
},
addSong:function(song){
var artist=false
var album=false;
for(var i=0;i<Collection.artists.length;i++){
if(Collection.artists[i].artist_id==song.song_artist){
artist=Collection.artists[i];
for(var j=0;j<artist.albums.length;j++){
if(artist.albums[j].album_id==song.song_album){
album=artist.albums[j];
break;
}
}
break;
}
}
if(!artist){
artist={artist_id:song.song_artist,artist_name:song.artist,albums:[]};
Collection.artists.push(artist);
if(!Collection.parent || Collection.parent.is(":visible")){
Collection.display();
}
}
if(!album){
album={album_id:song.song_album,album_name:song.album,album_artist:song.song_artist,songs:[]};
artist.albums.push(album)
}
album.songs.push(song)
}
}
@ -173,4 +206,13 @@ $(document).ready(function(){
$(this).parent().toggleClass('active');
Collection.showSongs($(this).parent());
});
Collection.parent.hide();
$('#scan input.start').click(function(){
$('#scan input.start').hide();
$('#scan input.stop').show();
$('#scan input.stop').click(function(){
Scanner.toggle();
});
Scanner.scanCollection();
});
});

72
apps/media/js/scanner.js Normal file
View File

@ -0,0 +1,72 @@
Scanner={
songsFound:0,
songsScanned:0,
startTime:null,
endTime:null,
stopScanning:false,
currentIndex:-1,
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){
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.songsScanned/Scanner.songsFound)*100;
$('#scanprogressbar').progressbar('value',progress)
Collection.addSong(song);
}
});
},
scanCollection:function(ready){
$('#scanprogressbar').progressbar({
value:0,
});
Scanner.startTime=new Date().getTime()/1000;
Scanner.findSongs(function(songs){
Scanner.songs=songs;
Scanner.start();
});
},
stop:function(){
Scanner.stopScanning=true;
},
start:function(ready){
Scanner.stopScanning=false;
var scanSong=function(){
Scanner.currentIndex++;
if(!Scanner.stopScanning && Scanner.currentIndex<Scanner.songs.length){
Scanner.scanFile(Scanner.songs[Scanner.currentIndex],scanSong)
}else{
Scanner.endTime=new Date().getTime()/1000;
if(ready){
ready();
}
}
}
scanSong();
},
toggle:function(){
if(Scanner.stopScanning){
Scanner.start();
$('#scan input.stop').val('Pause');
}else{
Scanner.stop();
$('#scan input.stop').val('Resume');
}
}
}

View File

@ -88,8 +88,7 @@ class OC_MEDIA_SCANNER{
return; //invalid mp3 file
}
}else{
$mimetype=OC_Filesystem::getMimeType($path);
if(substr($mimetype,0,4)!=='audio'){
if(!self::isMusic($path)){
return;
}
if(!self::$getID3){
@ -141,4 +140,14 @@ class OC_MEDIA_SCANNER{
$songId=OC_MEDIA_COLLECTION::addSong($title,$path,$artistId,$albumId,$length,$track,$size);
return (!($title=='unkown' && $artist=='unkown' && $album=='unkown'))?$songId:0;
}
/**
* quick check if a song is a music file by checking the extention, not as good as a proper mimetype check but way faster
* @param string $filename
* @return bool
*/
public static function isMusic($filename){
$ext=substr($filename,strrpos($filename,'.')+1);
return $ext=='mp3' || $ext=='flac' || $ext=='m4a' || $ext=='ogg' || $ext=='oga';
}
}

View File

@ -1,3 +1,9 @@
<div id='scan'>
<p><span class='songCount'>0</span> Songs scanned</p>
<div id="scanprogressbar"></div>
<input type='button' class='start' value='Recan'></input>
<input type='button' class='stop' style='display:none' value='Pause'></input>
</div>
<ul id='collection'>
<li class='artist'>
<img src="<?php echo image_path('files','loading.gif') ?>" alt='loading'/>Loading Collection...
@ -7,4 +13,5 @@
<button class='add'>Add</button>
<button class='play'>Play</button>
</li>
</ul>
</ul>