Completed
Pull Request — master (#29)
by Tim
13:43
created

MimeTypes::getMimeTypeByExtension()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * \AppserverIo\Server\Dictionaries\MimeTypes
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Johann Zelger <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/server
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Server\Dictionaries;
22
23
/**
24
 * Class MimeTypes
25
 *
26
 * @author    Johann Zelger <[email protected]>
27
 * @copyright 2015 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/appserver-io/server
30
 * @link      http://www.appserver.io
31
 */
32
class MimeTypes
33
{
34
35
    /**
36
     * Defines mimetype default
37
     *
38
     * @var string
39
     */
40
    const MIMETYPE_DEFAULT = "application/octet-stream";
41
42
    /**
43
     * Get the MIME Type for the specified filename.
44
     *
45
     * @param string $filename The filename to get mimeType for
46
     *
47
     * @return string
48
     */
49 3
    public static function getMimeTypeByFilename($filename)
50
    {
51
        // check if there is an filename extension
52 3
        $fileExtension = pathinfo($filename, PATHINFO_EXTENSION);
53 3
        if ($fileExtension) {
54
            // check if there is a mimeType for this extension
55 2
            if ($mimeType = self::getMimeTypeByExtension($fileExtension)) {
56 2
                return $mimeType;
57
            }
58
        }
59
        // return mimeType default
60 1
        return self::MIMETYPE_DEFAULT;
61
    }
62
63
    /**
64
     * Return's the mimeType by given extension
65
     *
66
     * @param string $extension The extension to find the mime type for
67
     *
68
     * @return string
69
     */
70 4
    public static function getMimeTypeByExtension($extension)
71
    {
72
        // normalize extension by doing lower str
73 4
        $extension = strtolower($extension);
74 4
        if (isset(self::$types[$extension])) {
75 2
            return self::$types[$extension];
76
        }
77 2
        return self::MIMETYPE_DEFAULT;
78
    }
79
80
    public static $types = array(
81
        "html" => "text/html",
82
        "htm" => "text/html",
83
        "shtml" => "text/html",
84
        "css" => "text/css",
85
        "xml" => "text/xml",
86
        "rss" => "text/xml",
87
        "gif" => "image/gif",
88
        "jpeg" => "image/jpeg",
89
        "jpg" => "image/jpeg",
90
        "js" => "application/javascript",
91
        "atom" => "application/atom+xml",
92
        "mml" => "text/mathml",
93
        "txt" => "text/plain",
94
        "csv" => "text/plain",
95
        "jad" => "text/vnd.sun.j2me.app-descriptor",
96
        "wml" => "text/vnd.wap.wml",
97
        "htc" => "text/component",
98
        "png" => "image/png",
99
        "tif" => "image/tiff",
100
        "tiff" => "image/tiff",
101
        "wbmp" => "image/vnd.wap.wbmp",
102
        "ico" => "image/icon",
103
        "jng" => "image/jng",
104
        "bmp" => "image/ms-bmp",
105
        "svg" => "image/svg+xml",
106
        "svgz" => "image/svg+xml",
107
        "jar" => "application/java-archive",
108
        "war" => "application/java-archive",
109
        "ear" => "application/java-archive",
110
        "json" => "application/json",
111
        "hqx" => "application/mac-binhex40",
112
        "doc" => "application/msword",
113
        "pdf" => "application/pdf",
114
        "ps" => "application/postscript",
115
        "eps" => "application/postscript",
116
        "ai" => "application/postscript",
117
        "rtf" => "application/rtf",
118
        "xls" => "application/vnd.ms-excel",
119
        "ppt" => "application/vnd.ms-powerpoint",
120
        "wmlc" => "application/vnd.wap.wmlc",
121
        "kml" => "application/vnd.google-earth.kml+xml",
122
        "kmz" => "application/vnd.google-earth.kmz",
123
        "7z" => "application/x-7z-compressed",
124
        "cco" => "application/x-cocoa",
125
        "jardiff" => "application/x-java-archive-diff",
126
        "jnlp" => "application/x-java-jnlp-file",
127
        "run" => "application/x-makeself",
128
        "pl" => "application/x-perl",
129
        "pm" => "application/x-perl",
130
        "prc" => "application/x-pilot",
131
        "pdb" => "application/x-pilot",
132
        "rar" => "application/x-rar-compressed",
133
        "rpm" => "application/x-redhat-package-manager",
134
        "sea" => "application/x-sea",
135
        "swf" => "application/x-shockwave-flash",
136
        "sit" => "application/x-stuffit",
137
        "tcl" => "application/x-tcl",
138
        "tk" => "application/x-tcl",
139
        "der" => "application/x-x509-ca-cert",
140
        "pem" => "application/x-x509-ca-cert",
141
        "crt" => "application/x-x509-ca-cert",
142
        "xpi" => "application/x-xpinstall",
143
        "xhtml" => "application/xhtml+xml",
144
        "zip" => "application/zip",
145
        "bin" => "application/octet-stream",
146
        "exe" => "application/octet-stream",
147
        "dll" => "application/octet-stream",
148
        "deb" => "application/octet-stream",
149
        "dmg" => "application/octet-stream",
150
        "eot" => "application/vnd.ms-fontobject",
151
        "iso" => "application/octet-stream",
152
        "img" => "application/octet-stream",
153
        "msi" => "application/octet-stream",
154
        "msp" => "application/octet-stream",
155
        "msm" => "application/octet-stream",
156
        "ogx" => "application/ogg",
157
        "mid" => "audio/midi",
158
        "midi" => "audio/midi",
159
        "kar" => "audio/midi",
160
        "mpga" => "audio/mpeg",
161
        "mpega" => "audio/mpeg",
162
        "mp2" => "audio/mpeg",
163
        "mp3" => "audio/mpeg",
164
        "m4a" => "audio/mpeg",
165
        "oga" => "audio/ogg",
166
        "ogg" => "audio/ogg",
167
        "spx" => "audio/ogg",
168
        "ra" => "audio/x-realaudio",
169
        "weba" => "audio/webm",
170
        "3gpp" => "video/3gpp",
171
        "3gp" => "video/3gpp",
172
        "mp4" => "video/mp4",
173
        "mpeg" => "video/mpeg",
174
        "mpg" => "video/mpeg",
175
        "mpe" => "video/mpeg",
176
        "ogv" => "video/ogg",
177
        "mov" => "video/quicktime",
178
        "webm" => "video/webm",
179
        "flv" => "video/x-flv",
180
        "mng" => "video/x-mng",
181
        "asx" => "video/x-ms-asf",
182
        "asf" => "video/x-ms-asf",
183
        "wmv" => "video/x-ms-wmv",
184
        "avi" => "video/x-msvideo",
185
        "otf" => "application/font-sfnt",
186
        "ttf" => "application/font-sfnt",
187
        "woff" => "application/font-woff"
188
    );
189
}
190