Files
VoiceStudio/backend/services/text_upload.py
T
kevin9327andClaude Opus 5 28ef19dcc6 fix(import): read UTF-32 marks, and survive a non-text declaration
Two Greptile P1s on this PR, both real:

- A UTF-32 LE byte-order mark starts with the UTF-16 LE one, so the BOM
  table matched UTF-16 first, stripped two bytes and read the file as
  NUL-interleaved UTF-16; a UTF-32 BE mark matched nothing and fell through
  to Windows-1252. The table lives in services.text_upload and is shared, so
  /dub/import-srt and the .txt/.md branch of /audiobook/import carried the
  same corruption -- it is fixed once, longest mark first, and both front
  doors and the EPUB path inherit it.
- A declaration such as encoding="hex_codec" passes codecs.lookup but
  bytes.decode refuses it with LookupError, which would have failed the
  whole EPUB import. The codecs.lookup pre-check is gone: the decode itself
  decides, and an encoding that cannot produce text now guesses like an
  undeclared document does.

text_upload gains bom_encoding() so the EPUB path asks the one table rather
than keeping a second copy of it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-18 07:44:30 +09:00

70 lines
2.4 KiB
Python

"""Decode an uploaded text file whose encoding nobody declared.
Subtitle and manuscript uploads arrive as raw bytes. Windows tools commonly
save them as UTF-16 with a byte-order mark (Notepad's "Unicode", many subtitle
editors) or in the legacy Windows-1252 code page. A UTF-8 decode turns the
first into NUL-interleaved text and, lossily, drops every accent, dash and
curly quote from the second.
"""
from __future__ import annotations
import codecs
# Longest-first: the UTF-32 LE mark starts with the UTF-16 LE one, so checking
# UTF-16 first would strip two bytes and read a UTF-32 file as NUL-interleaved
# UTF-16.
_BOMS = (
(codecs.BOM_UTF32_LE, "utf-32-le"),
(codecs.BOM_UTF32_BE, "utf-32-be"),
(codecs.BOM_UTF8, "utf-8"),
(codecs.BOM_UTF16_LE, "utf-16-le"),
(codecs.BOM_UTF16_BE, "utf-16-be"),
)
_CP1252_UNDEFINED = "voicestudio-cp1252-undefined"
def _undefined_as_latin1(exc: UnicodeDecodeError) -> tuple[str, int]:
# Only the offending bytes take their Latin-1 code point — what the
# browser's windows-1252 decoder does — so the rest of the file keeps its
# curly quotes and dashes.
return exc.object[exc.start:exc.end].decode("latin-1"), exc.end
codecs.register_error(_CP1252_UNDEFINED, _undefined_as_latin1)
def _bom(data: bytes) -> tuple[bytes, str] | None:
for bom, encoding in _BOMS:
if data.startswith(bom):
return bom, encoding
return None
def bom_encoding(data: bytes) -> str | None:
"""The encoding a leading byte-order mark names, or ``None``.
One table, so every caller that needs to know "does this file say what it
is?" asks the same question :func:`decode_text_upload` answers.
"""
found = _bom(data)
return found[1] if found else None
def decode_text_upload(data: bytes) -> str:
"""Return the text of ``data``, without its byte-order mark.
A BOM names the encoding. Without one, valid UTF-8 is UTF-8; anything else
is read as Windows-1252, the legacy code page such files come from. Each of
the five bytes Windows-1252 leaves undefined takes its Latin-1 code point,
so the decode never raises.
"""
found = _bom(data)
if found:
bom, encoding = found
return data[len(bom):].decode(encoding, errors="replace")
try:
return data.decode("utf-8")
except UnicodeDecodeError:
return data.decode("cp1252", errors=_CP1252_UNDEFINED)