From 543cce1f4b92994d9add1c5295b78c52c8ce1ba0 Mon Sep 17 00:00:00 2001 From: evoxwht <442384644@qq.com> Date: Wed, 24 May 2023 16:11:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96thinkphp=E7=9A=84fileinfo?= =?UTF-8?q?=E6=89=A9=E5=B1=95=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/FinfoMimeTypeDetector.php | 55 ++++++++++++++++--- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/crmeb/vendor/league/mime-type-detection/src/FinfoMimeTypeDetector.php b/crmeb/vendor/league/mime-type-detection/src/FinfoMimeTypeDetector.php index ba91938f7..d892515d1 100644 --- a/crmeb/vendor/league/mime-type-detection/src/FinfoMimeTypeDetector.php +++ b/crmeb/vendor/league/mime-type-detection/src/FinfoMimeTypeDetector.php @@ -45,7 +45,7 @@ class FinfoMimeTypeDetector implements MimeTypeDetector ?int $bufferSampleSize = null, array $inconclusiveMimetypes = self::INCONCLUSIVE_MIME_TYPES ) { - $this->finfo = new finfo(FILEINFO_MIME_TYPE, $magicFile); +// $this->finfo = new finfo(FILEINFO_MIME_TYPE, $magicFile); $this->extensionMap = $extensionMap ?: new GeneratedExtensionToMimeTypeMap(); $this->bufferSampleSize = $bufferSampleSize; $this->inconclusiveMimetypes = $inconclusiveMimetypes; @@ -53,17 +53,53 @@ class FinfoMimeTypeDetector implements MimeTypeDetector public function detectMimeType(string $path, $contents): ?string { - $mimeType = is_string($contents) - ? (@$this->finfo->buffer($this->takeSample($contents)) ?: null) - : null; - - if ($mimeType !== null && ! in_array($mimeType, $this->inconclusiveMimetypes)) { - return $mimeType; +// $mimeType = is_string($contents) +// ? (@$this->finfo->buffer($this->takeSample($contents)) ?: null) +// : null; +// +// if ($mimeType !== null && ! in_array($mimeType, $this->inconclusiveMimetypes)) { +// return $mimeType; +// } + if (is_string($contents)) { + return $this->extensionMimeType($path); + } else { + $mimeType = null; } return $this->detectMimeTypeFromPath($path); } + public function extensionMimeType(string $path) + { + $extension = pathinfo($path, PATHINFO_EXTENSION); + switch ($extension) { + case 'jpg': + case 'jpeg': + return 'image/jpeg'; + case 'png': + return 'image/png'; + case 'gif': + return 'image/gif'; + case 'bmp': + return 'image/bmp'; + case 'txt': + return 'text/plain'; + case 'pdf': + return 'application/pdf'; + case 'doc': + case 'docx': + return 'application/msword'; + case 'xls': + case 'xlsx': + return 'application/vnd.ms-excel'; + case 'ppt': + case 'pptx': + return 'application/vnd.ms-powerpoint'; + default: + return 'application/octet-stream'; + } + } + public function detectMimeTypeFromPath(string $path): ?string { $extension = strtolower(pathinfo($path, PATHINFO_EXTENSION)); @@ -73,12 +109,13 @@ class FinfoMimeTypeDetector implements MimeTypeDetector public function detectMimeTypeFromFile(string $path): ?string { - return @$this->finfo->file($path) ?: null; +// return @$this->finfo->file($path) ?: null; + return $this->extensionMimeType($path); } public function detectMimeTypeFromBuffer(string $contents): ?string { - return @$this->finfo->buffer($this->takeSample($contents)) ?: null; +// return @$this->finfo->buffer($this->takeSample($contents)) ?: null; } private function takeSample(string $contents): string