Files
smartserve/ts/utils/utils.mime.ts
2025-11-29 15:24:00 +00:00

102 lines
2.6 KiB
TypeScript

/**
* MIME type detection based on file extension
*/
const MIME_TYPES: Record<string, string> = {
// Text
'.html': 'text/html; charset=utf-8',
'.htm': 'text/html; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.js': 'text/javascript; charset=utf-8',
'.mjs': 'text/javascript; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.xml': 'application/xml; charset=utf-8',
'.txt': 'text/plain; charset=utf-8',
'.md': 'text/markdown; charset=utf-8',
'.csv': 'text/csv; charset=utf-8',
'.yaml': 'text/yaml; charset=utf-8',
'.yml': 'text/yaml; charset=utf-8',
// Images
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.webp': 'image/webp',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.bmp': 'image/bmp',
'.avif': 'image/avif',
// Fonts
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.ttf': 'font/ttf',
'.otf': 'font/otf',
'.eot': 'application/vnd.ms-fontobject',
// Audio
'.mp3': 'audio/mpeg',
'.wav': 'audio/wav',
'.ogg': 'audio/ogg',
'.m4a': 'audio/mp4',
'.flac': 'audio/flac',
'.aac': 'audio/aac',
// Video
'.mp4': 'video/mp4',
'.webm': 'video/webm',
'.ogv': 'video/ogg',
'.avi': 'video/x-msvideo',
'.mov': 'video/quicktime',
'.mkv': 'video/x-matroska',
// Documents
'.pdf': 'application/pdf',
'.doc': 'application/msword',
'.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'.xls': 'application/vnd.ms-excel',
'.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'.ppt': 'application/vnd.ms-powerpoint',
'.pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
// Archives
'.zip': 'application/zip',
'.tar': 'application/x-tar',
'.gz': 'application/gzip',
'.rar': 'application/vnd.rar',
'.7z': 'application/x-7z-compressed',
// Source maps
'.map': 'application/json',
// TypeScript
'.ts': 'text/typescript; charset=utf-8',
'.tsx': 'text/typescript; charset=utf-8',
'.d.ts': 'text/typescript; charset=utf-8',
// WebAssembly
'.wasm': 'application/wasm',
// Manifest
'.webmanifest': 'application/manifest+json',
};
/**
* Get MIME type for a file path
*/
export function getMimeType(filePath: string): string {
const ext = filePath.toLowerCase().match(/\.[^.]+$/)?.[0] ?? '';
return MIME_TYPES[ext] ?? 'application/octet-stream';
}
/**
* Check if MIME type is text-based
*/
export function isTextMimeType(mimeType: string): boolean {
return mimeType.startsWith('text/') ||
mimeType.includes('json') ||
mimeType.includes('xml') ||
mimeType.includes('javascript');
}