MimeType   A
last analyzed

Coupling/Cohesion

Components 0
Dependencies 0

Complexity

Total Complexity 11

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Test Coverage

Coverage 98.75%

Importance

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

4 Methods

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