|
1 | | -const EXTENSION_MIME_MAP: Record<string, string> = { |
2 | | - html: 'text/html', |
3 | | - htm: 'text/html', |
4 | | - md: 'text/markdown', |
5 | | - csv: 'text/csv', |
6 | | - json: 'application/json', |
7 | | - yaml: 'application/x-yaml', |
8 | | - yml: 'application/x-yaml', |
9 | | - xml: 'application/xml', |
10 | | - txt: 'text/plain', |
11 | | -} as const |
12 | | - |
13 | | -/** |
14 | | - * Infers MIME type from a file extension. Returns `text/plain` for unknown extensions. |
15 | | - */ |
16 | | -export function getMimeTypeFromExtension(ext: string): string { |
17 | | - return EXTENSION_MIME_MAP[ext.toLowerCase()] ?? 'text/plain' |
18 | | -} |
| 1 | +import { |
| 2 | + getFileExtension, |
| 3 | + getMimeTypeFromExtension as getUploadMimeType, |
| 4 | +} from '@/lib/uploads/utils/file-utils' |
19 | 5 |
|
20 | 6 | /** |
21 | 7 | * Extracts extension from a filename and returns the normalized filename and MIME type. |
22 | 8 | * If no extension is present, appends `.txt` and uses `text/plain`. |
| 9 | + * Falls back to `text/plain` for unknown extensions (knowledge docs are always text content). |
23 | 10 | */ |
24 | 11 | export function inferDocumentFileInfo(documentName: string): { |
25 | 12 | filename: string |
26 | 13 | mimeType: string |
27 | 14 | } { |
28 | | - const dotIndex = documentName.lastIndexOf('.') |
29 | | - if (dotIndex > 0) { |
30 | | - const ext = documentName.slice(dotIndex + 1).toLowerCase() |
31 | | - return { filename: documentName, mimeType: getMimeTypeFromExtension(ext) } |
| 15 | + const ext = getFileExtension(documentName) |
| 16 | + if (ext) { |
| 17 | + const mimeType = getUploadMimeType(ext) |
| 18 | + return { |
| 19 | + filename: documentName, |
| 20 | + mimeType: mimeType === 'application/octet-stream' ? 'text/plain' : mimeType, |
| 21 | + } |
32 | 22 | } |
33 | 23 | return { filename: `${documentName}.txt`, mimeType: 'text/plain' } |
34 | 24 | } |
|
0 commit comments