mime.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var mime_exports = {};
  19. __export(mime_exports, {
  20. getExtension: () => getExtension,
  21. getMimeType: () => getMimeType,
  22. mimes: () => baseMimes
  23. });
  24. module.exports = __toCommonJS(mime_exports);
  25. const getMimeType = (filename, mimes = baseMimes) => {
  26. const regexp = /\.([a-zA-Z0-9]+?)$/;
  27. const match = filename.match(regexp);
  28. if (!match) {
  29. return;
  30. }
  31. let mimeType = mimes[match[1].toLowerCase()];
  32. if (mimeType && mimeType.startsWith("text")) {
  33. mimeType += "; charset=utf-8";
  34. }
  35. return mimeType;
  36. };
  37. const getExtension = (mimeType) => {
  38. for (const ext in baseMimes) {
  39. if (baseMimes[ext] === mimeType) {
  40. return ext;
  41. }
  42. }
  43. };
  44. const _baseMimes = {
  45. aac: "audio/aac",
  46. avi: "video/x-msvideo",
  47. avif: "image/avif",
  48. av1: "video/av1",
  49. bin: "application/octet-stream",
  50. bmp: "image/bmp",
  51. css: "text/css",
  52. csv: "text/csv",
  53. eot: "application/vnd.ms-fontobject",
  54. epub: "application/epub+zip",
  55. gif: "image/gif",
  56. gz: "application/gzip",
  57. htm: "text/html",
  58. html: "text/html",
  59. ico: "image/x-icon",
  60. ics: "text/calendar",
  61. jpeg: "image/jpeg",
  62. jpg: "image/jpeg",
  63. js: "text/javascript",
  64. json: "application/json",
  65. jsonld: "application/ld+json",
  66. map: "application/json",
  67. mid: "audio/x-midi",
  68. midi: "audio/x-midi",
  69. mjs: "text/javascript",
  70. mp3: "audio/mpeg",
  71. mp4: "video/mp4",
  72. mpeg: "video/mpeg",
  73. oga: "audio/ogg",
  74. ogv: "video/ogg",
  75. ogx: "application/ogg",
  76. opus: "audio/opus",
  77. otf: "font/otf",
  78. pdf: "application/pdf",
  79. png: "image/png",
  80. rtf: "application/rtf",
  81. svg: "image/svg+xml",
  82. tif: "image/tiff",
  83. tiff: "image/tiff",
  84. ts: "video/mp2t",
  85. ttf: "font/ttf",
  86. txt: "text/plain",
  87. wasm: "application/wasm",
  88. webm: "video/webm",
  89. weba: "audio/webm",
  90. webmanifest: "application/manifest+json",
  91. webp: "image/webp",
  92. woff: "font/woff",
  93. woff2: "font/woff2",
  94. xhtml: "application/xhtml+xml",
  95. xml: "application/xml",
  96. zip: "application/zip",
  97. "3gp": "video/3gpp",
  98. "3g2": "video/3gpp2",
  99. gltf: "model/gltf+json",
  100. glb: "model/gltf-binary"
  101. };
  102. const baseMimes = _baseMimes;
  103. // Annotate the CommonJS export names for ESM import in node:
  104. 0 && (module.exports = {
  105. getExtension,
  106. getMimeType,
  107. mimes
  108. });