Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
fix(media): keep upload filenames within the UTF-8 byte limit
Shoaibb4f381ca5581
diff --git a/nanobot/utils/media_decode.py b/nanobot/utils/media_decode.pyindex cc8494bbe..5d39226f4 100644--- a/nanobot/utils/media_decode.py+++ b/nanobot/utils/media_decode.py@@ -92,5 +92,8 @@ def media_destination(media_dir: Path, mime_type: str, *, filename: str | None =ext = _MIME_EXTENSION_OVERRIDES.get(mime_type) or mimetypes.guess_extension(mime_type) or ".bin"base = safe_filename(filename or "")stem = Path(base).stem[:80] if base else ""+ # Leave room for the 12-character ID, separator, and extension.+ max_stem_bytes = 255 - 13 - len(ext.encode("utf-8"))+ stem = stem.encode("utf-8")[:max_stem_bytes].decode("utf-8", errors="ignore")saved_name = f"{uuid.uuid4().hex[:12]}_{stem}{ext}" if stem else f"{uuid.uuid4().hex[:12]}{ext}"return media_dir / safe_filename(saved_name)diff --git a/tests/utils/test_media_decode.py b/tests/utils/test_media_decode.pyindex cbe6cd187..3df6bfa5c 100644--- a/tests/utils/test_media_decode.py+++ b/tests/utils/test_media_decode.py@@ -62,6 +62,24 @@ def test_saves_document_with_safe_original_name(tmp_path) -> None:assert Path(result).read_bytes() == b"%PDF-1.4"[email protected]("stem", ["报告" * 40, "📄" * 60], ids=["cjk", "emoji"])+def test_saves_document_with_long_unicode_name(tmp_path: Path, stem: str) -> None:+ payload = b"%PDF-1.4"+ result = save_base64_data_url(+ _data_url(payload, mime="application/pdf"),+ tmp_path,+ filename=f"{stem}.pdf",+ )++ assert result is not None+ saved = Path(result)+ assert saved.parent == tmp_path+ assert saved.suffix == ".pdf"+ assert len(saved.name.encode("utf-8")) <= 255+ assert "�" not in saved.name+ assert saved.read_bytes() == payload++def test_document_filename_cannot_escape_media_dir(tmp_path) -> None:result = save_base64_data_url(_data_url(b"%PDF-1.4", mime="application/pdf"),