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 ".ico":
52 			return "image/vnd.microsoft.icon";
53 		case ".swf":
54 			return "application/x-shockwave-flash";
55 		case ".wav":
56 			return "audio/wav";
57 		case ".mp3":
58 			return "audio/mpeg";
59 		case ".webm":
60 			return "video/webm";
61 		case ".mp4":
62 			return "video/mp4";
63 
64 		case ".c":
65 			return "text/x-csrc";
66 		case ".h":
67 			return "text/x-chdr";
68 		case ".cpp":
69 		case ".c++":
70 		case ".cxx":
71 		case ".cc":
72 			return "text/x-c++src";
73 		case ".hpp":
74 		case ".h++":
75 		case ".hxx":
76 		case ".hh":
77 			return "text/x-c++hdr";
78 		case ".d": // by extension :P
79 			return "text/x-dsrc";
80 		case ".di":
81 			return "text/x-dhdr";
82 
83 		// https://pki-tutorial.readthedocs.io/en/latest/mime.html
84 
85 		case ".p8":
86 		// case ".key":
87 			return "application/pkcs8";
88 		case ".p10":
89 		// case ".csr":
90 			return "application/pkcs10";
91 		// case ".cer":
92 		// 	return "application/pkix-cert";
93 		// case ".crl":
94 		// 	return "application/pkix-crl";
95 		case ".p7c":
96 			return "application/pkcs7-mime";
97 
98 		// case ".crt":
99 		// case ".der":
100 		// 	return "application/x-x509-ca-cert";
101 		// case ".crt":
102 		// 	return "application/x-x509-user-cert";
103 		// case ".crl":
104 		// 	return "application/x-pkcs7-crl";
105 
106 		case ".pem":
107 			return "application/x-pem-file";
108 		case ".p12":
109 		case ".pfx":
110 			return "application/x-pkcs12";
111 
112 		case ".p7b":
113 		case ".spc":
114 			return "application/x-pkcs7-certificates";
115 		case ".p7r":
116 			return "application/x-pkcs7-certreqresp";
117 
118 		default:
119 			return defaultResult;
120 	}
121 }