]> source.dussan.org Git - nextcloud-server.git/commitdiff
some improvements to collection scanning
authorRobin Appelman <icewind1991@gmail.com>
Sun, 31 Jul 2011 22:07:46 +0000 (00:07 +0200)
committerRobin Appelman <icewind1991@gmail.com>
Sun, 31 Jul 2011 22:07:46 +0000 (00:07 +0200)
apps/media/ajax/api.php
apps/media/lib_collection.php
apps/media/lib_scanner.php
lib/db.php

index b86c69d0beb8c8252f7cba46abe5e8c306515e93..bb86c13f2875f088a73e8479be3d58658bc8e39a 100644 (file)
@@ -75,6 +75,7 @@ if($arguments['action']){
                        echo json_encode($artists);
                        break;
                case 'scan':
+                       OC_DB::beginTransaction();
                        set_time_limit(0); //recursive scan can take a while
                        $path=$arguments['path'];
                        if(OC_Filesystem::is_dir($path)){
@@ -85,6 +86,7 @@ if($arguments['action']){
                                }
                        }
                        echo OC_MEDIA_SCANNER::scanFolder($path);
+                       OC_DB::commit();
                        flush();
                        break;
                case 'scanFile':
index 7e540ab585f7e17d1d976b413cf46b8ce3292aa9..4721202690c17764d59f5eeca16266ad487a18e6 100644 (file)
@@ -28,6 +28,7 @@ class OC_MEDIA_COLLECTION{
        private static $artistIdCache=array();
        private static $albumIdCache=array();
        private static $songIdCache=array();
+       private static $queries=array();
        
        /**
        * get the id of an artist (case-insensitive)
@@ -254,8 +255,13 @@ class OC_MEDIA_COLLECTION{
                if($songId!=0){
                        return $songId;
                }else{
-                       $query=OC_DB::prepare("INSERT INTO  `*PREFIX*media_songs` (`song_id` ,`song_name` ,`song_artist` ,`song_album` ,`song_path` ,`song_user`,`song_length`,`song_track`,`song_size`,`song_playcount`,`song_lastplayed`)
-                       VALUES (NULL ,  ?, ?, ?, ?,?,?,?,?,0,0)");
+                       if(!isset(self::$queries['addsong'])){
+                               $query=OC_DB::prepare("INSERT INTO  `*PREFIX*media_songs` (`song_name` ,`song_artist` ,`song_album` ,`song_path` ,`song_user`,`song_length`,`song_track`,`song_size`,`song_playcount`,`song_lastplayed`)
+                               VALUES (?, ?, ?, ?,?,?,?,?,0,0)");
+                               self::$queries['addsong']=$query;
+                       }else{
+                               $query=self::$queries['addsong'];
+                       }
                        $query->execute(array($name,$artist,$album,$path,$uid,$length,$track,$size));
                        $songId=OC_DB::insertid();
 //                     self::setLastUpdated();
index f788562fdbf87995d3d3da74d612e705e729db7c..4d0b38f820ac7f47ec831a6045a3b6b103b2d209 100644 (file)
@@ -30,6 +30,7 @@ class OC_MEDIA_SCANNER{
        //these are used to store which artists and albums we found, it can save a lot of addArtist/addAlbum calls
        static private $artists=array();
        static private $albums=array();//stored as "$artist/$album" to allow albums with the same name from different artists
+       static private $useMp3Info=null;
        
        /**
         * scan a folder for music
@@ -37,7 +38,7 @@ class OC_MEDIA_SCANNER{
         * @return int the number of songs found
         */
        public static function scanFolder($path){
-               OC_DB::beginTransaction();
+//             OC_DB::beginTransaction();
                if (OC_Filesystem::is_dir($path)) {
                        $songs=0;
                        if ($dh = OC_Filesystem::opendir($path)) {
@@ -47,7 +48,8 @@ class OC_MEDIA_SCANNER{
                                                if(OC_Filesystem::is_dir($file)){
                                                        $songs+=self::scanFolder($file);
                                                }elseif(OC_Filesystem::is_file($file)){
-                                                       if(self::scanFile($file)){
+                                                       $data=self::scanFile($file);
+                                                       if($data){
                                                                $songs++;
                                                        }
                                                }
@@ -60,7 +62,7 @@ class OC_MEDIA_SCANNER{
                }else{
                        $songs=0;
                }
-               OC_DB::commit();
+//             OC_DB::commit();
                return $songs;
        }
 
@@ -70,45 +72,28 @@ class OC_MEDIA_SCANNER{
         * @return boolean
         */
        public static function scanFile($path){
+               if(is_null(self::$useMp3Info)){
+                       self::$useMp3Info=OC_Helper::canExecute("mp3info");
+               }
                $file=OC_Filesystem::getLocalFile($path);
-               if(substr($path,-3)=='mp3' and OC_Helper::canExecute("id3info") and OC_Helper::canExecute("mp3info")){//use the command line tool id3info if possible
+               if(substr($path,-3)=='mp3' and self::$useMp3Info){//use the command line tool id3info if possible
                        $output=array();
                        $size=filesize($file);
-                       $length=0;
-                       $title='unknown';
-                       $album='unknown';
-                       $artist='unknown';
-                       $track=0;
-                       exec('id3info "'.$file.'"',$output);
-                       $data=array();
-                       foreach($output as $line) {
-                               switch(substr($line,0,3)){
-                                       case '***'://comments
-                                               break;
-                                       case '==='://tag information
-                                               $key=substr($line,4,4);
-                                               $value=substr($line,strpos($line,':')+2);
-                                               switch(strtolower($key)){
-                                                       case 'tit1':
-                                                       case 'tit2':
-                                                               $title=$value;
-                                                               break;
-                                                       case 'tpe1':
-                                                       case 'tpe2':
-                                                               $artist=$value;
-                                                               break;
-                                                       case 'talb':
-                                                               $album=$value;
-                                                               break;
-                                                       case 'trck':
-                                                               $track=$value;
-                                                               break;
-                                               }
-                                               break;
-                               }
+                       exec('mp3info -p "%a\n%l\n%t\n%n\n%S" "'.$file.'"',$output);
+                       if(count($output)>4){
+                               $artist=$output[0];
+                               $album=$output[1];
+                               $title=$output[2];
+                               $track=$output[3];
+                               $length=$output[4];
+                       }else{
+                               return; //invalid mp3 file
                        }
-                       $length=exec('mp3info -p "%S" "'.$file.'"');
                }else{
+                       $mimetype=OC_Filesystem::getMimeType($path);
+                       if(substr($mimetype,0,4)!=='audio'){
+                               return;
+                       }
                        if(!self::$getID3){
                                self::$getID3=@new getID3();
                        }
index b7775b75ea45cac1c7afdbcd6e306b2e2b577bb4..9b18bc4c97f8e61b119afbe28b872088cfeb83c0 100644 (file)
@@ -367,6 +367,7 @@ class OC_DB {
         * @param string $savePoint (optional) name of the savepoint to set
         */
        public static function beginTransaction($savePoint=''){
+               self::connect();
                if (!self::$DBConnection->supports('transactions')) {
                        return false;
                }
@@ -385,6 +386,7 @@ class OC_DB {
         * @param string $savePoint (optional) name of the savepoint to commit
         */
        public static function commit($savePoint=''){
+               self::connect();
                if(!self::$DBConnection->inTransaction()){
                        return false;
                }