/**
* @param $file
* @param null $filename
* @return mixed|string
* @throws \Exception
*/
public static function detect($file, $filename = null)
{
if (!file_exists($file)) {
throw new \Exception("File " . $file . " doesn't exist");
}
if (!$filename) {
$filename = basename($file);
}
// check for an extension mapping first
if ($filename) {
$extension = \Pimcore\File::getFileExtension($filename);
if (array_key_exists($extension, self::$extensionMapping)) {
return self::$extensionMapping[$extension];
}
}
// check with fileinfo, if there's no extension mapping
$finfo = finfo_open(FILEINFO_MIME);
$type = finfo_file($finfo, $file);
finfo_close($finfo);
if ($type !== false && !empty($type)) {
if (strstr($type, ';')) {
$type = substr($type, 0, strpos($type, ';'));
}
return $type;
}
// return default mime-type if we're unable to detect it
return "application/octet-stream";
}