File class no longer depends on MIME

+ minor tweaks to MediaFile
This commit is contained in:
Mikael Nordfeldth 2014-03-08 03:51:47 +01:00
parent 6faed0e451
commit 639f1a01e0
3 changed files with 23 additions and 15 deletions

View File

@ -265,20 +265,13 @@ class File extends Managed_DataObject
static function filename($profile, $basename, $mimetype) static function filename($profile, $basename, $mimetype)
{ {
require_once 'MIME/Type/Extension.php'; try {
$ext = common_supported_mime_to_ext($mimetype);
// We have to temporarily disable auto handling of PEAR errors... } catch (Exception $e) {
PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN); // We don't support this mimetype, but let's guess the extension
$ext = substr(strrchr($mimetype, '/'), 1);
$mte = new MIME_Type_Extension();
$ext = $mte->getExtension($mimetype);
if (PEAR::isError($ext)) {
$ext = strtolower(preg_replace('/\W/', '', $mimetype));
} }
// Restore error handling.
PEAR::staticPopErrorHandling();
$nickname = $profile->nickname; $nickname = $profile->nickname;
$datestamp = strftime('%Y%m%dT%H%M%S', time()); $datestamp = strftime('%Y%m%dT%H%M%S', time());
$random = strtolower(common_confirmation_code(32)); $random = strtolower(common_confirmation_code(32));

View File

@ -298,9 +298,11 @@ class MediaFile
// If we didn't match, or it is an unclear match // If we didn't match, or it is an unclear match
if ($originalFilename && (!$mimetype || in_array($mimetype, $unclearTypes))) { if ($originalFilename && (!$mimetype || in_array($mimetype, $unclearTypes))) {
$type = common_supported_ext_to_mime($originalFilename); try {
if (!empty($type)) { $type = common_supported_ext_to_mime($originalFilename);
return $type; return $type;
} catch (Exception $e) {
// Extension not found, so $mimetype is our best guess
} }
} }

View File

@ -1812,7 +1812,20 @@ function common_supported_ext_to_mime($fileext)
} }
} }
return false; throw new ServerException('Unsupported file extension');
}
// Match by our supported mime types
function common_supported_mime_to_ext($mimetype)
{
$supported = common_config('attachments', 'supported');
foreach($supported as $type => $ext) {
if ($mimetype === $type) {
return $ext;
}
}
throw new ServerException('Unsupported MIME type');
} }
// The MIME "media" is the part before the slash (video in video/webm) // The MIME "media" is the part before the slash (video in video/webm)