some improvements to collection scanning

This commit is contained in:
Robin Appelman 2011-08-01 00:07:46 +02:00
parent 6e2ce76223
commit 2d19965750
4 changed files with 34 additions and 39 deletions

View File

@ -75,6 +75,7 @@ if($arguments['action']){
echo json_encode($artists); echo json_encode($artists);
break; break;
case 'scan': case 'scan':
OC_DB::beginTransaction();
set_time_limit(0); //recursive scan can take a while set_time_limit(0); //recursive scan can take a while
$path=$arguments['path']; $path=$arguments['path'];
if(OC_Filesystem::is_dir($path)){ if(OC_Filesystem::is_dir($path)){
@ -85,6 +86,7 @@ if($arguments['action']){
} }
} }
echo OC_MEDIA_SCANNER::scanFolder($path); echo OC_MEDIA_SCANNER::scanFolder($path);
OC_DB::commit();
flush(); flush();
break; break;
case 'scanFile': case 'scanFile':

View File

@ -28,6 +28,7 @@ class OC_MEDIA_COLLECTION{
private static $artistIdCache=array(); private static $artistIdCache=array();
private static $albumIdCache=array(); private static $albumIdCache=array();
private static $songIdCache=array(); private static $songIdCache=array();
private static $queries=array();
/** /**
* get the id of an artist (case-insensitive) * get the id of an artist (case-insensitive)
@ -254,8 +255,13 @@ class OC_MEDIA_COLLECTION{
if($songId!=0){ if($songId!=0){
return $songId; return $songId;
}else{ }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`) if(!isset(self::$queries['addsong'])){
VALUES (NULL , ?, ?, ?, ?,?,?,?,?,0,0)"); $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)); $query->execute(array($name,$artist,$album,$path,$uid,$length,$track,$size));
$songId=OC_DB::insertid(); $songId=OC_DB::insertid();
// self::setLastUpdated(); // self::setLastUpdated();

View 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 //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 $artists=array();
static private $albums=array();//stored as "$artist/$album" to allow albums with the same name from different artists 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 * scan a folder for music
@ -37,7 +38,7 @@ class OC_MEDIA_SCANNER{
* @return int the number of songs found * @return int the number of songs found
*/ */
public static function scanFolder($path){ public static function scanFolder($path){
OC_DB::beginTransaction(); // OC_DB::beginTransaction();
if (OC_Filesystem::is_dir($path)) { if (OC_Filesystem::is_dir($path)) {
$songs=0; $songs=0;
if ($dh = OC_Filesystem::opendir($path)) { if ($dh = OC_Filesystem::opendir($path)) {
@ -47,7 +48,8 @@ class OC_MEDIA_SCANNER{
if(OC_Filesystem::is_dir($file)){ if(OC_Filesystem::is_dir($file)){
$songs+=self::scanFolder($file); $songs+=self::scanFolder($file);
}elseif(OC_Filesystem::is_file($file)){ }elseif(OC_Filesystem::is_file($file)){
if(self::scanFile($file)){ $data=self::scanFile($file);
if($data){
$songs++; $songs++;
} }
} }
@ -60,7 +62,7 @@ class OC_MEDIA_SCANNER{
}else{ }else{
$songs=0; $songs=0;
} }
OC_DB::commit(); // OC_DB::commit();
return $songs; return $songs;
} }
@ -70,45 +72,28 @@ class OC_MEDIA_SCANNER{
* @return boolean * @return boolean
*/ */
public static function scanFile($path){ public static function scanFile($path){
if(is_null(self::$useMp3Info)){
self::$useMp3Info=OC_Helper::canExecute("mp3info");
}
$file=OC_Filesystem::getLocalFile($path); $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(); $output=array();
$size=filesize($file); $size=filesize($file);
$length=0; exec('mp3info -p "%a\n%l\n%t\n%n\n%S" "'.$file.'"',$output);
$title='unknown'; if(count($output)>4){
$album='unknown'; $artist=$output[0];
$artist='unknown'; $album=$output[1];
$track=0; $title=$output[2];
exec('id3info "'.$file.'"',$output); $track=$output[3];
$data=array(); $length=$output[4];
foreach($output as $line) { }else{
switch(substr($line,0,3)){ return; //invalid mp3 file
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;
}
} }
$length=exec('mp3info -p "%S" "'.$file.'"');
}else{ }else{
$mimetype=OC_Filesystem::getMimeType($path);
if(substr($mimetype,0,4)!=='audio'){
return;
}
if(!self::$getID3){ if(!self::$getID3){
self::$getID3=@new getID3(); self::$getID3=@new getID3();
} }

View File

@ -367,6 +367,7 @@ class OC_DB {
* @param string $savePoint (optional) name of the savepoint to set * @param string $savePoint (optional) name of the savepoint to set
*/ */
public static function beginTransaction($savePoint=''){ public static function beginTransaction($savePoint=''){
self::connect();
if (!self::$DBConnection->supports('transactions')) { if (!self::$DBConnection->supports('transactions')) {
return false; return false;
} }
@ -385,6 +386,7 @@ class OC_DB {
* @param string $savePoint (optional) name of the savepoint to commit * @param string $savePoint (optional) name of the savepoint to commit
*/ */
public static function commit($savePoint=''){ public static function commit($savePoint=''){
self::connect();
if(!self::$DBConnection->inTransaction()){ if(!self::$DBConnection->inTransaction()){
return false; return false;
} }