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