1 /**
2  * MIME types for common extensions.
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Vladimir Panteleev <vladimir@thecybershadow.net>
12  */
13 
14 module ae.utils.mime;
15 
16 import std.string;
17 import std.path;
18 
19 string guessMime(string fileName, string defaultResult = null)
20 {
21 	string ext = toLower(extension(fileName));
22 
23 	if (ext.endsWith("-opt"))
24 		ext = ext[0..$-4]; // HACK
25 
26 	switch (ext)
27 	{
28 		case ".txt":
29 			return "text/plain";
30 		case ".htm":
31 		case ".html":
32 			return "text/html";
33 		case ".js":
34 			return "text/javascript";
35 		case ".json":
36 			return "application/json";
37 		case ".css":
38 			return "text/css";
39 		case ".png":
40 			return "image/png";
41 		case ".gif":
42 			return "image/gif";
43 		case ".jpg":
44 		case ".jpeg":
45 			return "image/jpeg";
46 		case ".svg":
47 			return "image/svg+xml";
48 		case ".webm":
49 			return "video/webm";
50 		case ".wav":
51 			return "audio/wav";
52 		case ".ico":
53 			return "image/vnd.microsoft.icon";
54 
55 		case ".c":
56 			return "text/x-csrc";
57 		case ".h":
58 			return "text/x-chdr";
59 		case ".cpp":
60 		case ".c++":
61 		case ".cxx":
62 		case ".cc":
63 			return "text/x-c++src";
64 		case ".hpp":
65 		case ".h++":
66 		case ".hxx":
67 		case ".hh":
68 			return "text/x-c++hdr";
69 		case ".d": // by extension :P
70 			return "text/x-dsrc";
71 		case ".di":
72 			return "text/x-dhdr";
73 
74 		// https://pki-tutorial.readthedocs.io/en/latest/mime.html
75 
76 		case ".p8":
77 		// case ".key":
78 			return "application/pkcs8";
79 		case ".p10":
80 		// case ".csr":
81 			return "application/pkcs10";
82 		// case ".cer":
83 		// 	return "application/pkix-cert";
84 		// case ".crl":
85 		// 	return "application/pkix-crl";
86 		case ".p7c":
87 			return "application/pkcs7-mime";
88 
89 		// case ".crt":
90 		// case ".der":
91 		// 	return "application/x-x509-ca-cert";
92 		// case ".crt":
93 		// 	return "application/x-x509-user-cert";
94 		// case ".crl":
95 		// 	return "application/x-pkcs7-crl";
96 
97 		case ".pem":
98 			return "application/x-pem-file";
99 		case ".p12":
100 		case ".pfx":
101 			return "application/x-pkcs12";
102 
103 		case ".p7b":
104 		case ".spc":
105 			return "application/x-pkcs7-certificates";
106 		case ".p7r":
107 			return "application/x-pkcs7-certreqresp";
108 
109 		default:
110 			return defaultResult;
111 	}
112 }