Merge pull request #3621 from owncloud/path-mimetype

seperate mimetype guessing from filename
This commit is contained in:
Thomas Müller 2013-06-16 13:14:16 -07:00
commit e013e7adea
2 changed files with 31 additions and 12 deletions

View File

@ -363,6 +363,26 @@ class OC_Helper {
}
}
/**
* Try to guess the mimetype based on filename
*
* @param string $path
* @return string
*/
static public function getFileNameMimeType($path){
if(strpos($path, '.')) {
//try to guess the type by the file extension
if(!self::$mimetypes || self::$mimetypes != include 'mimetypes.list.php') {
self::$mimetypes=include 'mimetypes.list.php';
}
$extension=strtolower(strrchr(basename($path), "."));
$extension=substr($extension, 1);//remove leading .
return (isset(self::$mimetypes[$extension]))?self::$mimetypes[$extension]:'application/octet-stream';
}else{
return 'application/octet-stream';
}
}
/**
* get the mimetype form a local file
* @param string $path
@ -377,17 +397,7 @@ class OC_Helper {
return "httpd/unix-directory";
}
if(strpos($path, '.')) {
//try to guess the type by the file extension
if(!self::$mimetypes || self::$mimetypes != include 'mimetypes.list.php') {
self::$mimetypes=include 'mimetypes.list.php';
}
$extension=strtolower(strrchr(basename($path), "."));
$extension=substr($extension, 1);//remove leading .
$mimeType=(isset(self::$mimetypes[$extension]))?self::$mimetypes[$extension]:'application/octet-stream';
}else{
$mimeType='application/octet-stream';
}
$mimeType = self::getFileNameMimeType($path);
if($mimeType=='application/octet-stream' and function_exists('finfo_open')
and function_exists('finfo_file') and $finfo=finfo_open(FILEINFO_MIME)) {
@ -609,7 +619,7 @@ class OC_Helper {
}
/**
* remove all files in PHP /oc-noclean temp dir
* remove all files in PHP /oc-noclean temp dir
*/
public static function cleanTmpNoClean() {
$tmpDirNoCleanFile=get_temp_dir().'/oc-noclean/';

View File

@ -67,6 +67,15 @@ class Test_Helper extends PHPUnit_Framework_TestCase {
$this->assertEquals($result, $expected);
}
function testGetFileNameMimeType() {
$this->assertEquals('text/plain', OC_Helper::getFileNameMimeType('foo.txt'));
$this->assertEquals('image/png', OC_Helper::getFileNameMimeType('foo.png'));
$this->assertEquals('image/png', OC_Helper::getFileNameMimeType('foo.bar.png'));
$this->assertEquals('application/octet-stream', OC_Helper::getFileNameMimeType('.png'));
$this->assertEquals('application/octet-stream', OC_Helper::getFileNameMimeType('foo'));
$this->assertEquals('application/octet-stream', OC_Helper::getFileNameMimeType(''));
}
function testGetStringMimeType() {
$result = OC_Helper::getStringMimeType("/data/data.tar.gz");
$expected = 'text/plain; charset=us-ascii';