You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.8 KiB

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