add method mimetype()

This commit is contained in:
Uwe Steinmann 2022-11-17 11:31:12 +01:00
parent 0e8f9c5164
commit 7716d3ad54

View File

@ -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')) { /* {{{ */