Add MIME class

This commit is contained in:
Kijin Sung 2016-03-17 15:45:31 +09:00
parent 25cf1856a6
commit a9d6cf598d
3 changed files with 175 additions and 75 deletions

View file

@ -460,81 +460,7 @@ class Mail
*/
function returnMIMEType($filename)
{
preg_match("|\.([a-z0-9]{2,4})$|i", $filename, $fileSuffix);
switch(strtolower($fileSuffix[1]))
{
case "js" :
return "application/x-javascript";
case "json" :
return "application/json";
case "jpg" :
case "jpeg" :
case "jpe" :
return "image/jpg";
case "png" :
case "gif" :
case "bmp" :
case "tiff" :
return "image/" . strtolower($fileSuffix[1]);
case "css" :
return "text/css";
case "xml" :
return "application/xml";
case "doc" :
case "docx" :
return "application/msword";
case "xls" :
case "xlt" :
case "xlm" :
case "xld" :
case "xla" :
case "xlc" :
case "xlw" :
case "xll" :
return "application/vnd.ms-excel";
case "ppt" :
case "pps" :
return "application/vnd.ms-powerpoint";
case "rtf" :
return "application/rtf";
case "pdf" :
return "application/pdf";
case "html" :
case "htm" :
case "php" :
return "text/html";
case "txt" :
return "text/plain";
case "mpeg" :
case "mpg" :
case "mpe" :
return "video/mpeg";
case "mp3" :
return "audio/mpeg3";
case "wav" :
return "audio/wav";
case "aiff" :
case "aif" :
return "audio/aiff";
case "avi" :
return "video/msvideo";
case "wmv" :
return "video/x-ms-wmv";
case "mov" :
return "video/quicktime";
case "zip" :
return "application/zip";
case "tar" :
return "application/x-tar";
case "swf" :
return "application/x-shockwave-flash";
default :
if(function_exists("mime_content_type"))
{
$fileSuffix = mime_content_type($filename);
}
return "unknown/" . trim($fileSuffix[0], ".");
}
return Rhymix\Framework\MIME::getTypeByFilename($filename);
}
}

152
common/framework/mime.php Normal file
View file

@ -0,0 +1,152 @@
<?php
namespace Rhymix\Framework;
/**
* The MIME class.
*/
class MIME
{
/**
* Get the MIME type for the given extension.
*
* @param string $extension
* @return string
*/
public static function getTypeByExtension($extension)
{
$extension = strtolower($extension);
return array_key_exists($extension, self::$_types) ? self::$_types[$extension] : self::$_default;
}
/**
* Get the MIME type for the given filename.
*
* @param string $filename
* @return string
*/
public static function getTypeByFilename($filename)
{
$extension = strrchr($filename, '.');
if ($extension === false) return self::$_default;
$extension = strtolower(substr($extension, 1));
return array_key_exists($extension, self::$_types) ? self::$_types[$extension] : self::$_default;
}
/**
* Get the most common extension for the given MIME type.
*
* @param string $type
* @return string|false
*/
public static function getExtensionByType($type)
{
foreach (self::$_types as $extension => $mime)
{
if (!strncasecmp($type, $mime, strlen($type))) return $extension;
}
return false;
}
/**
* The default MIME type for unknown extensions.
*/
protected static $_default = 'application/octet-stream';
/**
* The list of known MIME types.
*/
protected static $_types = array(
// Text-based document formats.
'html' => 'text/html',
'htm' => 'text/html',
'shtml' => 'text/html',
'txt' => 'text/plain',
'text' => 'text/plain',
'log' => 'text/plain',
'rtf' => 'text/rtf',
'xml' => 'text/xml',
'xsl' => 'text/xml',
'css' => 'text/css',
'csv' => 'text/csv',
// Binary document formats.
'doc' => 'application/msword',
'dot' => 'application/msword',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
'odp' => 'application/vnd.oasis.opendocument.presentation',
'odg' => 'application/vnd.oasis.opendocument.graphics',
'odb' => 'application/vnd.oasis.opendocument.database',
'pdf' => 'application/pdf',
// Images.
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'png' => 'image/png',
'svg' => 'image/svg+xml',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'ico' => 'image/vnd.microsoft.icon',
// Audio.
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'mpga' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'aif' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'ra' => 'audio/x-realaudio',
'wav' => 'audio/x-wav',
'ogg' => 'audio/ogg',
// Video.
'avi' => 'video/x-msvideo',
'flv' => 'video/x-flv',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'mp4' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'movie' => 'video/x-sgi-movie',
'rv' => 'video/vnd.rn-realvideo',
'dvi' => 'application/x-dvi',
// Other multimedia file formats.
'psd' => 'application/x-photoshop',
'swf' => 'application/x-shockwave-flash',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'mif' => 'application/vnd.mif',
'xul' => 'application/vnd.mozilla.xul+xml',
// Source code formats.
'phps' => 'application/x-httpd-php-source',
'js' => 'application/x-javascript',
// Archives.
'bz2' => 'application/x-bzip',
'gz' => 'application/x-gzip',
'tar' => 'application/x-tar',
'tgz' => 'application/x-tar',
'gtar' => 'application/x-gtar',
'rar' => 'application/x-rar-compressed',
'zip' => 'application/x-zip',
// RFC822 email message.
'eml' => 'message/rfc822',
);
}

View file

@ -0,0 +1,22 @@
<?php
class MIMETest extends \Codeception\TestCase\Test
{
public function testMIME()
{
$this->assertEquals('audio/ogg', Rhymix\Framework\MIME::getTypeByExtension('ogg'));
$this->assertEquals('image/gif', Rhymix\Framework\MIME::getTypeByExtension('gif'));
$this->assertEquals('text/html', Rhymix\Framework\MIME::getTypeByExtension('htm'));
$this->assertEquals('application/msword', Rhymix\Framework\MIME::getTypeByFilename('attachment.doc'));
$this->assertEquals('application/pdf', Rhymix\Framework\MIME::getTypeByFilename('라이믹스.pdf'));
$this->assertEquals('application/postscript', Rhymix\Framework\MIME::getTypeByFilename('MyGraphics.v2.eps'));
$this->assertEquals('application/vnd.ms-excel', Mail::returnMIMEType('MySpreadsheet.xls'));
$this->assertEquals('application/octet-stream', Mail::returnMIMEType('Untitled File'));
$this->assertEquals('odt', Rhymix\Framework\MIME::getExtensionByType('application/vnd.oasis.opendocument.text'));
$this->assertEquals('jpg', Rhymix\Framework\MIME::getExtensionByType('image/jpeg'));
$this->assertEquals('mpeg', Rhymix\Framework\MIME::getExtensionByType('video/mpeg'));
$this->assertFalse(Rhymix\Framework\MIME::getExtensionByType('application/octet-stream'));
}
}