From 4467f8748fb5969cc78329f623b5787b03db1c1d Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Tue, 28 May 2024 17:31:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96FileInfo=E4=B8=ADMimeType?= =?UTF-8?q?=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Library/Utils/FileInfo.php | 115 +++++++++++++++++---------------- 1 file changed, 60 insertions(+), 55 deletions(-) diff --git a/app/Library/Utils/FileInfo.php b/app/Library/Utils/FileInfo.php index e0a95a34..0ff8f0f1 100644 --- a/app/Library/Utils/FileInfo.php +++ b/app/Library/Utils/FileInfo.php @@ -39,82 +39,87 @@ class FileInfo 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) { - $info = new \finfo(FILEINFO_MIME_TYPE); - - return $info->file($file); + return mime_content_type($file); } public static function getMimeTypeByExt($ext) { $mimeTypes = self::getMimeTypes(); - return $mimeTypes[$ext] ?? null; + return $mimeTypes[$ext] ? $mimeTypes[$ext][0] : ''; } public static function getMimeTypes() { return [ - 'aac' => 'audio/aac', - 'ogg' => 'audio/ogg', - 'wav' => 'audio/wav', - 'mp3' => 'audio/mpeg', - 'weba' => 'audio/webm', - 'm4a' => 'audio/x-m4a', - 'wma' => 'audio/x-ms-wma', + 'aac' => ['audio/aac'], + 'ogg' => ['audio/ogg'], + 'wav' => ['audio/wav'], + 'mp3' => ['audio/mpeg'], + 'weba' => ['audio/webm'], + 'm4a' => ['audio/x-m4a'], + 'wma' => ['audio/x-ms-wma'], - 'mp4' => 'video/mp4', - '3gp' => 'video/3gpp', - 'mpeg' => 'video/mpeg', - 'webm' => 'video/webm', - 'flv' => 'video/x-flv', - 'avi' => 'video/x-msvideo', - 'mkv' => 'video/x-matroska', - 'wmv' => 'video/x-ms-wmv', + 'mp4' => ['video/mp4'], + '3gp' => ['video/3gpp'], + 'mpeg' => ['video/mpeg'], + 'webm' => ['video/webm'], + 'flv' => ['video/x-flv'], + 'avi' => ['video/x-msvideo'], + 'mkv' => ['video/x-matroska'], + 'wmv' => ['video/x-ms-wmv'], - 'gif' => 'image/gif', - 'jpeg' => 'image/jpeg', - 'jpg' => 'image/jpeg', - 'png' => 'image/png', - 'webp' => 'image/webp', - 'bmp' => 'image/bmp', - 'ico' => 'image/x-icon', - 'tif' => 'image/tiff', - 'tiff' => 'image/tiff', - 'svg' => 'image/svg+xml', - 'psd' => 'image/vnd.adobe.photoshop', + 'gif' => ['image/gif'], + 'jpeg' => ['image/jpeg'], + 'jpg' => ['image/jpeg'], + 'png' => ['image/png'], + 'webp' => ['image/webp'], + 'bmp' => ['image/bmp'], + 'ico' => ['image/x-icon'], + 'tif' => ['image/tiff'], + 'tiff' => ['image/tiff'], + 'svg' => ['image/svg+xml'], + 'psd' => ['image/vnd.adobe.photoshop'], - 'rar' => 'application/vnd.rar', - 'tar' => 'application/x-tar', - '7z' => 'application/x-7z-compressed', - 'bz' => 'application/x-bzip', - 'bz2' => 'application/x-bzip2', - 'gz' => 'application/gzip', - 'zip' => 'application/zip', + 'rar' => ['application/vnd.rar'], + 'tar' => ['application/x-tar'], + '7z' => ['application/x-7z-compressed'], + 'bz' => ['application/x-bzip'], + 'bz2' => ['application/x-bzip2'], + 'gz' => ['application/gzip'], + 'zip' => ['application/zip'], - 'txt' => 'text/plain', - 'csv' => 'text/csv', - 'json' => 'application/json', - 'xml' => 'application/xml', + 'txt' => ['text/plain'], + 'csv' => ['text/csv'], + 'json' => ['application/json'], + 'xml' => ['application/xml'], - 'pdf' => 'application/pdf', - 'doc' => 'application/msword', - 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', - 'ppt' => 'application/vnd.ms-powerpoint', - 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', - 'xls' => 'application/vnd.ms-excel', - 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', - 'swf' => 'application/x-shockwave-flash', - 'vsd' => 'application/vnd.visio', - 'rtf' => 'application/rtf', + 'ofd' => ['application/octet-stream'], + 'pdf' => ['application/pdf'], + 'doc' => ['application/msword', 'application/CDFV2'], + 'docx' => ['application/vnd.openxmlformats-officedocument.wordprocessingml.document'], + 'ppt' => ['application/vnd.ms-powerpoint'], + 'pptx' => ['application/vnd.openxmlformats-officedocument.presentationml.presentation'], + 'xls' => ['application/vnd.ms-excel'], + 'xlsx' => ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'], + 'swf' => ['application/x-shockwave-flash'], + 'vsd' => ['application/vnd.visio'], + 'rtf' => ['application/rtf'], - 'ttf' => 'font/ttf', - 'woff' => 'font/woff', - 'woff2' => 'font/woff2', + 'ttf' => ['font/ttf'], + 'woff' => ['font/woff'], + 'woff2' => ['font/woff2'], ]; }