aboutsummaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorRobin Appelman <icewind1991@gmail.com>2011-08-01 23:53:01 +0200
committerRobin Appelman <icewind1991@gmail.com>2011-08-01 23:55:47 +0200
commit9f981de85437930ec89ecc85c2e79f096f65b5bb (patch)
tree49a313889f8aefdc131af360d539a4840d7422d5 /apps
parentad45c78b44f887be547329501cba7c84c3a3c331 (diff)
downloadnextcloud-server-9f981de85437930ec89ecc85c2e79f096f65b5bb.tar.gz
nextcloud-server-9f981de85437930ec89ecc85c2e79f096f65b5bb.zip
fance collection scanning wip
Diffstat (limited to 'apps')
-rw-r--r--apps/media/ajax/api.php46
-rw-r--r--apps/media/index.php1
-rw-r--r--apps/media/js/collection.js42
-rw-r--r--apps/media/js/scanner.js72
-rw-r--r--apps/media/lib_scanner.php13
-rw-r--r--apps/media/templates/collection.php9
6 files changed, 170 insertions, 13 deletions
diff --git a/apps/media/ajax/api.php b/apps/media/ajax/api.php
index bf1ebd8e4d1..60458167a96 100644
--- a/apps/media/ajax/api.php
+++ b/apps/media/ajax/api.php
@@ -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;
}
?> \ No newline at end of file
diff --git a/apps/media/index.php b/apps/media/index.php
index 43423d27de6..a7128aaad42 100644
--- a/apps/media/index.php
+++ b/apps/media/index.php
@@ -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');
diff --git a/apps/media/js/collection.js b/apps/media/js/collection.js
index 95e5293ea48..42d249112b1 100644
--- a/apps/media/js/collection.js
+++ b/apps/media/js/collection.js
@@ -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();
+ });
});
diff --git a/apps/media/js/scanner.js b/apps/media/js/scanner.js
new file mode 100644
index 00000000000..e3867580bdb
--- /dev/null
+++ b/apps/media/js/scanner.js
@@ -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');
+ }
+ }
+
+}
diff --git a/apps/media/lib_scanner.php b/apps/media/lib_scanner.php
index ab38d76294b..c774c3c9fdb 100644
--- a/apps/media/lib_scanner.php
+++ b/apps/media/lib_scanner.php
@@ -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';
+ }
} \ No newline at end of file
diff --git a/apps/media/templates/collection.php b/apps/media/templates/collection.php
index e2c256a6480..fe50bf5ebdf 100644
--- a/apps/media/templates/collection.php
+++ b/apps/media/templates/collection.php
@@ -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> \ No newline at end of file
+</ul>
+