Created a new Metadata class to hold static functions which parse audio

file metadata.  This got rid of the "camp_" prefixes on the function names.
This commit is contained in:
paul.baranowski 2011-03-06 11:05:12 -05:00
parent 1c2ff6c150
commit 95a4694b5a
3 changed files with 255 additions and 271 deletions

View File

@ -124,7 +124,7 @@ class PluploadController extends Zend_Controller_Action
}
}
$metadata = camp_get_audio_metadata($audio_file);
$metadata = Metadata::LoadFromFile($audio_file);
if (PEAR::isError($metadata)) {
die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": ' + $metadata->getMessage() + '}}');

View File

@ -52,6 +52,8 @@ $g_metadata_xml_to_db_mapping = array(
"dc:contributor" => "contributor",
"dc:language" => "language");
class Metadata {
/**
* Track numbers in metadata tags can come in many formats:
* "1 of 20", "1/20", "20/1". This function parses the track
@ -61,7 +63,7 @@ $g_metadata_xml_to_db_mapping = array(
* @param string $p_trackNumber
* @return int
*/
function camp_parse_track_number($p_trackNumber)
public static function ParseTrackNumber($p_trackNumber)
{
$num = trim($p_trackNumber);
if (!is_numeric($num)) {
@ -84,8 +86,7 @@ function camp_parse_track_number($p_trackNumber)
/**
* Add data to the global array $mdata, also sets global variables
* $titleHaveSet and $titleKey.
* Add data to the array $p_mdata.
*
* Converts the given string ($val) into UTF-8.
*
@ -98,7 +99,7 @@ function camp_parse_track_number($p_trackNumber)
* @param string $p_inputEncoding
* Encoding type of the input value.
*/
function camp_add_metadata(&$p_mdata, $p_key, $p_val, $p_inputEncoding='iso-8859-1')
public static function AddToArray(&$p_mdata, $p_key, $p_val, $p_inputEncoding='iso-8859-1')
{
if (!is_null($p_val)) {
$data = $p_val;
@ -143,7 +144,7 @@ function camp_add_metadata(&$p_mdata, $p_key, $p_val, $p_inputEncoding='iso-8859
* to the return array.
* @return array|PEAR_Error
*/
function camp_get_audio_metadata($p_filename, $p_testonly = false)
public static function LoadFromFile($p_filename, $p_testonly = false)
{
$getID3 = new getID3();
$infoFromFile = $getID3->analyze($p_filename);
@ -289,9 +290,9 @@ function camp_get_audio_metadata($p_filename, $p_testonly = false)
// Special case handling for track number
if ($key == "ls:track_num") {
$data = camp_parse_track_number($data);
$data = Metadata::ParseTrackNumber($data);
}
camp_add_metadata($mdata, $key, $data, $enc);
Metadata::AddToArray($mdata, $key, $data, $enc);
if ($key == $titleKey) {
$titleHaveSet = TRUE;
}
@ -304,10 +305,11 @@ function camp_get_audio_metadata($p_filename, $p_testonly = false)
}
if (!$titleHaveSet || trim($mdata[$titleKey]) == '') {
camp_add_metadata($mdata, $titleKey, basename($p_filename));
Metadata::AddToArray($mdata, $titleKey, basename($p_filename));
}
return $mdata;
}
}
/**
@ -316,7 +318,7 @@ function camp_get_audio_metadata($p_filename, $p_testonly = false)
* Airtime file storage support class.<br>
* Represents one virtual file in storage. Virtual file has up to two parts:
* <ul>
* <li>metadata in database - represented by MetaData class</li>
* <li>metadata in database </li>
* <li>binary media data in real file</li>
* </ul>
*
@ -607,7 +609,7 @@ class StoredFile {
if (isset($p_values["metadata"])) {
$metadata = $p_values['metadata'];
} else {
$metadata = camp_get_audio_metadata($p_values["filepath"]);
$metadata = Metadata::LoadFromFile($p_values["filepath"]);
}
$storedFile->name = isset($p_values['filename']) ? $p_values['filename'] : $p_values["filepath"];
@ -906,24 +908,6 @@ class StoredFile {
}
/**
* Analyze file with getid3 module.<br>
* Obtain some metadata stored in media file.<br>
* This method should be used for prefilling metadata input form.
*
* @return array
* hierarchical hasharray with information about media file
*/
public function analyzeFile()
{
if (!$this->exists) {
return FALSE;
}
$ia = camp_get_audio_metadata($this->filepath);
return $ia;
}
/**
* Create instance of StoredFile object and make copy of existing file
*
@ -1511,7 +1495,7 @@ class StoredFile {
*/
public function getMime()
{
$a = $this->analyzeFile();
$a = Metadata::LoadFromFile($this->filepath);
if (PEAR::isError($a)) {
return $a;
}

View File

@ -20,7 +20,7 @@ class StoredFileTest extends PHPUnit_TestCase {
function testGetAudioMetadata() {
$filePath = dirname(__FILE__)."/ex1.mp3";
$metadata = camp_get_audio_metadata($filePath);
$metadata = Metadata::LoadFromFile($filePath);
if (PEAR::isError($metadata)) {
$this->fail($metadata->getMessage());
return;