12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- 'use strict';
- function Mime() {
- this._types = Object.create(null);
- this._extensions = Object.create(null);
- for (let i = 0; i < arguments.length; i++) {
- this.define(arguments[i]);
- }
- this.define = this.define.bind(this);
- this.getType = this.getType.bind(this);
- this.getExtension = this.getExtension.bind(this);
- }
- Mime.prototype.define = function(typeMap, force) {
- for (let type in typeMap) {
- let extensions = typeMap[type].map(function(t) {
- return t.toLowerCase();
- });
- type = type.toLowerCase();
- for (let i = 0; i < extensions.length; i++) {
- const ext = extensions[i];
-
-
- if (ext[0] === '*') {
- continue;
- }
- if (!force && (ext in this._types)) {
- throw new Error(
- 'Attempt to change mapping for "' + ext +
- '" extension from "' + this._types[ext] + '" to "' + type +
- '". Pass `force=true` to allow this, otherwise remove "' + ext +
- '" from the list of extensions for "' + type + '".'
- );
- }
- this._types[ext] = type;
- }
-
- if (force || !this._extensions[type]) {
- const ext = extensions[0];
- this._extensions[type] = (ext[0] !== '*') ? ext : ext.substr(1);
- }
- }
- };
- Mime.prototype.getType = function(path) {
- path = String(path);
- let last = path.replace(/^.*[/\\]/, '').toLowerCase();
- let ext = last.replace(/^.*\./, '').toLowerCase();
- let hasPath = last.length < path.length;
- let hasDot = ext.length < last.length - 1;
- return (hasDot || !hasPath) && this._types[ext] || null;
- };
- Mime.prototype.getExtension = function(type) {
- type = /^\s*([^;\s]*)/.test(type) && RegExp.$1;
- return type && this._extensions[type.toLowerCase()] || null;
- };
- module.exports = Mime;
|