diff --git a/framework/utils/CFileHelper.php b/framework/utils/CFileHelper.php index 7250e2c36..0514a34b6 100644 --- a/framework/utils/CFileHelper.php +++ b/framework/utils/CFileHelper.php @@ -248,23 +248,18 @@ class CFileHelper */ public static function getMimeTypeByExtension($file,$magicFile=null) { - static $extensions, $customExtensions; + static $extensions, $customExtensions=array(); if($magicFile===null && $extensions===null) $extensions=require(Yii::getPathOfAlias('system.utils.mimeTypes').'.php'); - else if($magicFile!==null) - { - if(is_array($magicFile)) - $customExtensions=$magicFile; - else if($customExtensions===null) - $customExtensions=require($magicFile); - } + else if($magicFile!==null && !isset($customExtensions[$magicFile])) + $customExtensions[$magicFile]=require($magicFile); if(($ext=pathinfo($file, PATHINFO_EXTENSION))!=='') { $ext=strtolower($ext); - if(isset($extensions[$ext])) + if($magicFile===null && isset($extensions[$ext])) return $extensions[$ext]; - else if(isset($customExtensions[$ext])) - return $customExtensions[$ext]; + else if($magicFile!==null && isset($customExtensions[$magicFile][$ext])) + return $customExtensions[$magicFile][$ext]; } return null; } diff --git a/tests/framework/utils/CFileHelperTest.php b/tests/framework/utils/CFileHelperTest.php new file mode 100644 index 000000000..e67dc904c --- /dev/null +++ b/tests/framework/utils/CFileHelperTest.php @@ -0,0 +1,26 @@ +assertNull(CFileHelper::getMimeTypeByExtension('test.txa')); + $this->assertNull(CFileHelper::getMimeTypeByExtension('test.txb')); + $this->assertEquals('text/plain',CFileHelper::getMimeTypeByExtension('test.txt')); + + $path=dirname(__FILE__).DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'mimeTypes1.php'; + $this->assertEquals('application/json',CFileHelper::getMimeTypeByExtension('test.txa',$path)); + $this->assertEquals('another/mime',CFileHelper::getMimeTypeByExtension('test.txb',$path)); + $this->assertNull(CFileHelper::getMimeTypeByExtension('test.txt',$path)); + + $path=dirname(__FILE__).DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'mimeTypes2.php'; + $this->assertNull(CFileHelper::getMimeTypeByExtension('test.txa',$path)); + $this->assertEquals('another/mime2',CFileHelper::getMimeTypeByExtension('test.txb',$path)); + $this->assertEquals('text/plain',CFileHelper::getMimeTypeByExtension('test.txt',$path)); + } + } +} diff --git a/tests/framework/utils/data/mimeTypes1.php b/tests/framework/utils/data/mimeTypes1.php new file mode 100644 index 000000000..f6752d17a --- /dev/null +++ b/tests/framework/utils/data/mimeTypes1.php @@ -0,0 +1,6 @@ +'application/json', + 'txb'=>'another/mime', +); diff --git a/tests/framework/utils/data/mimeTypes2.php b/tests/framework/utils/data/mimeTypes2.php new file mode 100644 index 000000000..2fc0de0ed --- /dev/null +++ b/tests/framework/utils/data/mimeTypes2.php @@ -0,0 +1,6 @@ +'text/plain', + 'txb'=>'another/mime2', +);