from __future__ import annotations from pathlib import Path TEXT_ENCODINGS = ("utf-8-sig", "utf-8", "gbk", "latin-1") DEFAULT_TEXT_PREVIEW_BYTES = 2 * 1024 * 1024 DEFAULT_SEARCH_INDEX_BYTES = 512 * 1024 TRUNCATED_TEXT_NOTICE = "\n\n[MDHelper: 文件较大,已截断预览内容。]" def file_size(path: Path) -> int | None: try: return path.stat().st_size except OSError: return None def is_within_size_limit(path: Path, max_bytes: int) -> bool: size = file_size(path) return size is None or size <= max_bytes def read_text_with_fallback(path: Path, max_bytes: int | None = None) -> str: try: data = path.read_bytes() if max_bytes is None else _read_limited_bytes(path, max_bytes) except OSError as exc: return f"读取失败:{exc}" truncated = max_bytes is not None and len(data) > max_bytes if truncated: data = data[:max_bytes] for encoding in TEXT_ENCODINGS: try: text = data.decode(encoding) break except UnicodeDecodeError: continue else: text = data.decode("utf-8", errors="replace") if truncated: text += TRUNCATED_TEXT_NOTICE return text def _read_limited_bytes(path: Path, max_bytes: int) -> bytes: with path.open("rb") as handle: return handle.read(max_bytes + 1)