import path from "node:path"; import { allowedCaseFileExtensions, allowedCaseFileMimeTypes, MAX_CASE_FILE_SIZE_BYTES, } from "@/lib/constants"; export function getFileExtension(fileName: string) { return path.extname(fileName).toLowerCase(); } export function isAllowedCaseFileType(fileName: string, mimeType?: string | null) { const extension = getFileExtension(fileName); return ( allowedCaseFileExtensions.includes( extension as (typeof allowedCaseFileExtensions)[number], ) || (!!mimeType && allowedCaseFileMimeTypes.includes( mimeType as (typeof allowedCaseFileMimeTypes)[number], )) ); } export function isPreviewableMimeType(mimeType: string) { return ( mimeType === "application/pdf" || mimeType.startsWith("image/") || mimeType.startsWith("text/") ); } export function assertCaseFileWithinSizeLimit(sizeBytes: number) { return sizeBytes <= MAX_CASE_FILE_SIZE_BYTES; } export function getSafeMimeType(fileName: string, mimeType?: string | null) { if (mimeType && mimeType !== "application/octet-stream") { return mimeType; } switch (getFileExtension(fileName)) { case ".pdf": return "application/pdf"; case ".jpg": case ".jpeg": return "image/jpeg"; case ".png": return "image/png"; case ".webp": return "image/webp"; case ".gif": return "image/gif"; case ".svg": return "image/svg+xml"; case ".txt": return "text/plain"; case ".csv": return "text/csv"; case ".doc": return "application/msword"; case ".docx": return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; case ".xls": return "application/vnd.ms-excel"; case ".xlsx": return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; case ".ppt": return "application/vnd.ms-powerpoint"; case ".pptx": return "application/vnd.openxmlformats-officedocument.presentationml.presentation"; default: return "application/octet-stream"; } }