mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 14:49:46 +08:00
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>
This commit is contained in:
committed by
GitHub
parent
32acc5889a
commit
df2d6f0b0b
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"fingerprint": "b07f96622b2ee82aefdf285bc45374836cb0b01e4119ce2ae0500a2c05448b06",
|
||||
"fingerprint": "ee4e2365b3d6f6d8a2702f903d6b8264cdeb4171de7edd898992902145585f3a",
|
||||
"routers": {
|
||||
"deeptutor.api.routers.auth:router": [
|
||||
{
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user