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 <ae@cy.md>
12  */
13 
14 module ae.utils.mime;
15 
16 import std.string;
17 import std.path;
18 
19 /// Return a likely MIME type for a file with the given name.
20 string guessMime(string fileName, string defaultResult = null)
21 {
22 	string ext = toLower(extension(fileName));
23 
24 	if (ext.endsWith("-opt"))
25 		ext = ext[0..$-4]; // HACK
26 
27 	switch (ext)
28 	{
29 		case ".txt":
30 			return "text/plain";
31 		case ".htm":
32 		case ".html":
33 			return "text/html";
34 		case ".js":
35 			return "text/javascript";
36 		case ".json":
37 			return "application/json";
38 		case ".wasm":
39 			return "application/wasm";
40 		case ".css":
41 			return "text/css";
42 		case ".png":
43 			return "image/png";
44 		case ".gif":
45 			return "image/gif";
46 		case ".jpg":
47 		case ".jpeg":
48 			return "image/jpeg";
49 		case ".svg":
50 			return "image/svg+xml";
51 		case ".swf":
52 			return "application/x-shockwave-flash";
53 		case ".webm":
54 			return "video/webm";
55 		case ".wav":
56 			return "audio/wav";
57 		case ".ico":
58 			return "image/vnd.microsoft.icon";
59 
60 		case ".c":
61 			return "text/x-csrc";
62 		case ".h":
63 			return "text/x-chdr";
64 		case ".cpp":
65 		case ".c++":
66 		case ".cxx":
67 		case ".cc":
68 			return "text/x-c++src";
69 		case ".hpp":
70 		case ".h++":
71 		case ".hxx":
72 		case ".hh":
73 			return "text/x-c++hdr";
74 		case ".d": // by extension :P
75 			return "text/x-dsrc";
76 		case ".di":
77 			return "text/x-dhdr";
78 
79 		// https://pki-tutorial.readthedocs.io/en/latest/mime.html
80 
81 		case ".p8":
82 		// case ".key":
83 			return "application/pkcs8";
84 		case ".p10":
85 		// case ".csr":
86 			return "application/pkcs10";
87 		// case ".cer":
88 		// 	return "application/pkix-cert";
89 		// case ".crl":
90 		// 	return "application/pkix-crl";
91 		case ".p7c":
92 			return "application/pkcs7-mime";
93 
94 		// case ".crt":
95 		// case ".der":
96 		// 	return "application/x-x509-ca-cert";
97 		// case ".crt":
98 		// 	return "application/x-x509-user-cert";
99 		// case ".crl":
100 		// 	return "application/x-pkcs7-crl";
101 
102 		case ".pem":
103 			return "application/x-pem-file";
104 		case ".p12":
105 		case ".pfx":
106 			return "application/x-pkcs12";
107 
108 		case ".p7b":
109 		case ".spc":
110 			return "application/x-pkcs7-certificates";
111 		case ".p7r":
112 			return "application/x-pkcs7-certreqresp";
113 
114 		default:
115 			return defaultResult;
116 	}
117 }