mirror of
https://github.com/getgrav/grav.git
synced 2026-03-14 08:31:09 +01:00
Removed duplicate method Grav::mime() and used existing (renamed) Utils::getMimeByExtension(), also added Utils::getExtensionByMime()
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
|
||||
1. [](#new)
|
||||
* Added ability for Page to override the output format (`html`, `xml`, etc..) [#1067](https://github.com/getgrav/grav/issues/1067)
|
||||
* Added `Utils::getExtensionByMime()` and cleaned up `Utils::getMimeByExtension` + tests
|
||||
1. [](#improved)
|
||||
* Add `batch()` function to Page Collection class
|
||||
* Added new `cache.redis.socket` setting that allow to pass a UNIX socket as redis server
|
||||
|
||||
@@ -208,44 +208,6 @@ class Grav extends Container
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns mime type for the file format.
|
||||
*
|
||||
* @param string $format
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function mime($format)
|
||||
{
|
||||
// look for some standard types
|
||||
switch ($format) {
|
||||
case null:
|
||||
return 'text/html';
|
||||
case 'json':
|
||||
return 'application/json';
|
||||
case 'html':
|
||||
return 'text/html';
|
||||
case 'atom':
|
||||
return 'application/atom+xml';
|
||||
case 'rss':
|
||||
return 'application/rss+xml';
|
||||
case 'xml':
|
||||
return 'application/xml';
|
||||
}
|
||||
|
||||
// Try finding mime type from media
|
||||
$media_types = $this['config']->get('media.types');
|
||||
if (key_exists($format, $media_types)) {
|
||||
$type = $media_types[$format];
|
||||
if (isset($type['mime'])) {
|
||||
return $type['mime'];
|
||||
}
|
||||
}
|
||||
|
||||
// Can't find the mime type, send as HTML
|
||||
return 'text/html';
|
||||
}
|
||||
|
||||
/**
|
||||
* Set response header.
|
||||
*/
|
||||
@@ -256,7 +218,7 @@ class Grav extends Container
|
||||
|
||||
$format = $page->templateFormat();
|
||||
|
||||
header('Content-type: ' . $this->mime($format));
|
||||
header('Content-type: ' . Utils::getMimeByExtension($format, 'text/html'));
|
||||
|
||||
// Calculate Expires Headers if set to > 0
|
||||
$expires = $page->expires();
|
||||
|
||||
@@ -235,7 +235,7 @@ abstract class Utils
|
||||
Grav::instance()->fireEvent('onBeforeDownload', new Event(['file' => $file]));
|
||||
|
||||
$file_parts = pathinfo($file);
|
||||
$mimetype = Utils::getMimeType($file_parts['extension']);
|
||||
$mimetype = Utils::getMimeByExtension($file_parts['extension']);
|
||||
$size = filesize($file); // File size
|
||||
|
||||
// clean all buffers
|
||||
@@ -321,22 +321,84 @@ abstract class Utils
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the mimetype based on filename
|
||||
* Return the mimetype based on filename extension
|
||||
*
|
||||
* @param string $extension Extension of file (eg "txt")
|
||||
* @param string $default
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getMimeType($extension)
|
||||
public static function getMimeByExtension($extension, $default = 'application/octet-stream')
|
||||
{
|
||||
$extension = strtolower($extension);
|
||||
$config = Grav::instance()['config']->get('media.types');
|
||||
|
||||
if (isset($config[$extension])) {
|
||||
return $config[$extension]['mime'];
|
||||
// look for some standard types
|
||||
switch ($extension) {
|
||||
case null:
|
||||
return $default;
|
||||
case 'json':
|
||||
return 'application/json';
|
||||
case 'html':
|
||||
return 'text/html';
|
||||
case 'atom':
|
||||
return 'application/atom+xml';
|
||||
case 'rss':
|
||||
return 'application/rss+xml';
|
||||
case 'xml':
|
||||
return 'application/xml';
|
||||
}
|
||||
|
||||
return 'application/octet-stream';
|
||||
$media_types = Grav::instance()['config']->get('media.types');
|
||||
|
||||
if (isset($media_types[$extension])) {
|
||||
if (isset($media_types[$extension]['mime'])) {
|
||||
return $media_types[$extension]['mime'];
|
||||
}
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the mimetype based on filename extension
|
||||
*
|
||||
* @param string $mime mime type (eg "text/html")
|
||||
* @param string $default default value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getExtensionByMime($mime, $default = 'html')
|
||||
{
|
||||
$mime = strtolower($mime);
|
||||
|
||||
// look for some standard mime types
|
||||
switch ($mime) {
|
||||
case '*/*':
|
||||
case 'text/*':
|
||||
case 'text/html':
|
||||
return 'html';
|
||||
case 'application/json':
|
||||
return 'json';
|
||||
case 'application/atom+xml':
|
||||
return 'atom';
|
||||
case 'application/rss+xml':
|
||||
return 'rss';
|
||||
case 'application/xml':
|
||||
return 'xml';
|
||||
}
|
||||
|
||||
$media_types = Grav::instance()['config']->get('media.types');
|
||||
|
||||
foreach ($media_types as $extension => $type) {
|
||||
if ($extension == 'defaults') {
|
||||
continue;
|
||||
}
|
||||
if (isset($type['mime']) && $type['mime'] == $mime) {
|
||||
return $extension;
|
||||
}
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -152,13 +152,36 @@ class UtilsTest extends \Codeception\TestCase\Test
|
||||
|
||||
}
|
||||
|
||||
public function testGetMimeType()
|
||||
public function testGetMimeByExtension()
|
||||
{
|
||||
$this->assertEquals('application/octet-stream', Utils::getMimeType(''));
|
||||
$this->assertEquals('image/jpeg', Utils::getMimeType('jpg'));
|
||||
$this->assertEquals('image/png', Utils::getMimeType('png'));
|
||||
$this->assertEquals('text/plain', Utils::getMimeType('txt'));
|
||||
$this->assertEquals('application/msword', Utils::getMimeType('doc'));
|
||||
$this->assertEquals('application/octet-stream', Utils::getMimeByExtension(''));
|
||||
$this->assertEquals('text/html', Utils::getMimeByExtension('html'));
|
||||
$this->assertEquals('application/json', Utils::getMimeByExtension('json'));
|
||||
$this->assertEquals('application/atom+xml', Utils::getMimeByExtension('atom'));
|
||||
$this->assertEquals('application/rss+xml', Utils::getMimeByExtension('rss'));
|
||||
$this->assertEquals('image/jpeg', Utils::getMimeByExtension('jpg'));
|
||||
$this->assertEquals('image/png', Utils::getMimeByExtension('png'));
|
||||
$this->assertEquals('text/plain', Utils::getMimeByExtension('txt'));
|
||||
$this->assertEquals('application/msword', Utils::getMimeByExtension('doc'));
|
||||
$this->assertEquals('application/octet-stream', Utils::getMimeByExtension('foo'));
|
||||
$this->assertEquals('foo/bar', Utils::getMimeByExtension('foo', 'foo/bar'));
|
||||
$this->assertEquals('text/html', Utils::getMimeByExtension('foo', 'text/html'));
|
||||
}
|
||||
|
||||
public function testGetExtensionByMime()
|
||||
{
|
||||
$this->assertEquals('html', Utils::getExtensionByMime('*/*'));
|
||||
$this->assertEquals('html', Utils::getExtensionByMime('text/*'));
|
||||
$this->assertEquals('html', Utils::getExtensionByMime('text/html'));
|
||||
$this->assertEquals('json', Utils::getExtensionByMime('application/json'));
|
||||
$this->assertEquals('atom', Utils::getExtensionByMime('application/atom+xml'));
|
||||
$this->assertEquals('rss', Utils::getExtensionByMime('application/rss+xml'));
|
||||
$this->assertEquals('jpg', Utils::getExtensionByMime('image/jpeg'));
|
||||
$this->assertEquals('png', Utils::getExtensionByMime('image/png'));
|
||||
$this->assertEquals('txt', Utils::getExtensionByMime('text/plain'));
|
||||
$this->assertEquals('doc', Utils::getExtensionByMime('application/msword'));
|
||||
$this->assertEquals('html', Utils::getExtensionByMime('foo/bar'));
|
||||
$this->assertEquals('baz', Utils::getExtensionByMime('foo/bar', 'baz'));
|
||||
}
|
||||
|
||||
public function testNormalizePath()
|
||||
|
||||
Reference in New Issue
Block a user