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);
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':

View 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();

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
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();
}

View 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;
}