(buffer: Buffer)
| 33 | } |
| 34 | |
| 35 | async parseBuffer(buffer: Buffer): Promise<FileParseResult> { |
| 36 | try { |
| 37 | if (!buffer || buffer.length === 0) { |
| 38 | throw new Error('Empty buffer provided') |
| 39 | } |
| 40 | |
| 41 | assertOoxmlArchiveWithinLimits(buffer) |
| 42 | |
| 43 | try { |
| 44 | const result = await mammoth.extractRawText({ buffer }) |
| 45 | |
| 46 | if (result.value && result.value.trim().length > 0) { |
| 47 | let htmlResult: MammothResult = { value: '', messages: [] } |
| 48 | try { |
| 49 | htmlResult = await mammoth.convertToHtml({ buffer }) |
| 50 | } catch { |
| 51 | // HTML conversion is optional |
| 52 | } |
| 53 | |
| 54 | return { |
| 55 | content: sanitizeTextForUTF8(result.value), |
| 56 | metadata: { |
| 57 | extractionMethod: 'mammoth', |
| 58 | messages: [...result.messages, ...htmlResult.messages], |
| 59 | html: htmlResult.value, |
| 60 | }, |
| 61 | } |
| 62 | } |
| 63 | } catch (mammothError) { |
| 64 | logger.warn('mammoth failed, trying officeparser:', mammothError) |
| 65 | } |
| 66 | |
| 67 | try { |
| 68 | const officeParser = await import('officeparser') |
| 69 | const result = await officeParser.parseOfficeAsync(buffer) |
| 70 | |
| 71 | if (result) { |
| 72 | const resultString = typeof result === 'string' ? result : String(result) |
| 73 | const content = sanitizeTextForUTF8(resultString.trim()) |
| 74 | |
| 75 | if (content.length > 0) { |
| 76 | return { |
| 77 | content, |
| 78 | metadata: { |
| 79 | extractionMethod: 'officeparser', |
| 80 | characterCount: content.length, |
| 81 | }, |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } catch (officeError) { |
| 86 | logger.warn('officeparser failed:', officeError) |
| 87 | } |
| 88 | |
| 89 | const isZipFile = buffer.length >= 2 && buffer[0] === 0x50 && buffer[1] === 0x4b |
| 90 | if (!isZipFile) { |
| 91 | const textContent = buffer.toString('utf8').trim() |
| 92 | if (textContent.length > 0) { |
no test coverage detected