* Handle a generic text file buffer in memory
( fileBuffer: Buffer, filename: string, extension: string, fileType?: string, originalPath?: string, maxParsedOutputBytes?: number )
| 1014 | * Handle a generic text file buffer in memory |
| 1015 | */ |
| 1016 | async function handleGenericTextBuffer( |
| 1017 | fileBuffer: Buffer, |
| 1018 | filename: string, |
| 1019 | extension: string, |
| 1020 | fileType?: string, |
| 1021 | originalPath?: string, |
| 1022 | maxParsedOutputBytes?: number |
| 1023 | ): Promise<ParseResult> { |
| 1024 | try { |
| 1025 | logger.info(`Parsing text file in memory: ${filename}`) |
| 1026 | |
| 1027 | try { |
| 1028 | const { parseBuffer, isSupportedFileType } = await import('@/lib/file-parsers') |
| 1029 | |
| 1030 | if (isSupportedFileType(extension)) { |
| 1031 | const result = await parseBuffer(fileBuffer, extension) |
| 1032 | |
| 1033 | return { |
| 1034 | success: true, |
| 1035 | content: assertParsedContentWithinLimit(result.content, maxParsedOutputBytes), |
| 1036 | filePath: originalPath || filename, |
| 1037 | metadata: { |
| 1038 | fileType: fileType || getMimeTypeFromExtension(extension), |
| 1039 | size: fileBuffer.length, |
| 1040 | hash: createHash('md5').update(fileBuffer).digest('hex'), |
| 1041 | processingTime: 0, |
| 1042 | }, |
| 1043 | } |
| 1044 | } |
| 1045 | } catch (parserError) { |
| 1046 | if (isPayloadSizeLimitError(parserError)) throw parserError |
| 1047 | |
| 1048 | logger.warn('Specialized parser failed, falling back to generic parsing:', parserError) |
| 1049 | } |
| 1050 | |
| 1051 | const content = fileBuffer.toString('utf-8') |
| 1052 | const limitedContent = assertParsedContentWithinLimit(content, maxParsedOutputBytes) |
| 1053 | |
| 1054 | return { |
| 1055 | success: true, |
| 1056 | content: limitedContent, |
| 1057 | filePath: originalPath || filename, |
| 1058 | metadata: { |
| 1059 | fileType: fileType || getMimeTypeFromExtension(extension), |
| 1060 | size: fileBuffer.length, |
| 1061 | hash: createHash('md5').update(fileBuffer).digest('hex'), |
| 1062 | processingTime: 0, |
| 1063 | }, |
| 1064 | } |
| 1065 | } catch (error) { |
| 1066 | if (isPayloadSizeLimitError(error)) throw error |
| 1067 | |
| 1068 | logger.error('Failed to parse text file in memory:', error) |
| 1069 | return { |
| 1070 | success: false, |
| 1071 | error: `Failed to parse file: ${(error as Error).message}`, |
| 1072 | filePath: originalPath || filename, |
| 1073 | metadata: { |
no test coverage detected