Browse Source

优化thinkphp的fileinfo扩展使用

evoxwht 2 years ago
parent
commit
543cce1f4b

+ 46 - 9
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