83 lines
1.9 KiB
JavaScript
83 lines
1.9 KiB
JavaScript
// src/utils/mime.ts
|
|
var getMimeType = (filename, mimes = baseMimes) => {
|
|
const regexp = /\.([a-zA-Z0-9]+?)$/;
|
|
const match = filename.match(regexp);
|
|
if (!match) {
|
|
return;
|
|
}
|
|
let mimeType = mimes[match[1]];
|
|
if (mimeType && mimeType.startsWith("text") || mimeType === "application/json") {
|
|
mimeType += "; charset=utf-8";
|
|
}
|
|
return mimeType;
|
|
};
|
|
var getExtension = (mimeType) => {
|
|
for (const ext in baseMimes) {
|
|
if (baseMimes[ext] === mimeType) {
|
|
return ext;
|
|
}
|
|
}
|
|
};
|
|
var baseMimes = {
|
|
aac: "audio/aac",
|
|
avi: "video/x-msvideo",
|
|
avif: "image/avif",
|
|
av1: "video/av1",
|
|
bin: "application/octet-stream",
|
|
bmp: "image/bmp",
|
|
css: "text/css",
|
|
csv: "text/csv",
|
|
eot: "application/vnd.ms-fontobject",
|
|
epub: "application/epub+zip",
|
|
gif: "image/gif",
|
|
gz: "application/gzip",
|
|
htm: "text/html",
|
|
html: "text/html",
|
|
ico: "image/x-icon",
|
|
ics: "text/calendar",
|
|
jpeg: "image/jpeg",
|
|
jpg: "image/jpeg",
|
|
js: "text/javascript",
|
|
json: "application/json",
|
|
jsonld: "application/ld+json",
|
|
map: "application/json",
|
|
mid: "audio/x-midi",
|
|
midi: "audio/x-midi",
|
|
mjs: "text/javascript",
|
|
mp3: "audio/mpeg",
|
|
mp4: "video/mp4",
|
|
mpeg: "video/mpeg",
|
|
oga: "audio/ogg",
|
|
ogv: "video/ogg",
|
|
ogx: "application/ogg",
|
|
opus: "audio/opus",
|
|
otf: "font/otf",
|
|
pdf: "application/pdf",
|
|
png: "image/png",
|
|
rtf: "application/rtf",
|
|
svg: "image/svg+xml",
|
|
tif: "image/tiff",
|
|
tiff: "image/tiff",
|
|
ts: "video/mp2t",
|
|
ttf: "font/ttf",
|
|
txt: "text/plain",
|
|
wasm: "application/wasm",
|
|
webm: "video/webm",
|
|
weba: "audio/webm",
|
|
webp: "image/webp",
|
|
woff: "font/woff",
|
|
woff2: "font/woff2",
|
|
xhtml: "application/xhtml+xml",
|
|
xml: "application/xml",
|
|
zip: "application/zip",
|
|
"3gp": "video/3gpp",
|
|
"3g2": "video/3gpp2",
|
|
gltf: "model/gltf+json",
|
|
glb: "model/gltf-binary"
|
|
};
|
|
export {
|
|
getExtension,
|
|
getMimeType,
|
|
baseMimes as mimes
|
|
};
|