From df2d6f0b0be7a269ea6c0ee70372e17854fc3c6c Mon Sep 17 00:00:00 2001 From: Rudy Mizrahi Celekli <47457359+rudycelekli@users.noreply.github.com> Date: Sat, 28 Nov 2026 06:26:37 -0600 Subject: [PATCH] fix(reading): preserve original extensions on long multilingual filenames (#2780) * fix(reading): fit original filenames within the UTF-8 limit * fix(reading): shorten the temp upload name the same way The upload is first written to a temp file named after the upload. A 100-character Chinese name is 300 UTF-8 bytes, so on Linux the request failed with "File name too long" before the stored-name limit added here was ever reached. * chore(api): refresh router index for the reading upload change --------- Co-authored-by: zszz3 <91608029+zszz3@users.noreply.github.com> --- deeptutor/api/router_index.json | 2 +- deeptutor/api/routers/reading.py | 7 ++++++- deeptutor/reading/store.py | 5 ++++- tests/reading/test_router.py | 14 ++++++++++++++ tests/reading/test_safe_filename.py | 13 +++++++++++++ 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/deeptutor/api/router_index.json b/deeptutor/api/router_index.json index ca6385f76..e84de6211 100644 --- a/deeptutor/api/router_index.json +++ b/deeptutor/api/router_index.json @@ -1,5 +1,5 @@ { - "fingerprint": "b07f96622b2ee82aefdf285bc45374836cb0b01e4119ce2ae0500a2c05448b06", + "fingerprint": "ee4e2365b3d6f6d8a2702f903d6b8264cdeb4171de7edd898992902145585f3a", "routers": { "deeptutor.api.routers.auth:router": [ { diff --git a/deeptutor/api/routers/reading.py b/deeptutor/api/routers/reading.py index ac402a3a5..eb92040e1 100644 --- a/deeptutor/api/routers/reading.py +++ b/deeptutor/api/routers/reading.py @@ -62,6 +62,7 @@ from deeptutor.reading.knowledge_capture import ( send_workspace_to_notebook, ) from deeptutor.reading.models import MAX_TEXT_SELECTOR_CHARS +from deeptutor.reading.store import _safe_filename from deeptutor.services.session.workspace_preferences import WORKSPACE_MODE_READING from deeptutor.utils.document_validator import DocumentValidator @@ -1062,7 +1063,11 @@ async def upload_material( filename = filename[: -len(".zip")] tmp_dir = Path(tempfile.mkdtemp(prefix="dt-reading-")) - tmp_path = tmp_dir / Path(filename).name + # The temp copy only needs the right suffix; the display name is passed on + # separately. A long multilingual name can exceed the 255-byte filename + # limit of most Linux filesystems, so it is shortened the same way the + # stored original is. + tmp_path = tmp_dir / _safe_filename(Path(filename).name, fallback="upload") written = 0 try: with tmp_path.open("wb") as sink: diff --git a/deeptutor/reading/store.py b/deeptutor/reading/store.py index 0c5884dd9..15c916aa5 100644 --- a/deeptutor/reading/store.py +++ b/deeptutor/reading/store.py @@ -1560,6 +1560,7 @@ def _carry_dir(source: Path, target: Path) -> None: _MAX_FILENAME_CHARS = 180 +_MAX_FILENAME_BYTES = 255 def _safe_filename(name: str, *, fallback: str) -> str: @@ -1574,7 +1575,7 @@ def _safe_filename(name: str, *, fallback: str) -> str: if os.altsep: base = base.replace(os.altsep, "_") base = base.strip(". ") or Path(fallback).name or "material" - if len(base) <= _MAX_FILENAME_CHARS: + if len(base) <= _MAX_FILENAME_CHARS and len(base.encode("utf-8")) <= _MAX_FILENAME_BYTES: return base # Cut the stem, not the extension: the stored original is found again by # its suffix (a PDF is only re-extracted when the raw file ends in .pdf). @@ -1582,6 +1583,8 @@ def _safe_filename(name: str, *, fallback: str) -> str: if len(suffix) > 16: suffix = "" stem = base[: len(base) - len(suffix)][: _MAX_FILENAME_CHARS - len(suffix)] + byte_budget = _MAX_FILENAME_BYTES - len(suffix.encode("utf-8")) + stem = stem.encode("utf-8")[:byte_budget].decode("utf-8", errors="ignore") return stem.rstrip(". ") + suffix diff --git a/tests/reading/test_router.py b/tests/reading/test_router.py index 2a62f1698..972fd5b33 100644 --- a/tests/reading/test_router.py +++ b/tests/reading/test_router.py @@ -960,3 +960,17 @@ def test_collection_color_round_trips_through_create_and_patch(client: TestClien json={"title": "Slow reading", "color": "green"}, ).json()["workspace"] assert (patched["title"], patched["color"]) == ("Slow reading", "green") + + +def test_upload_with_a_long_multilingual_filename_is_stored(client: TestClient) -> None: + # 100 CJK characters are 300 UTF-8 bytes, past the 255-byte name limit of + # ext4 and most Linux filesystems; the upload must not use it verbatim. + name = "课" * 100 + ".pdf" + + body = _upload(client, name=name) + + assert body["filename"] == name + assert body["has_raw_view"] is True + raw = client.get(f"/api/reading/materials/{body['material_id']}/raw") + assert raw.status_code == 200 + assert raw.content.startswith(b"%PDF") diff --git a/tests/reading/test_safe_filename.py b/tests/reading/test_safe_filename.py index 204d2ea22..b6f52f4a3 100644 --- a/tests/reading/test_safe_filename.py +++ b/tests/reading/test_safe_filename.py @@ -2,6 +2,8 @@ from __future__ import annotations +from pathlib import Path + from deeptutor.reading.store import _safe_filename @@ -30,3 +32,14 @@ def test_overlong_suffix_is_cut_like_the_rest_of_the_name() -> None: name = "paper." + "x" * 300 assert _safe_filename(name, fallback="material") == name[:180] + + +def test_long_utf8_name_fits_on_disk_and_keeps_extension(tmp_path: Path) -> None: + name = "课" * 100 + ".pdf" + + stored = _safe_filename(name, fallback="material") + + assert stored == "课" * 83 + ".pdf" + raw_file = tmp_path / stored + raw_file.write_bytes(b"original") + assert raw_file.read_bytes() == b"original"