1 var path = require('path'),
4 var mime = module.exports = {
5 /** Map of extension to mime type */
8 /** Map of mime type to extension */
12 * Define mimetype -> extension mappings. Each key is a mime-type that maps
13 * to an array of extensions associated with the type. The first extension is
14 * used as the default extension for the type.
16 * e.g. mime.define({'audio/ogg', ['oga', 'ogg', 'spx']});
18 * @param map (Object) type definitions
20 define: function(map) {
21 for (var type in map) {
24 for (var i = 0; i < exts.length; i++) {
25 mime.types[exts[i]] = type;
28 mime.extensions[type] = exts[0];
33 * Load an Apache2-style ".types" file
35 * This may be called multiple times (it's expected). Where files declare
36 * overlapping types/extensions, the last file wins.
38 * @param file (String) path of file to load.
40 load: function(file) {
41 // Read file and split into lines
43 content = fs.readFileSync(file, 'ascii'),
44 lines = content.split(/[\r\n]+/);
46 lines.forEach(function(line, lineno) {
47 // Clean up whitespace/comments, and split into fields
48 var fields = line.replace(/\s*#.*|^\s*|\s*$/g, '').split(/\s+/);
49 map[fields.shift()] = fields;
56 * Lookup a mime type based on extension
58 lookup: function(path, fallback) {
59 var ext = path.replace(/.*[\.\/]/, '').toLowerCase();
60 return mime.types[ext] || fallback || mime.default_type;
64 * Return file extension associated with a mime type
66 extension: function(mimeType) {
67 return mime.extensions[mimeType];
71 * Lookup a charset based on mime type.
74 lookup: function (mimeType, fallback) {
75 // Assume text types are utf8. Modify mime logic as needed.
76 return /^text\//.test(mimeType) ? 'UTF-8' : fallback;
81 // Load our local copy of
82 // http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
83 mime.load(path.join(__dirname, 'mime.types'));
85 // Overlay enhancements we've had requests for (and that seem to make sense)
86 mime.load(path.join(__dirname, 'node.types'));
88 // Set the default type
89 mime.default_type = mime.types.bin;