MimeType   A
last analyzed

Coupling/Cohesion

Components 0
Dependencies 0

Complexity

Total Complexity 11

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
lcom 0
cbo 0
dl 0
loc 207
ccs 158
cts 158
cp 1
rs 10
c 2
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B detectByContent() 0 14 5
A detectByFileExtension() 0 14 3
A detectByFilename() 0 6 2
B getExtensionToMimeTypeMap() 0 145 1
1
<?php
2
3
namespace League\Flysystem\Util;
4
5
use finfo;
6
use ErrorException;
7
8
/**
9
 * @internal
10
 */
11
class MimeType
12
{
13
    /**
14
     * Detects MIME Type based on given content.
15
     *
16
     * @param mixed $content
17
     *
18
     * @return string|null MIME Type or NULL if no mime type detected
19
     */
20 30
    public static function detectByContent($content)
21
    {
22 30
        if ( ! class_exists('finfo') || ! is_string($content)) {
23 3
            return null;
24
        }
25
        try {
26 27
            $finfo = new finfo(FILEINFO_MIME_TYPE);
27
28 27
            return $finfo->buffer($content) ?: null;
29
        // @codeCoverageIgnoreStart
30
        } catch( ErrorException $e ) {
31
            // This is caused by an array to string conversion error.
32
        }
33
    } // @codeCoverageIgnoreEnd
34
35
    /**
36
     * Detects MIME Type based on file extension.
37
     *
38
     * @param string $extension
39
     *
40
     * @return string|null MIME Type or NULL if no extension detected
41
     */
42 30
    public static function detectByFileExtension($extension)
43
    {
44 30
        static $extensionToMimeTypeMap;
45
46 30
        if (! $extensionToMimeTypeMap) {
47 3
            $extensionToMimeTypeMap = static::getExtensionToMimeTypeMap();
48 1
        }
49
50 30
        if (isset($extensionToMimeTypeMap[$extension])) {
51 27
            return $extensionToMimeTypeMap[$extension];
52
        }
53
54 3
        return 'text/plain';
55
    }
56
57
    /**
58
     * @param string $filename
59
     *
60
     * @return string|null MIME Type or NULL if no extension detected
61
     */
62 27
    public static function detectByFilename($filename)
63
    {
64 27
        $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
65
66 27
        return empty($extension) ? 'text/plain' : static::detectByFileExtension($extension);
67
    }
68
69
    /**
70
     * @return array Map of file extension to MIME Type
71
     */
72 3
    public static function getExtensionToMimeTypeMap()
73
    {
74
        return [
75 3
            'hqx'   => 'application/mac-binhex40',
76 1
            'cpt'   => 'application/mac-compactpro',
77 1
            'csv'   => 'text/x-comma-separated-values',
78 1
            'bin'   => 'application/octet-stream',
79 1
            'dms'   => 'application/octet-stream',
80 1
            'lha'   => 'application/octet-stream',
81 1
            'lzh'   => 'application/octet-stream',
82 1
            'exe'   => 'application/octet-stream',
83 1
            'class' => 'application/octet-stream',
84 1
            'psd'   => 'application/x-photoshop',
85 1
            'so'    => 'application/octet-stream',
86 1
            'sea'   => 'application/octet-stream',
87 1
            'dll'   => 'application/octet-stream',
88 1
            'oda'   => 'application/oda',
89 1
            'pdf'   => 'application/pdf',
90 1
            'ai'    => 'application/pdf',
91 1
            'eps'   => 'application/postscript',
92 1
            'ps'    => 'application/postscript',
93 1
            'smi'   => 'application/smil',
94 1
            'smil'  => 'application/smil',
95 1
            'mif'   => 'application/vnd.mif',
96 1
            'xls'   => 'application/vnd.ms-excel',
97 1
            'ppt'   => 'application/powerpoint',
98 1
            'pptx'  => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
99 1
            'wbxml' => 'application/wbxml',
100 1
            'wmlc'  => 'application/wmlc',
101 1
            'dcr'   => 'application/x-director',
102 1
            'dir'   => 'application/x-director',
103 1
            'dxr'   => 'application/x-director',
104 1
            'dvi'   => 'application/x-dvi',
105 1
            'gtar'  => 'application/x-gtar',
106 1
            'gz'    => 'application/x-gzip',
107 1
            'gzip'  => 'application/x-gzip',
108 1
            'php'   => 'application/x-httpd-php',
109 1
            'php4'  => 'application/x-httpd-php',
110 1
            'php3'  => 'application/x-httpd-php',
111 1
            'phtml' => 'application/x-httpd-php',
112 1
            'phps'  => 'application/x-httpd-php-source',
113 1
            'js'    => 'application/javascript',
114 1
            'swf'   => 'application/x-shockwave-flash',
115 1
            'sit'   => 'application/x-stuffit',
116 1
            'tar'   => 'application/x-tar',
117 1
            'tgz'   => 'application/x-tar',
118 1
            'z'     => 'application/x-compress',
119 1
            'xhtml' => 'application/xhtml+xml',
120 1
            'xht'   => 'application/xhtml+xml',
121 1
            'zip'   => 'application/x-zip',
122 1
            'rar'   => 'application/x-rar',
123 1
            'mid'   => 'audio/midi',
124 1
            'midi'  => 'audio/midi',
125 1
            'mpga'  => 'audio/mpeg',
126 1
            'mp2'   => 'audio/mpeg',
127 1
            'mp3'   => 'audio/mpeg',
128 1
            'aif'   => 'audio/x-aiff',
129 1
            'aiff'  => 'audio/x-aiff',
130 1
            'aifc'  => 'audio/x-aiff',
131 1
            'ram'   => 'audio/x-pn-realaudio',
132 1
            'rm'    => 'audio/x-pn-realaudio',
133 1
            'rpm'   => 'audio/x-pn-realaudio-plugin',
134 1
            'ra'    => 'audio/x-realaudio',
135 1
            'rv'    => 'video/vnd.rn-realvideo',
136 1
            'wav'   => 'audio/x-wav',
137 1
            'jpg'   => 'image/jpeg',
138 1
            'jpeg'  => 'image/jpeg',
139 1
            'jpe'   => 'image/jpeg',
140 1
            'png'   => 'image/png',
141 1
            'gif'   => 'image/gif',
142 1
            'bmp'   => 'image/bmp',
143 1
            'tiff'  => 'image/tiff',
144 1
            'tif'   => 'image/tiff',
145 1
            'svg'   => 'image/svg+xml',
146 1
            'css'   => 'text/css',
147 1
            'html'  => 'text/html',
148 1
            'htm'   => 'text/html',
149 1
            'shtml' => 'text/html',
150 1
            'txt'   => 'text/plain',
151 1
            'text'  => 'text/plain',
152 1
            'log'   => 'text/plain',
153 1
            'rtx'   => 'text/richtext',
154 1
            'rtf'   => 'text/rtf',
155 1
            'xml'   => 'application/xml',
156 1
            'xsl'   => 'application/xml',
157 1
            'mpeg'  => 'video/mpeg',
158 1
            'mpg'   => 'video/mpeg',
159 1
            'mpe'   => 'video/mpeg',
160 1
            'qt'    => 'video/quicktime',
161 1
            'mov'   => 'video/quicktime',
162 1
            'avi'   => 'video/x-msvideo',
163 1
            'movie' => 'video/x-sgi-movie',
164 1
            'doc'   => 'application/msword',
165 1
            'docx'  => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
166 1
            'dot'   => 'application/msword',
167 1
            'dotx'  => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
168 1
            'xlsx'  => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
169 1
            'word'  => 'application/msword',
170 1
            'xl'    => 'application/excel',
171 1
            'eml'   => 'message/rfc822',
172 1
            'json'  => 'application/json',
173 1
            'pem'   => 'application/x-x509-user-cert',
174 1
            'p10'   => 'application/x-pkcs10',
175 1
            'p12'   => 'application/x-pkcs12',
176 1
            'p7a'   => 'application/x-pkcs7-signature',
177 1
            'p7c'   => 'application/pkcs7-mime',
178 1
            'p7m'   => 'application/pkcs7-mime',
179 1
            'p7r'   => 'application/x-pkcs7-certreqresp',
180 1
            'p7s'   => 'application/pkcs7-signature',
181 1
            'crt'   => 'application/x-x509-ca-cert',
182 1
            'crl'   => 'application/pkix-crl',
183 1
            'der'   => 'application/x-x509-ca-cert',
184 1
            'kdb'   => 'application/octet-stream',
185 1
            'pgp'   => 'application/pgp',
186 1
            'gpg'   => 'application/gpg-keys',
187 1
            'sst'   => 'application/octet-stream',
188 1
            'csr'   => 'application/octet-stream',
189 1
            'rsa'   => 'application/x-pkcs7',
190 1
            'cer'   => 'application/pkix-cert',
191 1
            '3g2'   => 'video/3gpp2',
192 1
            '3gp'   => 'video/3gp',
193 1
            'mp4'   => 'video/mp4',
194 1
            'm4a'   => 'audio/x-m4a',
195 1
            'f4v'   => 'video/mp4',
196 1
            'webm'  => 'video/webm',
197 1
            'aac'   => 'audio/x-acc',
198 1
            'm4u'   => 'application/vnd.mpegurl',
199 1
            'm3u'   => 'text/plain',
200 1
            'xspf'  => 'application/xspf+xml',
201 1
            'vlc'   => 'application/videolan',
202 1
            'wmv'   => 'video/x-ms-wmv',
203 1
            'au'    => 'audio/x-au',
204 1
            'ac3'   => 'audio/ac3',
205 1
            'flac'  => 'audio/x-flac',
206 1
            'ogg'   => 'audio/ogg',
207 1
            'kmz'   => 'application/vnd.google-earth.kmz',
208 1
            'kml'   => 'application/vnd.google-earth.kml+xml',
209 1
            'ics'   => 'text/calendar',
210 1
            'zsh'   => 'text/x-scriptzsh',
211 1
            '7zip'  => 'application/x-7z-compressed',
212 1
            'cdr'   => 'application/cdr',
213 1
            'wma'   => 'audio/x-ms-wma',
214 1
            'jar'   => 'application/java-archive',
215 1
        ];
216
    }
217
}
218