2024-02-29 10:05:03 -08:00
|
|
|
const MimeLib = require("mime");
|
|
|
|
class MimeDetector {
|
2025-01-31 13:31:26 -08:00
|
|
|
nonTextTypes = ["multipart", "image", "model", "audio", "video", "font"];
|
2024-02-29 10:05:03 -08:00
|
|
|
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 14:54:33 -07:00
|
|
|
"text/plain": [
|
|
|
|
"ts",
|
2024-06-03 05:01:41 -04:00
|
|
|
"tsx",
|
2024-04-12 14:54:33 -07:00
|
|
|
"py",
|
|
|
|
"opts",
|
|
|
|
"lock",
|
|
|
|
"jsonl",
|
|
|
|
"qml",
|
|
|
|
"sh",
|
|
|
|
"c",
|
|
|
|
"cs",
|
|
|
|
"h",
|
|
|
|
"js",
|
|
|
|
"lua",
|
|
|
|
"pas",
|
2024-05-30 22:52:00 -07:00
|
|
|
"r",
|
2024-08-10 12:29:29 +10:00
|
|
|
"go",
|
2024-10-28 11:44:15 -07:00
|
|
|
"ino",
|
2024-11-20 09:56:03 -08:00
|
|
|
"hpp",
|
|
|
|
"linq",
|
|
|
|
"cs",
|
2024-04-12 14:54:33 -07:00
|
|
|
],
|
2024-02-29 10:05:03 -08:00
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-02 15:13:31 -07:00
|
|
|
/**
|
|
|
|
* Returns the MIME type of the file. If the file has no extension found, it will be processed as a text file.
|
|
|
|
* @param {string} filepath
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2024-02-29 10:05:03 -08:00
|
|
|
getType(filepath) {
|
2024-10-02 15:13:31 -07:00
|
|
|
const parsedMime = this.lib.getType(filepath);
|
|
|
|
if (!!parsedMime) return parsedMime;
|
|
|
|
return null;
|
2024-02-29 10:05:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
MimeDetector,
|
|
|
|
};
|