|
| 1 | +/** |
| 2 | + * 根据文件扩展名获取 MIME 类型 |
| 3 | + * @param filename 文件名 |
| 4 | + * @returns MIME 类型 |
| 5 | + */ |
| 6 | +export function getMimeTypeFromFilename(filename: string): string { |
| 7 | + const ext = filename.split('.').pop()?.toLowerCase() || '' |
| 8 | + |
| 9 | + const mimeTypes: Record<string, string> = { |
| 10 | + // 图片 |
| 11 | + jpg: 'image/jpeg', |
| 12 | + jpeg: 'image/jpeg', |
| 13 | + png: 'image/png', |
| 14 | + gif: 'image/gif', |
| 15 | + webp: 'image/webp', |
| 16 | + svg: 'image/svg+xml', |
| 17 | + ico: 'image/x-icon', |
| 18 | + bmp: 'image/bmp', |
| 19 | + tiff: 'image/tiff', |
| 20 | + tif: 'image/tiff', |
| 21 | + |
| 22 | + // 视频 |
| 23 | + mp4: 'video/mp4', |
| 24 | + webm: 'video/webm', |
| 25 | + ogv: 'video/ogg', |
| 26 | + avi: 'video/x-msvideo', |
| 27 | + mov: 'video/quicktime', |
| 28 | + wmv: 'video/x-ms-wmv', |
| 29 | + flv: 'video/x-flv', |
| 30 | + mkv: 'video/x-matroska', |
| 31 | + |
| 32 | + // 音频 |
| 33 | + mp3: 'audio/mpeg', |
| 34 | + wav: 'audio/wav', |
| 35 | + ogg: 'audio/ogg', |
| 36 | + oga: 'audio/ogg', |
| 37 | + m4a: 'audio/mp4', |
| 38 | + flac: 'audio/flac', |
| 39 | + aac: 'audio/aac', |
| 40 | + |
| 41 | + // 文档 |
| 42 | + pdf: 'application/pdf', |
| 43 | + doc: 'application/msword', |
| 44 | + docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
| 45 | + xls: 'application/vnd.ms-excel', |
| 46 | + xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
| 47 | + ppt: 'application/vnd.ms-powerpoint', |
| 48 | + pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
| 49 | + txt: 'text/plain', |
| 50 | + csv: 'text/csv', |
| 51 | + rtf: 'application/rtf', |
| 52 | + |
| 53 | + // 压缩文件 |
| 54 | + zip: 'application/zip', |
| 55 | + rar: 'application/x-rar-compressed', |
| 56 | + '7z': 'application/x-7z-compressed', |
| 57 | + tar: 'application/x-tar', |
| 58 | + gz: 'application/gzip', |
| 59 | + bz2: 'application/x-bzip2', |
| 60 | + |
| 61 | + // Web 文件 |
| 62 | + html: 'text/html', |
| 63 | + htm: 'text/html', |
| 64 | + css: 'text/css', |
| 65 | + js: 'application/javascript', |
| 66 | + json: 'application/json', |
| 67 | + xml: 'application/xml', |
| 68 | + |
| 69 | + // 其他常见格式 |
| 70 | + wasm: 'application/wasm', |
| 71 | + ttf: 'font/ttf', |
| 72 | + otf: 'font/otf', |
| 73 | + woff: 'font/woff', |
| 74 | + woff2: 'font/woff2', |
| 75 | + eot: 'application/vnd.ms-fontobject', |
| 76 | + } |
| 77 | + |
| 78 | + return mimeTypes[ext] || 'application/octet-stream' |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * 从响应头中获取内容类型,如果没有则根据文件名推断 |
| 83 | + * @param headers Response Headers 对象 |
| 84 | + * @param filename 文件名 |
| 85 | + * @returns MIME 类型 |
| 86 | + */ |
| 87 | +export function getContentType(headers: Headers, filename: string): string { |
| 88 | + const contentType = headers.get('content-type') |
| 89 | + if (contentType) { |
| 90 | + return contentType |
| 91 | + } |
| 92 | + // 如果服务器没有返回 content-type,根据文件扩展名推断 |
| 93 | + return getMimeTypeFromFilename(filename) |
| 94 | +} |
0 commit comments