chengkun
2025-09-12 b21e53f16f228d3192eb54178f081395878b2406
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
 
namespace League\Flysystem\Util;
 
use League\MimeTypeDetection\FinfoMimeTypeDetector;
use League\MimeTypeDetection\GeneratedExtensionToMimeTypeMap;
use League\MimeTypeDetection\MimeTypeDetector;
 
/**
 * @internal
 */
class MimeType
{
    protected static $extensionToMimeTypeMap = GeneratedExtensionToMimeTypeMap::MIME_TYPES_FOR_EXTENSIONS;
    protected static $detector;
 
    public static function useDetector(MimeTypeDetector $detector)
    {
        static::$detector = $detector;
    }
 
    /**
     * @return MimeTypeDetector
     */
    protected static function detector()
    {
        if ( ! static::$detector instanceof MimeTypeDetector) {
            static::$detector = new FinfoMimeTypeDetector();
        }
 
        return static::$detector;
    }
 
 
    /**
     * Detects MIME Type based on given content.
     *
     * @param mixed $content
     *
     * @return string MIME Type
     */
    public static function detectByContent($content)
    {
        if (is_string($content)) {
            return static::detector()->detectMimeTypeFromBuffer($content);
        }
 
        return 'text/plain';
    }
 
    /**
     * Detects MIME Type based on file extension.
     *
     * @param string $extension
     *
     * @return string MIME Type
     */
    public static function detectByFileExtension($extension)
    {
        return static::detector()->detectMimeTypeFromPath('artificial.' . $extension) ?: 'text/plain';
    }
 
    /**
     * @param string $filename
     *
     * @return string MIME Type
     */
    public static function detectByFilename($filename)
    {
        return static::detector()->detectMimeTypeFromPath($filename) ?: 'text/plain';
    }
 
    /**
     * @return array Map of file extension to MIME Type
     */
    public static function getExtensionToMimeTypeMap()
    {
        return static::$extensionToMimeTypeMap;
    }
}