[6a3a178] | 1 | '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 (let 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 (let type in typeMap) {
|
---|
| 41 | let extensions = typeMap[type].map(function(t) {
|
---|
| 42 | return t.toLowerCase();
|
---|
| 43 | });
|
---|
| 44 | type = type.toLowerCase();
|
---|
| 45 |
|
---|
| 46 | for (let i = 0; i < extensions.length; i++) {
|
---|
| 47 | const ext = extensions[i];
|
---|
| 48 |
|
---|
| 49 | // '*' prefix = not the preferred type for this extension. So fixup the
|
---|
| 50 | // extension, and skip it.
|
---|
| 51 | if (ext[0] === '*') {
|
---|
| 52 | continue;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | if (!force && (ext in this._types)) {
|
---|
| 56 | throw new Error(
|
---|
| 57 | 'Attempt to change mapping for "' + ext +
|
---|
| 58 | '" extension from "' + this._types[ext] + '" to "' + type +
|
---|
| 59 | '". Pass `force=true` to allow this, otherwise remove "' + ext +
|
---|
| 60 | '" from the list of extensions for "' + type + '".'
|
---|
| 61 | );
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | this._types[ext] = type;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | // Use first extension as default
|
---|
| 68 | if (force || !this._extensions[type]) {
|
---|
| 69 | const ext = extensions[0];
|
---|
| 70 | this._extensions[type] = (ext[0] !== '*') ? ext : ext.substr(1);
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | };
|
---|
| 74 |
|
---|
| 75 | /**
|
---|
| 76 | * Lookup a mime type based on extension
|
---|
| 77 | */
|
---|
| 78 | Mime.prototype.getType = function(path) {
|
---|
| 79 | path = String(path);
|
---|
| 80 | let last = path.replace(/^.*[/\\]/, '').toLowerCase();
|
---|
| 81 | let ext = last.replace(/^.*\./, '').toLowerCase();
|
---|
| 82 |
|
---|
| 83 | let hasPath = last.length < path.length;
|
---|
| 84 | let hasDot = ext.length < last.length - 1;
|
---|
| 85 |
|
---|
| 86 | return (hasDot || !hasPath) && this._types[ext] || null;
|
---|
| 87 | };
|
---|
| 88 |
|
---|
| 89 | /**
|
---|
| 90 | * Return file extension associated with a mime type
|
---|
| 91 | */
|
---|
| 92 | Mime.prototype.getExtension = function(type) {
|
---|
| 93 | type = /^\s*([^;\s]*)/.test(type) && RegExp.$1;
|
---|
| 94 | return type && this._extensions[type.toLowerCase()] || null;
|
---|
| 95 | };
|
---|
| 96 |
|
---|
| 97 | module.exports = Mime;
|
---|