Commit b8354224 by Qiang Xue

Fixes #4897: Added `yii\helpers\FileHelper::mimeMagicFile

parent 353cf1b3
...@@ -202,6 +202,7 @@ Yii Framework 2 Change Log ...@@ -202,6 +202,7 @@ Yii Framework 2 Change Log
- Enh #4062: Added 'caseSensitive' option to `yii\helpers\BaseFileHelper::findFiles()` (klimov-paul) - Enh #4062: Added 'caseSensitive' option to `yii\helpers\BaseFileHelper::findFiles()` (klimov-paul)
- Enh #4691: Encoding on `ActiveForm` and `ActiveField` validation errors is now configurable (Alex-Code) - Enh #4691: Encoding on `ActiveForm` and `ActiveField` validation errors is now configurable (Alex-Code)
- Enh #4740: Added `yii\web\Session::addFlash()` (restyler) - Enh #4740: Added `yii\web\Session::addFlash()` (restyler)
- Enh #4897: Added `yii\helpers\FileHelper::mimeMagicFile` (qiangxue)
- Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue) - Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue)
- Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue) - Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue)
- Enh: Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules (qiangxue) - Enh: Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules (qiangxue)
......
...@@ -28,6 +28,10 @@ class BaseFileHelper ...@@ -28,6 +28,10 @@ class BaseFileHelper
const PATTERN_NEGATIVE = 16; const PATTERN_NEGATIVE = 16;
const PATTERN_CASE_INSENSITIVE = 32; const PATTERN_CASE_INSENSITIVE = 32;
/**
* @var string the path (or alias) of a PHP file containing MIME type information.
*/
public static $mimeMagicFile = '@yii/helpers/mimeTypes.php';
/** /**
* Normalizes a file/directory path. * Normalizes a file/directory path.
...@@ -112,11 +116,13 @@ class BaseFileHelper ...@@ -112,11 +116,13 @@ class BaseFileHelper
/** /**
* Determines the MIME type of the specified file. * Determines the MIME type of the specified file.
* This method will first try to determine the MIME type based on * This method will first try to determine the MIME type based on
* [finfo_open](http://php.net/manual/en/function.finfo-open.php). If this doesn't work, it will * [finfo_open](http://php.net/manual/en/function.finfo-open.php). If the `fileinfo` extension is not installed,
* fall back to [[getMimeTypeByExtension()]]. * it will fall back to [[getMimeTypeByExtension()]] when `$checkExtension` is true.
* @param string $file the file name. * @param string $file the file name.
* @param string $magicFile name of the optional magic database file, usually something like `/path/to/magic.mime`. * @param string $magicFile name of the optional magic database file (or alias), usually something like `/path/to/magic.mime`.
* This will be passed as the second parameter to [finfo_open](http://php.net/manual/en/function.finfo-open.php). * This will be passed as the second parameter to [finfo_open()](http://php.net/manual/en/function.finfo-open.php)
* when the `fileinfo` extension is installed. If the MIME type is being determined based via [[getMimeTypeByExtension()]]
* and this is null, it will use the file specified by [[mimeMagicFile]].
* @param boolean $checkExtension whether to use the file extension to determine the MIME type in case * @param boolean $checkExtension whether to use the file extension to determine the MIME type in case
* `finfo_open()` cannot determine it. * `finfo_open()` cannot determine it.
* @return string the MIME type (e.g. `text/plain`). Null is returned if the MIME type cannot be determined. * @return string the MIME type (e.g. `text/plain`). Null is returned if the MIME type cannot be determined.
...@@ -124,6 +130,9 @@ class BaseFileHelper ...@@ -124,6 +130,9 @@ class BaseFileHelper
*/ */
public static function getMimeType($file, $magicFile = null, $checkExtension = true) public static function getMimeType($file, $magicFile = null, $checkExtension = true)
{ {
if ($magicFile !== null) {
$magicFile = Yii::getAlias($magicFile);
}
if (!extension_loaded('fileinfo')) { if (!extension_loaded('fileinfo')) {
if ($checkExtension) { if ($checkExtension) {
return static::getMimeTypeByExtension($file, $magicFile); return static::getMimeTypeByExtension($file, $magicFile);
...@@ -149,8 +158,8 @@ class BaseFileHelper ...@@ -149,8 +158,8 @@ class BaseFileHelper
* Determines the MIME type based on the extension name of the specified file. * Determines the MIME type based on the extension name of the specified file.
* This method will use a local map between extension names and MIME types. * This method will use a local map between extension names and MIME types.
* @param string $file the file name. * @param string $file the file name.
* @param string $magicFile the path of the file that contains all available MIME type information. * @param string $magicFile the path (or alias) of the file that contains all available MIME type information.
* If this is not set, the default file aliased by `@yii/helpers/mimeTypes.php` will be used. * If this is not set, the file specified by [[mimeMagicFile]] will be used.
* @return string the MIME type. Null is returned if the MIME type cannot be determined. * @return string the MIME type. Null is returned if the MIME type cannot be determined.
*/ */
public static function getMimeTypeByExtension($file, $magicFile = null) public static function getMimeTypeByExtension($file, $magicFile = null)
...@@ -171,8 +180,8 @@ class BaseFileHelper ...@@ -171,8 +180,8 @@ class BaseFileHelper
* Determines the extensions by given MIME type. * Determines the extensions by given MIME type.
* This method will use a local map between extension names and MIME types. * This method will use a local map between extension names and MIME types.
* @param string $mimeType file MIME type. * @param string $mimeType file MIME type.
* @param string $magicFile the path of the file that contains all available MIME type information. * @param string $magicFile the path (or alias) of the file that contains all available MIME type information.
* If this is not set, the default file aliased by `@yii/helpers/mimeTypes.php` will be used. * If this is not set, the file specified by [[mimeMagicFile]] will be used.
* @return array the extensions corresponding to the specified MIME type * @return array the extensions corresponding to the specified MIME type
*/ */
public static function getExtensionsByMimeType($mimeType, $magicFile = null) public static function getExtensionsByMimeType($mimeType, $magicFile = null)
...@@ -185,15 +194,16 @@ class BaseFileHelper ...@@ -185,15 +194,16 @@ class BaseFileHelper
/** /**
* Loads MIME types from the specified file. * Loads MIME types from the specified file.
* @param string $magicFile the file that contains MIME type information. * @param string $magicFile the path (or alias) of the file that contains all available MIME type information.
* If null, the file `@yii/helpers/mimeTypes.php` will be used. * If this is not set, the file specified by [[mimeMagicFile]] will be used.
* @return array the mapping from file extensions to MIME types * @return array the mapping from file extensions to MIME types
*/ */
protected static function loadMimeTypes($magicFile) protected static function loadMimeTypes($magicFile)
{ {
if ($magicFile === null) { if ($magicFile === null) {
$magicFile = __DIR__ . '/mimeTypes.php'; $magicFile = static::$mimeMagicFile;
} }
$magicFile = Yii::getAlias($magicFile);
if (!isset(self::$_mimeTypes[$magicFile])) { if (!isset(self::$_mimeTypes[$magicFile])) {
self::$_mimeTypes[$magicFile] = require($magicFile); self::$_mimeTypes[$magicFile] = require($magicFile);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment