MimeType::detectByFilename()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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