2015-02-17 18:17:49 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class FileDataHelper {
|
|
|
|
|
2015-07-07 21:48:22 +02:00
|
|
|
public static function getAudioMimeTypeArray() {
|
|
|
|
return array(
|
|
|
|
"audio/ogg" => "ogg",
|
|
|
|
"application/ogg" => "ogg",
|
|
|
|
"audio/vorbis" => "ogg",
|
|
|
|
"audio/mp3" => "mp3",
|
|
|
|
"audio/mpeg" => "mp3",
|
|
|
|
"audio/mpeg3" => "mp3",
|
2015-07-07 22:15:35 +02:00
|
|
|
"audio/aac" => "aac",
|
|
|
|
"audio/aacp" => "aac",
|
2015-07-07 21:48:22 +02:00
|
|
|
"audio/mp4" => "mp4",
|
|
|
|
"audio/x-flac" => "flac",
|
|
|
|
"audio/wav" => "wav",
|
|
|
|
"audio/x-wav" => "wav",
|
|
|
|
"audio/mp2" => "mp2",
|
|
|
|
"audio/mp1" => "mp1",
|
|
|
|
"audio/x-ms-wma" => "wma",
|
|
|
|
"audio/basic" => "au",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-02-17 18:17:49 +01:00
|
|
|
/**
|
|
|
|
* We want to throw out invalid data and process the upload successfully
|
|
|
|
* at all costs, so check the data and sanitize it if necessary
|
|
|
|
* @param array $data array containing new file metadata
|
|
|
|
*/
|
2015-02-18 22:29:08 +01:00
|
|
|
public static function sanitizeData(&$data)
|
|
|
|
{
|
|
|
|
if (array_key_exists("track_number", $data)) {
|
|
|
|
// If the track number isn't numeric, this will return 0
|
|
|
|
$data["track_number"] = intval($data["track_number"]);
|
|
|
|
}
|
|
|
|
if (array_key_exists("year", $data)) {
|
|
|
|
// If the track number isn't numeric, this will return 0
|
|
|
|
$data["year"] = intval($data["year"]);
|
|
|
|
}
|
2015-02-19 15:31:23 +01:00
|
|
|
if (array_key_exists("bpm", $data)) {
|
|
|
|
//Some BPM tags are silly and include the word "BPM". Let's strip that...
|
2015-02-20 20:01:29 +01:00
|
|
|
$data["bpm"] = str_ireplace("BPM", "", $data["bpm"]);
|
2015-02-19 15:31:23 +01:00
|
|
|
// This will convert floats to ints too.
|
2015-02-20 20:01:29 +01:00
|
|
|
$data["bpm"] = intval($data["bpm"]);
|
2015-02-19 15:31:23 +01:00
|
|
|
}
|
2015-02-17 18:17:49 +01:00
|
|
|
}
|
2015-07-07 19:54:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a suitable extension for the given file
|
|
|
|
*
|
|
|
|
* @param string $mime
|
|
|
|
*
|
2015-07-07 21:48:22 +02:00
|
|
|
* @return string file extension with(!) a dot (for convenience)
|
2015-07-07 19:54:19 +02:00
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public static function getFileExtensionFromMime($mime)
|
|
|
|
{
|
2015-07-07 21:48:22 +02:00
|
|
|
$mime = trim(strtolower($mime));
|
|
|
|
try {
|
|
|
|
return ('.' . static::getAudioMimeTypeArray()[$mime]);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
throw new Exception("Unknown file type: $mime");
|
2015-07-07 19:54:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 18:17:49 +01:00
|
|
|
}
|