Skip to content

Commit

Permalink
generated mimeType detection file from apache http mime.types file
Browse files Browse the repository at this point in the history
MimeTypeController generates a map of file extensions to MIME types

It uses `mime.types` file from apache http located under
http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=markup

This file has been placed in the public domain for unlimited
redistribution,
so we can use it and ship it with Yii.

see also yiisoft/yii#2851 and yiisoft/yii#2857
  • Loading branch information
cebe committed May 30, 2014
1 parent 873b2b0 commit e5713c8
Show file tree
Hide file tree
Showing 2 changed files with 947 additions and 71 deletions.
72 changes: 72 additions & 0 deletions build/controllers/MimeTypeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

namespace yii\build\controllers;

use Yii;
use yii\console\Controller;
use yii\helpers\VarDumper;

/**
* MimeTypeController generates a map of file extensions to MIME types
*
* It uses `mime.types` file from apache http located under
* http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=markup
*
* This file has been placed in the public domain for unlimited redistribution,
* so we can use it and ship it with Yii.
*
* @author Carsten Brandt <[email protected]>
* @since 2.0
*/
class MimeTypeController extends Controller
{
/**
* @param string $outFile the file to update. Defaults to @yii/helpers/mimeTypes.php
*/
public function actionIndex($outFile = null)
{
if ($outFile === null) {
$outFile = Yii::getAlias('@yii/helpers/mimeTypes.php');
}
if ($content = file_get_contents('http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=co')) {
$mimeMap = [];
foreach(explode("\n", $content) as $line) {
$line = trim($line);
if (empty($line) || $line[0] == '#') { // skip comments and empty lines
continue;
}
$parts = preg_split('/\s+/', $line);
$mime = array_shift($parts);
foreach($parts as $ext) {
if (!empty($ext)) {
$mimeMap[$ext] = $mime;
}
}
}
ksort($mimeMap);
$array = VarDumper::export($mimeMap);
$content = <<<EOD
<?php
/**
* MIME types.
*
* This file contains most commonly used MIME types
* according to file extension names.
* Its content is generated from the apache http mime.types file.
* http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=markup
* This file has been placed in the public domain for unlimited redistribution.
*/
return $array;
EOD;
file_put_contents($outFile, $content);
} else {
$this->stderr("Failed to download mime.types file from apache SVN.\n");
}
}
}
Loading

0 comments on commit e5713c8

Please sign in to comment.