from __future__ import annotations from pathlib import Path TextFileTypes = {"markdown", "text", "config", "html"} MimeTypeMap = { ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif", ".webp": "image/webp", ".svg": "image/svg+xml", ".mp4": "video/mp4", ".webm": "video/webm", ".mov": "video/quicktime", ".mkv": "video/x-matroska", } def ReadTextFile(FilePath: Path) -> str: """Read a text-classified file as UTF-8.""" return FilePath.read_text(encoding="utf-8") def GetFileSize(FilePath: Path) -> int: return FilePath.stat().st_size def GetMimeType(FilePath: Path) -> str: return MimeTypeMap.get(FilePath.suffix.lower(), "application/octet-stream") def ParseRangeHeader(RangeHeader: str, FileSize: int) -> tuple[int, int]: """Parse a single-range HTTP Range header into an inclusive (Start, End).""" RangeValue = RangeHeader.replace("bytes=", "").strip() StartText, _, EndText = RangeValue.partition("-") Start = int(StartText) if StartText else 0 End = int(EndText) if EndText else FileSize - 1 End = min(End, FileSize - 1) return Start, End