From 602efc3bc3e2539b31f1eacb049132ab05cf25c3 Mon Sep 17 00:00:00 2001 From: Zihan Xu <2024010904024@std.uestc.edu.cn> Date: Tue, 22 Sep 2026 21:44:00 +0800 Subject: [PATCH] fix(files): decode BOM-marked text correctly --- nanobot/utils/document.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/nanobot/utils/document.py b/nanobot/utils/document.py index c87c63ab5..136e9e73f 100644 --- a/nanobot/utils/document.py +++ b/nanobot/utils/document.py @@ -1,5 +1,6 @@ """Document text extraction utilities for nanobot.""" +import codecs import mimetypes from collections.abc import Iterator from dataclasses import dataclass @@ -603,14 +604,28 @@ def _office_archive_error(path: Path) -> str | None: return None +def _decode_bom_text(raw: bytes) -> str | None: + """Decode text whose byte-order mark declares a Unicode encoding.""" + if raw.startswith(codecs.BOM_UTF8): + return raw.decode("utf-8-sig") + # UTF-32 LE starts with the UTF-16 LE BOM, so check UTF-32 first. + if raw.startswith((codecs.BOM_UTF32_LE, codecs.BOM_UTF32_BE)): + return raw.decode("utf-32") + if raw.startswith((codecs.BOM_UTF16_LE, codecs.BOM_UTF16_BE)): + return raw.decode("utf-16") + return None + + def _extract_text_file(path: Path) -> str: """Extract text from a plain text file.""" try: - # Try UTF-8 first, then latin-1 fallback - try: - content = path.read_text(encoding="utf-8") - except UnicodeDecodeError: - content = path.read_text(encoding="latin-1") + raw = path.read_bytes() + content = _decode_bom_text(raw) + if content is None: + try: + content = raw.decode("utf-8") + except UnicodeDecodeError: + content = raw.decode("latin-1") return _truncate(content, _MAX_TEXT_LENGTH) except Exception as e: logger.exception("Failed to read text file {}", path)