From 1a8b605c17951b281ef6d93cb319aff83fa08edd Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Sat, 18 Apr 2009 09:53:36 +0000 Subject: [PATCH] method to create user friendly label for a mime type, eg. "PDF file (application/pdf)" --- etemplate/inc/class.vfs_widget.inc.php | 10 ++-------- phpgwapi/inc/class.mime_magic.inc.php | 24 +++++++++++++++++++++++- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/etemplate/inc/class.vfs_widget.inc.php b/etemplate/inc/class.vfs_widget.inc.php index f5d71c3506..cbcce7242f 100644 --- a/etemplate/inc/class.vfs_widget.inc.php +++ b/etemplate/inc/class.vfs_widget.inc.php @@ -212,14 +212,8 @@ class vfs_widget } //error_log(__METHOD__."() type=vfs-mime: value=".array2string($value).": mime=$mime, path=$path"); $cell['type'] = 'image'; - if ($mime == egw_vfs::DIR_MIME_TYPE) - { - $cell['label'] = lang('Directory'); - } - else - { - $cell['label'] = lang('%1 file',strtoupper(mime_magic::mime2ext($mime))).' ('.$mime.')'; - } + $cell['label'] = mime_magic::mime2label($mime); + list($mime_main,$mime_sub) = explode('/',$mime); if ($mime_main == 'egw') { diff --git a/phpgwapi/inc/class.mime_magic.inc.php b/phpgwapi/inc/class.mime_magic.inc.php index b94043ed8a..3f4a937310 100644 --- a/phpgwapi/inc/class.mime_magic.inc.php +++ b/phpgwapi/inc/class.mime_magic.inc.php @@ -26,6 +26,27 @@ */ class mime_magic { + /** + * Get a user friendly label for a mime type: e.g. "PDF file (application/pdf)" + * + * @param string $mime + * @return string + */ + public function mime2label($mime) + { + $mime = strtolower($mime); + + if ($mime == egw_vfs::DIR_MIME_TYPE) + { + return lang('Directory'); + } + elseif (!($ext = self::mime2ext($mime))) + { + return $mime; + } + return lang('%1 file',strtoupper($ext)).' ('.$mime.')'; + } + /** * Convert a file extension to a MIME type * @@ -92,6 +113,7 @@ class mime_magic */ public static function mime2ext($type) { + $type = strtolower($type); $key = array_search($type, self::$mime_extension_map); if (empty($type) || $key === false) { @@ -294,7 +316,7 @@ class mime_magic 'odt' => 'application/vnd.oasis.opendocument.text', 'odp' => 'application/vnd.oasis.opendocument.presentation', 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', - 'patch' => 'text/diff', + 'patch' => 'text/x-diff', 'pbm' => 'image/x-portable-bitmap', 'pdb' => 'chemical/x-pdb', 'pdf' => 'application/pdf',