1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-30 14:14:55 +08:00

优化FileInfo中MimeType检查

This commit is contained in:
xiaochong0302 2024-05-28 17:31:39 +08:00
parent 45125453e0
commit 4467f8748f

View File

@ -39,82 +39,87 @@ class FileInfo
public static function isSecure($mime) public static function isSecure($mime)
{ {
return in_array($mime, self::getMimeTypes()); foreach (self::getMimeTypes() as $mimeTypes) {
if (in_array($mime, $mimeTypes)) {
return true;
}
}
return false;
} }
public static function getMimeType($file) public static function getMimeType($file)
{ {
$info = new \finfo(FILEINFO_MIME_TYPE); return mime_content_type($file);
return $info->file($file);
} }
public static function getMimeTypeByExt($ext) public static function getMimeTypeByExt($ext)
{ {
$mimeTypes = self::getMimeTypes(); $mimeTypes = self::getMimeTypes();
return $mimeTypes[$ext] ?? null; return $mimeTypes[$ext] ? $mimeTypes[$ext][0] : '';
} }
public static function getMimeTypes() public static function getMimeTypes()
{ {
return [ return [
'aac' => 'audio/aac', 'aac' => ['audio/aac'],
'ogg' => 'audio/ogg', 'ogg' => ['audio/ogg'],
'wav' => 'audio/wav', 'wav' => ['audio/wav'],
'mp3' => 'audio/mpeg', 'mp3' => ['audio/mpeg'],
'weba' => 'audio/webm', 'weba' => ['audio/webm'],
'm4a' => 'audio/x-m4a', 'm4a' => ['audio/x-m4a'],
'wma' => 'audio/x-ms-wma', 'wma' => ['audio/x-ms-wma'],
'mp4' => 'video/mp4', 'mp4' => ['video/mp4'],
'3gp' => 'video/3gpp', '3gp' => ['video/3gpp'],
'mpeg' => 'video/mpeg', 'mpeg' => ['video/mpeg'],
'webm' => 'video/webm', 'webm' => ['video/webm'],
'flv' => 'video/x-flv', 'flv' => ['video/x-flv'],
'avi' => 'video/x-msvideo', 'avi' => ['video/x-msvideo'],
'mkv' => 'video/x-matroska', 'mkv' => ['video/x-matroska'],
'wmv' => 'video/x-ms-wmv', 'wmv' => ['video/x-ms-wmv'],
'gif' => 'image/gif', 'gif' => ['image/gif'],
'jpeg' => 'image/jpeg', 'jpeg' => ['image/jpeg'],
'jpg' => 'image/jpeg', 'jpg' => ['image/jpeg'],
'png' => 'image/png', 'png' => ['image/png'],
'webp' => 'image/webp', 'webp' => ['image/webp'],
'bmp' => 'image/bmp', 'bmp' => ['image/bmp'],
'ico' => 'image/x-icon', 'ico' => ['image/x-icon'],
'tif' => 'image/tiff', 'tif' => ['image/tiff'],
'tiff' => 'image/tiff', 'tiff' => ['image/tiff'],
'svg' => 'image/svg+xml', 'svg' => ['image/svg+xml'],
'psd' => 'image/vnd.adobe.photoshop', 'psd' => ['image/vnd.adobe.photoshop'],
'rar' => 'application/vnd.rar', 'rar' => ['application/vnd.rar'],
'tar' => 'application/x-tar', 'tar' => ['application/x-tar'],
'7z' => 'application/x-7z-compressed', '7z' => ['application/x-7z-compressed'],
'bz' => 'application/x-bzip', 'bz' => ['application/x-bzip'],
'bz2' => 'application/x-bzip2', 'bz2' => ['application/x-bzip2'],
'gz' => 'application/gzip', 'gz' => ['application/gzip'],
'zip' => 'application/zip', 'zip' => ['application/zip'],
'txt' => 'text/plain', 'txt' => ['text/plain'],
'csv' => 'text/csv', 'csv' => ['text/csv'],
'json' => 'application/json', 'json' => ['application/json'],
'xml' => 'application/xml', 'xml' => ['application/xml'],
'pdf' => 'application/pdf', 'ofd' => ['application/octet-stream'],
'doc' => 'application/msword', 'pdf' => ['application/pdf'],
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'doc' => ['application/msword', 'application/CDFV2'],
'ppt' => 'application/vnd.ms-powerpoint', 'docx' => ['application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'ppt' => ['application/vnd.ms-powerpoint'],
'xls' => 'application/vnd.ms-excel', 'pptx' => ['application/vnd.openxmlformats-officedocument.presentationml.presentation'],
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'xls' => ['application/vnd.ms-excel'],
'swf' => 'application/x-shockwave-flash', 'xlsx' => ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
'vsd' => 'application/vnd.visio', 'swf' => ['application/x-shockwave-flash'],
'rtf' => 'application/rtf', 'vsd' => ['application/vnd.visio'],
'rtf' => ['application/rtf'],
'ttf' => 'font/ttf', 'ttf' => ['font/ttf'],
'woff' => 'font/woff', 'woff' => ['font/woff'],
'woff2' => 'font/woff2', 'woff2' => ['font/woff2'],
]; ];
} }