2024-02-29 19:05:03 +01:00
|
|
|
const MimeLib = require("mime");
|
|
|
|
|
|
|
|
class MimeDetector {
|
|
|
|
nonTextTypes = ["multipart", "image", "model", "audio", "video"];
|
|
|
|
badMimes = [
|
|
|
|
"application/octet-stream",
|
|
|
|
"application/zip",
|
|
|
|
"application/pkcs8",
|
|
|
|
"application/vnd.microsoft.portable-executable",
|
|
|
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // XLSX are binaries and need to be handled explicitly.
|
|
|
|
"application/x-msdownload",
|
|
|
|
];
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.lib = MimeLib;
|
|
|
|
this.setOverrides();
|
|
|
|
}
|
|
|
|
|
|
|
|
setOverrides() {
|
|
|
|
// the .ts extension maps to video/mp2t because of https://en.wikipedia.org/wiki/MPEG_transport_stream
|
|
|
|
// which has had this extension far before TS was invented. So need to force re-map this MIME map.
|
|
|
|
this.lib.define(
|
|
|
|
{
|
2024-04-12 23:54:33 +02:00
|
|
|
"text/plain": [
|
|
|
|
"ts",
|
2024-06-03 11:01:41 +02:00
|
|
|
"tsx",
|
2024-04-12 23:54:33 +02:00
|
|
|
"py",
|
|
|
|
"opts",
|
|
|
|
"lock",
|
|
|
|
"jsonl",
|
|
|
|
"qml",
|
|
|
|
"sh",
|
|
|
|
"c",
|
|
|
|
"cs",
|
|
|
|
"h",
|
|
|
|
"js",
|
|
|
|
"lua",
|
|
|
|
"pas",
|
2024-05-31 07:52:00 +02:00
|
|
|
"r",
|
2024-08-10 04:29:29 +02:00
|
|
|
"go",
|
2024-04-12 23:54:33 +02:00
|
|
|
],
|
2024-02-29 19:05:03 +01:00
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getType(filepath) {
|
|
|
|
return this.lib.getType(filepath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
MimeDetector,
|
|
|
|
};
|