forked from broofa/mime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mime.js
65 lines (53 loc) · 1.93 KB
/
mime.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var path = require('path'),
fs = require('fs');
var mime = module.exports = {
// Map of extension to mime type
types: {},
// Map of mime type to extension
extensions :{},
// Load an Apache2-style ".types" file
//
// This may be called multiple times (it's expected). Where files declare
// overlapping types/extensions, the last file wins.
load: function(file) {
// Read file and split into lines
var content = fs.readFileSync(file, 'ascii'),
lines = content.split(/[\r\n]+/);
lines.forEach(function(line, lineno) {
// Clean up whitespace/comments, and split into fields
var fields = line.replace(/^\s*|\s*#.*|\s*$/, '')
.split(/\s+/);
if (fields.length >= 2) { // Must have type and at least 1 extension
// Map each extension to the mime type
for (var i = 1; i < fields.length; i++) {
mime.types[fields[i]] = fields[0];
}
// Map mime type to the first extension
mime.extensions[fields[0]] = fields[1];
}
});
},
// Lookup a mime type based on extension
lookup: function(path, fallback) {
var ext = path.replace(/.*[\.\/]/, '').toLowerCase();
return mime.types[ext] || fallback || mime.default_type;
},
// Return file extension associated with a mime type
extension: function(mimeType) {
return mime.extensions[mimeType];
},
// Lookup a charset based on mime type.
charsets: {
lookup: function (mimeType, fallback) {
// Assume text types are utf8. Modify mime logic as needed.
return /^text\//.test(mimeType) ? 'UTF-8' : fallback;
}
}
};
// Load our local copy of
// http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
mime.load(path.join(__dirname, 'mime.types'));
// Overlay enhancements we've had requests for (and that seem to make sense)
mime.load(path.join(__dirname, 'node.types'));
// Set the default type
mime.default_type = mime.types.bin;