Merge "LOG SQL dump files getting installed"
[sdnc/oam.git] / dgbuilder / dgeflows / node_modules / express / node_modules / send / node_modules / mime / mime.js
1 var path = require('path');
2 var fs = require('fs');
3
4 function Mime() {
5   // Map of extension -> mime type
6   this.types = Object.create(null);
7
8   // Map of mime type -> extension
9   this.extensions = Object.create(null);
10 }
11
12 /**
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.
16  *
17  * e.g. mime.define({'audio/ogg', ['oga', 'ogg', 'spx']});
18  *
19  * @param map (Object) type definitions
20  */
21 Mime.prototype.define = function (map) {
22   for (var type in map) {
23     var exts = map[type];
24
25     for (var i = 0; i < exts.length; i++) {
26       if (process.env.DEBUG_MIME && this.types[exts]) {
27         console.warn(this._loading.replace(/.*\//, ''), 'changes "' + exts[i] + '" extension type from ' +
28           this.types[exts] + ' to ' + type);
29       }
30
31       this.types[exts[i]] = type;
32     }
33
34     // Default extension is the first one we encounter
35     if (!this.extensions[type]) {
36       this.extensions[type] = exts[0];
37     }
38   }
39 };
40
41 /**
42  * Load an Apache2-style ".types" file
43  *
44  * This may be called multiple times (it's expected).  Where files declare
45  * overlapping types/extensions, the last file wins.
46  *
47  * @param file (String) path of file to load.
48  */
49 Mime.prototype.load = function(file) {
50
51   this._loading = file;
52   // Read file and split into lines
53   var map = {},
54       content = fs.readFileSync(file, 'ascii'),
55       lines = content.split(/[\r\n]+/);
56
57   lines.forEach(function(line) {
58     // Clean up whitespace/comments, and split into fields
59     var fields = line.replace(/\s*#.*|^\s*|\s*$/g, '').split(/\s+/);
60     map[fields.shift()] = fields;
61   });
62
63   this.define(map);
64
65   this._loading = null;
66 };
67
68 /**
69  * Lookup a mime type based on extension
70  */
71 Mime.prototype.lookup = function(path, fallback) {
72   var ext = path.replace(/.*[\.\/\\]/, '').toLowerCase();
73
74   return this.types[ext] || fallback || this.default_type;
75 };
76
77 /**
78  * Return file extension associated with a mime type
79  */
80 Mime.prototype.extension = function(mimeType) {
81   var type = mimeType.match(/^\s*([^;\s]*)(?:;|\s|$)/)[1].toLowerCase();
82   return this.extensions[type];
83 };
84
85 // Default instance
86 var mime = new Mime();
87
88 // Load local copy of
89 // http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
90 mime.load(path.join(__dirname, 'types/mime.types'));
91
92 // Load additional types from node.js community
93 mime.load(path.join(__dirname, 'types/node.types'));
94
95 // Default type
96 mime.default_type = mime.lookup('bin');
97
98 //
99 // Additional API specific to the default instance
100 //
101
102 mime.Mime = Mime;
103
104 /**
105  * Lookup a charset based on mime type.
106  */
107 mime.charsets = {
108   lookup: function(mimeType, fallback) {
109     // Assume text types are utf8
110     return (/^text\//).test(mimeType) ? 'UTF-8' : fallback;
111   }
112 };
113
114 module.exports = mime;