1 var path = require('path');
2 var fs = require('fs');
5 // Map of extension -> mime type
6 this.types = Object.create(null);
8 // Map of mime type -> extension
9 this.extensions = Object.create(null);
13 * Define mimetype -> extension mappings. Each key is a mime-type that maps
14 * to an array of extensions associated with the type. The first extension is
15 * used as the default extension for the type.
17 * e.g. mime.define({'audio/ogg', ['oga', 'ogg', 'spx']});
19 * @param map (Object) type definitions
21 Mime.prototype.define = function (map) {
22 for (var type in map) {
24 for (var i = 0; i < exts.length; i++) {
25 if (process.env.DEBUG_MIME && this.types[exts]) {
26 console.warn(this._loading.replace(/.*\//, ''), 'changes "' + exts[i] + '" extension type from ' +
27 this.types[exts] + ' to ' + type);
30 this.types[exts[i]] = type;
33 // Default extension is the first one we encounter
34 if (!this.extensions[type]) {
35 this.extensions[type] = exts[0];
41 * Load an Apache2-style ".types" file
43 * This may be called multiple times (it's expected). Where files declare
44 * overlapping types/extensions, the last file wins.
46 * @param file (String) path of file to load.
48 Mime.prototype.load = function(file) {
50 // Read file and split into lines
52 content = fs.readFileSync(file, 'ascii'),
53 lines = content.split(/[\r\n]+/);
55 lines.forEach(function(line) {
56 // Clean up whitespace/comments, and split into fields
57 var fields = line.replace(/\s*#.*|^\s*|\s*$/g, '').split(/\s+/);
58 map[fields.shift()] = fields;
67 * Lookup a mime type based on extension
69 Mime.prototype.lookup = function(path, fallback) {
70 var ext = path.replace(/.*[\.\/\\]/, '').toLowerCase();
72 return this.types[ext] || fallback || this.default_type;
76 * Return file extension associated with a mime type
78 Mime.prototype.extension = function(mimeType) {
79 var type = mimeType.match(/^\s*([^;\s]*)(?:;|\s|$)/)[1].toLowerCase();
80 return this.extensions[type];
84 var mime = new Mime();
86 // Define built-in types
87 mime.define(require('./types.json'));
90 mime.default_type = mime.lookup('bin');
93 // Additional API specific to the default instance
99 * Lookup a charset based on mime type.
102 lookup: function(mimeType, fallback) {
103 // Assume text types are utf8
104 return (/^text\//).test(mimeType) ? 'UTF-8' : fallback;
108 module.exports = mime;