Move the mimeTypes.php file

The file /framework/utils/mimeTypes.php is used by the framework internally to compare the mime type of a uploaded file (either by the extension or against the result from Finfo), and personally, i am including it in models for additional checks.

However, this file doesn’t have all the needed extensions, and when we add a new extension => mime/type we need to keep track of this so that when upgrading this won’t break, therefore i purpose to move it in the config directory so we can add file extensions to it more easily and to not worry about it when upgrading.

Also, right now, the file will return a key=>value pairs array where key is the file extension and the value is it’s mime type. This is a problem because a file can have multiple valid mime types, for example the zip files or xls files or powerpoint files etc.

The idea is that we should be able to pass an array of mime types to the file extension key of the returned array, like this :




'aif'	=>	'audio/x-aiff',

[...]

'xls'	=>	array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'),

'ppt'	=>	array('application/powerpoint', 'application/vnd.ms-powerpoint'),

'tgz'	=>	array('application/x-tar', 'application/x-gzip-compressed'),

'zip'	=>      array('application/x-zip', 'application/zip', 'application/x-zip-compressed'),

'jpeg'	=>	array('image/jpeg', 'image/pjpeg'),

'jpg'	=>	array('image/jpeg', 'image/pjpeg'),

'jpe'	=>	array('image/jpeg', 'image/pjpeg'),

'png'	=>	array('image/png',  'image/x-png'),



Assuming that the mimeTypes.php will not ‘shrink’ after updates, one might use a solution as follows:

/protected/config/mimeTypes.php

[PHP]

$mimeTypes = require Yii::getPathOfAlias(‘system.utils’) . DIRECTORY_SEPARATOR . ‘mimeTypes.php’;

return array_merge_recursive($mimeTypes, array(

'xls'   =>      array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'),


'ppt'   =>      array('application/powerpoint', 'application/vnd.ms-powerpoint'),


'tgz'   =>      array('application/x-tar', 'application/x-gzip-compressed'),


'zip'   =>      array('application/x-zip', 'application/zip', 'application/x-zip-compressed'),


'jpeg'  =>      array('image/jpeg', 'image/pjpeg'),


'jpg'   =>      array('image/jpeg', 'image/pjpeg'),


'jpe'   =>      array('image/jpeg', 'image/pjpeg'),


'png'   =>      array('image/png',  'image/x-png'),

));

?>

[/PHP]

This avoids redundancy and also makes the explicit changes/extensions of you and your team clear.

@Coksnuss - that’s out of discussion because is not only about adding mime types to the file, but also changing the logic when checking the mime type of a file (check against an array, not only a string)