Corp/frontend/src/lib/document-preview.ts

101 lines
1.4 KiB
TypeScript

const DOCUMENT_PREVIEW_EXTENSIONS = new Set([
"pdf",
"jpg",
"jpeg",
"png",
"gif",
"webp",
"svg",
"bmp",
"ico",
"avif",
"docx",
"xls",
"xlsx",
"xlsm",
"xlsb",
"ods",
"csv",
"tsv",
"txt",
"text",
"md",
"markdown",
"json",
"xml",
"html",
"htm",
"css",
"scss",
"sass",
"less",
"js",
"jsx",
"ts",
"tsx",
"mjs",
"cjs",
"yml",
"yaml",
"ini",
"conf",
"cfg",
"log",
"sql",
"py",
"java",
"c",
"cpp",
"h",
"hpp",
"cs",
"go",
"rs",
"php",
"rb",
"kt",
"kts",
"swift",
"sh",
"bash",
"zsh",
"ps1",
"bat",
"cmd",
"properties",
"env",
"rtf",
"mp4",
"webm",
"ogg",
"ogv",
"mov",
"m4v",
"mp3",
"wav",
"oga",
"m4a",
"aac",
"flac",
]);
const DOCUMENT_PREVIEW_FILE_NAMES = new Set([
"dockerfile",
"makefile",
"readme",
"license",
".gitignore",
".env",
]);
export function getDocumentFileExtension(fileName?: string | null): string {
const cleanName = (fileName || "").toLowerCase().split("?")[0].split("#")[0];
return cleanName.match(/\.([^.]+)$/)?.[1] || "";
}
export function canPreviewDocument(fileName?: string | null): boolean {
const lowerName = (fileName || "").toLowerCase();
const extension = getDocumentFileExtension(fileName);
return DOCUMENT_PREVIEW_EXTENSIONS.has(extension) || DOCUMENT_PREVIEW_FILE_NAMES.has(lowerName);
}