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 3
        }
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 3
            'cpt'   => 'application/mac-compactpro',
76 3
            'csv'   => 'text/x-comma-separated-values',
77 3
            'bin'   => 'application/octet-stream',
78 3
            'dms'   => 'application/octet-stream',
79 3
            'lha'   => 'application/octet-stream',
80 3
            'lzh'   => 'application/octet-stream',
81 3
            'exe'   => 'application/octet-stream',
82 3
            'class' => 'application/octet-stream',
83 3
            'psd'   => 'application/x-photoshop',
84 3
            'so'    => 'application/octet-stream',
85 3
            'sea'   => 'application/octet-stream',
86 3
            'dll'   => 'application/octet-stream',
87 3
            'oda'   => 'application/oda',
88 3
            'pdf'   => 'application/pdf',
89 3
            'ai'    => 'application/pdf',
90 3
            'eps'   => 'application/postscript',
91 3
            'ps'    => 'application/postscript',
92 3
            'smi'   => 'application/smil',
93 3
            'smil'  => 'application/smil',
94 3
            'mif'   => 'application/vnd.mif',
95 3
            'xls'   => 'application/vnd.ms-excel',
96 3
            'ppt'   => 'application/powerpoint',
97 3
            'pptx'  => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
98 3
            'wbxml' => 'application/wbxml',
99 3
            'wmlc'  => 'application/wmlc',
100 3
            'dcr'   => 'application/x-director',
101 3
            'dir'   => 'application/x-director',
102 3
            'dxr'   => 'application/x-director',
103 3
            'dvi'   => 'application/x-dvi',
104 3
            'gtar'  => 'application/x-gtar',
105 3
            'gz'    => 'application/x-gzip',
106 3
            'gzip'  => 'application/x-gzip',
107 3
            'php'   => 'application/x-httpd-php',
108 3
            'php4'  => 'application/x-httpd-php',
109 3
            'php3'  => 'application/x-httpd-php',
110 3
            'phtml' => 'application/x-httpd-php',
111 3
            'phps'  => 'application/x-httpd-php-source',
112 3
            'js'    => 'application/javascript',
113 3
            'swf'   => 'application/x-shockwave-flash',
114 3
            'sit'   => 'application/x-stuffit',
115 3
            'tar'   => 'application/x-tar',
116 3
            'tgz'   => 'application/x-tar',
117 3
            'z'     => 'application/x-compress',
118 3
            'xhtml' => 'application/xhtml+xml',
119 3
            'xht'   => 'application/xhtml+xml',
120 3
            'zip'   => 'application/x-zip',
121 3
            'rar'   => 'application/x-rar',
122 3
            'mid'   => 'audio/midi',
123 3
            'midi'  => 'audio/midi',
124 3
            'mpga'  => 'audio/mpeg',
125 3
            'mp2'   => 'audio/mpeg',
126 3
            'mp3'   => 'audio/mpeg',
127 3
            'aif'   => 'audio/x-aiff',
128 3
            'aiff'  => 'audio/x-aiff',
129 3
            'aifc'  => 'audio/x-aiff',
130 3
            'ram'   => 'audio/x-pn-realaudio',
131 3
            'rm'    => 'audio/x-pn-realaudio',
132 3
            'rpm'   => 'audio/x-pn-realaudio-plugin',
133 3
            'ra'    => 'audio/x-realaudio',
134 3
            'rv'    => 'video/vnd.rn-realvideo',
135 3
            'wav'   => 'audio/x-wav',
136 3
            'jpg'   => 'image/jpeg',
137 3
            'jpeg'  => 'image/jpeg',
138 3
            'jpe'   => 'image/jpeg',
139 3
            'png'   => 'image/png',
140 3
            'gif'   => 'image/gif',
141 3
            'bmp'   => 'image/bmp',
142 3
            'tiff'  => 'image/tiff',
143 3
            'tif'   => 'image/tiff',
