sitegen/framework/mime.ts

31 lines
782 B
TypeScript
Raw Normal View History

const db = new Map(
fs.readFileSync(path.join(import.meta.dirname, "mime.txt"), "utf8")
.split("\n").filter(Boolean).map((line) =>
line.split(/\s+/) as [string, string]
),
);
/**
* Accepts:
* - Full file path
* - Extension (with or without dot)
*/
export function contentTypeFor(file: string) {
if (file.includes("/") || file.includes("\\")) {
// Some file names are special cased.
switch (path.basename(file)) {
case "rss.xml":
return "application/rss+xml";
}
file = path.extname(file);
}
const dot = file.indexOf(".");
if (dot === -1) file = "." + file;
else if (dot > 0) file = file.slice(dot);
return db.get(file) ?? "application/octet-stream";
}
import * as fs from "./fs.ts";
import * as path from "node:path";