Tizen 2.0 Release
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.2.0 / node_modules / grunt / node_modules / connect / 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       this.types[exts[i]] = type;
27     }
28
29     // Default extension is the first one we encounter
30     if (!this.extensions[type]) {
31       this.extensions[type] = exts[0];
32     }
33   }
34 };
35
36 /**
37  * Load an Apache2-style ".types" file
38  *
39  * This may be called multiple times (it's expected).  Where files declare
40  * overlapping types/extensions, the last file wins.
41  *
42  * @param file (String) path of file to load.
43  */
44 Mime.prototype.load = function(file) {
45   // Read file and split into lines
46   var map = {},
47       content = fs.readFileSync(file, 'ascii'),
48       lines = content.split(/[\r\n]+/);
49
50   lines.forEach(function(line) {
51     // Clean up whitespace/comments, and split into fields
52     var fields = line.replace(/\s*#.*|^\s*|\s*$/g, '').split(/\s+/);
53     map[fields.shift()] = fields;
54   });
55
56   this.define(map);
57 };
58
59 /**
60  * Lookup a mime type based on extension
61  */
62 Mime.prototype.lookup = function(path, fallback) {
63   var ext = path.replace(/.*[\.\/]/, '').toLowerCase();
64
65   return this.types[ext] || fallback || this.default_type;
66 };
67
68 /**
69  * Return file extension associated with a mime type
70  */
71 Mime.prototype.extension = function(mimeType) {
72   return this.extensions[mimeType];
73 };
74
75 // Default instance
76 var mime = new Mime();
77
78 // Load local copy of
79 // http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
80 mime.load(path.join(__dirname, 'types/mime.types'));
81
82 // Load additional types from node.js community
83 mime.load(path.join(__dirname, 'types/node.types'));
84
85 // Default type
86 mime.default_type = mime.lookup('bin');
87
88 //
89 // Additional API specific to the default instance
90 //
91
92 mime.Mime = Mime;
93
94 /**
95  * Lookup a charset based on mime type.
96  */
97 mime.charsets = {
98   lookup: function(mimeType, fallback) {
99     // Assume text types are utf8
100     return (/^text\//).test(mimeType) ? 'UTF-8' : fallback;
101   }
102 }
103
104 module.exports = mime;