Projekt

Obecné

Profil

Stáhnout (2.8 KB) Statistiky
| Větev: | Revize:
1 3a515b92 cagy
'use strict';
2
3
/**
4
 * @param typeMap [Object] Map of MIME type -> Array[extensions]
5
 * @param ...
6
 */
7
function Mime() {
8
  this._types = Object.create(null);
9
  this._extensions = Object.create(null);
10
11
  for (var i = 0; i < arguments.length; i++) {
12
    this.define(arguments[i]);
13
  }
14
15
  this.define = this.define.bind(this);
16
  this.getType = this.getType.bind(this);
17
  this.getExtension = this.getExtension.bind(this);
18
}
19
20
/**
21
 * Define mimetype -> extension mappings.  Each key is a mime-type that maps
22
 * to an array of extensions associated with the type.  The first extension is
23
 * used as the default extension for the type.
24
 *
25
 * e.g. mime.define({'audio/ogg', ['oga', 'ogg', 'spx']});
26
 *
27
 * If a type declares an extension that has already been defined, an error will
28
 * be thrown.  To suppress this error and force the extension to be associated
29
 * with the new type, pass `force`=true.  Alternatively, you may prefix the
30
 * extension with "*" to map the type to extension, without mapping the
31
 * extension to the type.
32
 *
33
 * e.g. mime.define({'audio/wav', ['wav']}, {'audio/x-wav', ['*wav']});
34
 *
35
 *
36
 * @param map (Object) type definitions
37
 * @param force (Boolean) if true, force overriding of existing definitions
38
 */
39
Mime.prototype.define = function(typeMap, force) {
40
  for (var type in typeMap) {
41
    var extensions = typeMap[type].map(function(t) {return t.toLowerCase()});
42
    type = type.toLowerCase();
43
44
    for (var i = 0; i < extensions.length; i++) {
45
      var ext = extensions[i];
46
47
      // '*' prefix = not the preferred type for this extension.  So fixup the
48
      // extension, and skip it.
49
      if (ext[0] == '*') {
50
        continue;
51
      }
52
53
      if (!force && (ext in this._types)) {
54
        throw new Error(
55
          'Attempt to change mapping for "' + ext +
56
          '" extension from "' + this._types[ext] + '" to "' + type +
57
          '". Pass `force=true` to allow this, otherwise remove "' + ext +
58
          '" from the list of extensions for "' + type + '".'
59
        );
60
      }
61
62
      this._types[ext] = type;
63
    }
64
65
    // Use first extension as default
66
    if (force || !this._extensions[type]) {
67
      var ext = extensions[0];
68
      this._extensions[type] = (ext[0] != '*') ? ext : ext.substr(1)
69
    }
70
  }
71
};
72
73
/**
74
 * Lookup a mime type based on extension
75
 */
76
Mime.prototype.getType = function(path) {
77
  path = String(path);
78
  var last = path.replace(/^.*[/\\]/, '').toLowerCase();
79
  var ext = last.replace(/^.*\./, '').toLowerCase();
80
81
  var hasPath = last.length < path.length;
82
  var hasDot = ext.length < last.length - 1;
83
84
  return (hasDot || !hasPath) && this._types[ext] || null;
85
};
86
87
/**
88
 * Return file extension associated with a mime type
89
 */
90
Mime.prototype.getExtension = function(type) {
91
  type = /^\s*([^;\s]*)/.test(type) && RegExp.$1;
92
  return type && this._extensions[type.toLowerCase()] || null;
93
};
94
95
module.exports = Mime;