GeezerVison is showing github.com — fix(server): GB18030 RTF attachments no longer fail text extraction (… · patroza/t3code@f7d6796
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/documentTextWorker.ts +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -506,6 +506,7 @@ const RTF_CODE_PAGES = new Map([ | 506 | 506 | [949, "euc-kr"], | 507 | 507 | [950, "big5"], | 508 | 508 | [10000, "macintosh"], | | 509 | + [54936, "gb18030"], | 509 | 510 | [65001, "utf-8"], | 510 | 511 | ]); | 511 | 512 |
| @@ -546,7 +547,8 @@ function* rtf(source: string): Generator<string, void> { | 546 | 547 | yield String.fromCharCode(byte); | 547 | 548 | return; | 548 | 549 | } | 549 | | - encoded ??= new Uint8Array(Math.min(source.length, MAX_OUTPUT * 3 + 4)); | | 550 | + // GB18030 spends up to 4 bytes per UTF-16 unit, so a full buffer decodes past MAX_OUTPUT. | | 551 | + encoded ??= new Uint8Array(Math.min(source.length, MAX_OUTPUT * 4 + 4)); | 550 | 552 | if (encodedLength === encoded.length) yield* flushBytes(); | 551 | 553 | encoded[encodedLength++] = byte; | 552 | 554 | } | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/userInputAttachments.test.ts +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -375,6 +375,11 @@ describe("question answer paths", () => { | 375 | 375 | "{\\rtf1\\ansi\\ansicpg936 \\'c4\n\\'e3\\'ba\\'c3}", | 376 | 376 | "你好", | 377 | 377 | ], | | 378 | + [ | | 379 | + "GB18030 code page", | | 380 | + String.raw`{\rtf1\ansi\ansicpg54936 \'d6\'d0\'ce\'c4 \'95\'32\'82\'36}`, | | 381 | + "中文 𠀀", | | 382 | + ], | 378 | 383 | ["Big5 code page", String.raw`{\rtf1\ansi\ansicpg950 \'a7A\'a6n}`, "你好"], | 379 | 384 | [ | 380 | 385 | "multibyte font charsets and font code pages", | @@ -434,6 +439,27 @@ describe("question answer paths", () => { | 434 | 439 | }).pipe(Effect.provide(layer)), | 435 | 440 | ); | 436 | 441 |
| | 442 | + it.effect("keeps long GB18030 four-byte RTF runs intact up to the output limit", () => | | 443 | + Effect.gen(function* () { | | 444 | + const config = yield* ServerConfig.ServerConfig; | | 445 | + const path = NodePath.join(config.attachmentsDir, "gb18030-long.rtf"); | | 446 | + NodeFS.writeFileSync( | | 447 | + path, | | 448 | + Buffer.concat([ | | 449 | + Buffer.from(String.raw`{\rtf1\ansi\ansicpg54936 `, "latin1"), | | 450 | + Buffer.from([0xd6, 0xd0]), | | 451 | + Buffer.alloc(1_050_000 * 4, Buffer.from([0x81, 0x39, 0xee, 0x39])), | | 452 | + Buffer.from("}", "latin1"), | | 453 | + ]), | | 454 | + ); | | 455 | + const text = yield* Effect.promise(() => extractDocumentText(path, ".rtf")); | | 456 | + expect(text.search(/[^中㐀]/u)).toBe(1_000_000); | | 457 | + expect(text.slice(1_000_000)).toBe( | | 458 | + "\n\n[Extracted text truncated at document extraction limits.]", | | 459 | + ); | | 460 | + }).pipe(Effect.provide(layer)), | | 461 | + ); | | 462 | + | 437 | 463 | it.effect.each([false, true])("limits the child heap for standalone=%s", (standalone) => | 438 | 464 | Effect.acquireUseRelease( | 439 | 465 | Effect.promise(async () => { | |
|