MimeType::getExtensionToMimeTypeMap()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

216
        $extension = strtolower(/** @scrutinizer ignore-type */ pathinfo($filename, PATHINFO_EXTENSION));
Loading history...
217
218
        return empty($extension) ? 'text/plain' : static::detectByFileExtension($extension);
219
    }
220
221
    /**
222
     * @return array Map of file extension to MIME Type
223
     */
224
    public static function getExtensionToMimeTypeMap()
225
    {
226
        return static::$extensionToMimeTypeMap;
227
    }
228
}
229