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