From kde-commits Sun Jul 31 22:08:39 2011 From: Robin Appelman Date: Sun, 31 Jul 2011 22:08:39 +0000 To: kde-commits Subject: [owncloud] /: some improvements to collection scanning Message-Id: <20110731220839.1823AA60BB () git ! kde ! org> X-MARC-Message: https://marc.info/?l=kde-commits&m=131215016819859 Git commit 2d199657506a53dda1bd2879f951684c26133ff2 by Robin Appelman. Committed on 01/08/2011 at 00:07. Pushed by rappelman into branch 'master'. some improvements to collection scanning M +2 -0 apps/media/ajax/api.php M +22 -37 apps/media/lib_scanner.php M +2 -0 lib/db.php M +8 -2 apps/media/lib_collection.php http://commits.kde.org/owncloud/2d199657506a53dda1bd2879f951684c26133ff2 diff --git a/apps/media/ajax/api.php b/apps/media/ajax/api.php index b86c69d..bb86c13 100644 --- a/apps/media/ajax/api.php +++ b/apps/media/ajax/api.php @@ -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': diff --git a/apps/media/lib_collection.php b/apps/media/lib_collection.php index 7e540ab..4721202 100644 --- a/apps/media/lib_collection.php +++ b/apps/media/lib_collection.php @@ -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(); diff --git a/apps/media/lib_scanner.php b/apps/media/lib_scanner.php index f788562..4d0b38f 100644 --- a/apps/media/lib_scanner.php +++ b/apps/media/lib_scanner.php @@ -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(); } diff --git a/lib/db.php b/lib/db.php index b7775b7..9b18bc4 100644 --- a/lib/db.php +++ b/lib/db.php @@ -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; }