144 3
            'svg'   => 'image/svg+xml',
145 3
            'css'   => 'text/css',
146 3
            'html'  => 'text/html',
147 3
            'htm'   => 'text/html',
148 3
            'shtml' => 'text/html',
149 3
            'txt'   => 'text/plain',
150 3
            'text'  => 'text/plain',
151 3
            'log'   => 'text/plain',
152 3
            'rtx'   => 'text/richtext',
153 3
            'rtf'   => 'text/rtf',
154 3
            'xml'   => 'application/xml',
155 3
            'xsl'   => 'application/xml',
156 3
            'mpeg'  => 'video/mpeg',
157 3
            'mpg'   => 'video/mpeg',
158 3
            'mpe'   => 'video/mpeg',
159 3
            'qt'    => 'video/quicktime',
160 3
            'mov'   => 'video/quicktime',
161 3
            'avi'   => 'video/x-msvideo',
162 3
            'movie' => 'video/x-sgi-movie',
163 3
            'doc'   => 'application/msword',
164 3
            'docx'  => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
165 3
            'dot'   => 'application/msword',
166 3
            'dotx'  => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
167 3
            'xlsx'  => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
168 3
            'word'  => 'application/msword',
169 3
            'xl'    => 'application/excel',
170 3
            'eml'   => 'message/rfc822',
171 3
            'json'  => 'application/json',
172 3
            'pem'   => 'application/x-x509-user-cert',
173 3
            'p10'   => 'application/x-pkcs10',
174 3
            'p12'   => 'application/x-pkcs12',
175 3
            'p7a'   => 'application/x-pkcs7-signature',
176 3
            'p7c'   => 'application/pkcs7-mime',
177 3
            'p7m'   => 'application/pkcs7-mime',
178 3
            'p7r'   => 'application/x-pkcs7-certreqresp',
179 3
            'p7s'   => 'application/pkcs7-signature',
180 3
            'crt'   => 'application/x-x509-ca-cert',
181 3
            'crl'   => 'application/pkix-crl',
182 3
            'der'   => 'application/x-x509-ca-cert',
183 3
            'kdb'   => 'application/octet-stream',
184 3
            'pgp'   => 'application/pgp',
185 3
            'gpg'   => 'application/gpg-keys',
186 3
            'sst'   => 'application/octet-stream',
187 3
            'csr'   => 'application/octet-stream',
188 3
            'rsa'   => 'application/x-pkcs7',
189 3
            'cer'   => 'application/pkix-cert',
190 3
            '3g2'   => 'video/3gpp2',
191 3
            '3gp'   => 'video/3gp',
192 3
            'mp4'   => 'video/mp4',
193 3
            'm4a'   => 'audio/x-m4a',
194 3
            'f4v'   => 'video/mp4',
195 3
            'webm'  => 'video/webm',
196 3
            'aac'   => 'audio/x-acc',
197 3
            'm4u'   => 'application/vnd.mpegurl',
198 3
            'm3u'   => 'text/plain',
199 3
            'xspf'  => 'application/xspf+xml',
200 3
            'vlc'   => 'application/videolan',
201 3
            'wmv'   => 'video/x-ms-wmv',
202 3
            'au'    => 'audio/x-au',
203 3
            'ac3'   => 'audio/ac3',
204 3
            'flac'  => 'audio/x-flac',
205 3
            'ogg'   => 'audio/ogg',
206 3
            'kmz'   => 'application/vnd.google-earth.kmz',
207 3
            'kml'   => 'application/vnd.google-earth.kml+xml',
208 3
            'ics'   => 'text/calendar',
209 3
            'zsh'   => 'text/x-scriptzsh',
210 3
            '7zip'  => 'application/x-7z-compressed',
211 3
            'cdr'   => 'application/cdr',
212 3
            'wma'   => 'audio/x-ms-wma',
213 3
            'jar'   => 'application/java-archive',
214 3
        ];
215
    }
216
}
217