From cf7321915d3913888e03e0624b32296194a91e6e Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 6 Jun 2013 23:51:44 +0200 Subject: [PATCH 1/4] seperate mimetype guessing from filename --- lib/helper.php | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/lib/helper.php b/lib/helper.php index 225e9fd2a9a..c3dc8740ee8 100644 --- a/lib/helper.php +++ b/lib/helper.php @@ -364,19 +364,12 @@ class OC_Helper { } /** - * get the mimetype form a local file - * @param string $path + * Try to guess the mimetype based on filename + * + * @param string $name * @return string - * does NOT work for ownClouds filesystem, use OC_FileSystem::getMimeType instead */ - static function getMimeType($path) { - $isWrapped=(strpos($path, '://')!==false) and (substr($path, 0, 7)=='file://'); - - if (@is_dir($path)) { - // directories are easy - return "httpd/unix-directory"; - } - + static public function getFileNameMimeType($name){ if(strpos($path, '.')) { //try to guess the type by the file extension if(!self::$mimetypes || self::$mimetypes != include 'mimetypes.list.php') { @@ -388,6 +381,23 @@ class OC_Helper { }else{ $mimeType='application/octet-stream'; } + } + + /** + * get the mimetype form a local file + * @param string $path + * @return string + * does NOT work for ownClouds filesystem, use OC_FileSystem::getMimeType instead + */ + static function getMimeType($path) { + $isWrapped=(strpos($path, '://')!==false) and (substr($path, 0, 7)=='file://'); + + if (@is_dir($path)) { + // directories are easy + return "httpd/unix-directory"; + } + + $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/'; From 0470a5ba94d08447c169114387783d87c70c4382 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 7 Jun 2013 00:17:51 +0200 Subject: [PATCH 2/4] fix variable name --- lib/helper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/helper.php b/lib/helper.php index c3dc8740ee8..84ebcf1d558 100644 --- a/lib/helper.php +++ b/lib/helper.php @@ -366,10 +366,10 @@ class OC_Helper { /** * Try to guess the mimetype based on filename * - * @param string $name + * @param string $path * @return string */ - static public function getFileNameMimeType($name){ + 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') { From 85abede9bf91b0bdbc2bbb938dc2cb5ef26fd361 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 7 Jun 2013 00:22:05 +0200 Subject: [PATCH 3/4] actually return result --- lib/helper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/helper.php b/lib/helper.php index 84ebcf1d558..a315c640d1a 100644 --- a/lib/helper.php +++ b/lib/helper.php @@ -377,9 +377,9 @@ class OC_Helper { } $extension=strtolower(strrchr(basename($path), ".")); $extension=substr($extension, 1);//remove leading . - $mimeType=(isset(self::$mimetypes[$extension]))?self::$mimetypes[$extension]:'application/octet-stream'; + return (isset(self::$mimetypes[$extension]))?self::$mimetypes[$extension]:'application/octet-stream'; }else{ - $mimeType='application/octet-stream'; + return 'application/octet-stream'; } } From d42f7b85f3d6ec524d1144a599edf3b67e88b1a1 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sun, 16 Jun 2013 20:19:37 +0200 Subject: [PATCH 4/4] Basic tests for getFileNameMimeType --- tests/lib/helper.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/lib/helper.php b/tests/lib/helper.php index 336e8f8b3c5..6acb0dfaa6b 100644 --- a/tests/lib/helper.php +++ b/tests/lib/helper.php @@ -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';