From 7716d3ad54693b28ad049fddfbf4cddcd108df16 Mon Sep 17 00:00:00 2001 From: Uwe Steinmann Date: Thu, 17 Nov 2022 11:31:12 +0100 Subject: [PATCH] add method mimetype() --- SeedDMS_Core/Core/inc.FileUtils.php | 33 +++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/SeedDMS_Core/Core/inc.FileUtils.php b/SeedDMS_Core/Core/inc.FileUtils.php index c0b0f1b8e..ac09421b0 100644 --- a/SeedDMS_Core/Core/inc.FileUtils.php +++ b/SeedDMS_Core/Core/inc.FileUtils.php @@ -80,8 +80,37 @@ class SeedDMS_Core_File { } /* }}} */ /** - * @param $size - * @param array $sizes + * Return the mimetype of a given file + * + * This method uses finfo to determine the mimetype + * but will correct some mimetypes which are + * not propperly determined or could be more specific, e.g. text/plain + * when it is actually text/markdown. In thoses cases + * the file extension will be taken into account. + * + * @param string $filename name of file on disc + * @return string mimetype + */ + static function mimetype($filename) { /* {{{ */ + $finfo = finfo_open(FILEINFO_MIME_TYPE); + $mimetype = finfo_file($finfo, $filename); + + switch($mimetype) { + case 'application/octet-stream': + case 'text/plain': + $lastDotIndex = strrpos($filename, "."); + if($lastDotIndex === false) $fileType = "."; + else $fileType = substr($filename, $lastDotIndex); + if($fileType == '.md') + $mimetype = 'text/markdown'; + break; + } + return $mimetype; + } /* }}} */ + + /** + * @param integer $size + * @param array $sizes list of units for 10^0, 10^3, 10^6, ..., 10^(n*3) bytes * @return string */ static function format_filesize($size, $sizes = array('Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')) { /* {{{ */