Skip to content

Commit

Permalink
Allow spaces in avatar filenames. Fixes Chainlit#1370. (Chainlit#1418)
Browse files Browse the repository at this point in the history
  • Loading branch information
dokterbob authored Oct 9, 2024
1 parent 9f31668 commit 970bde2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion backend/chainlit/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ async def get_logo(theme: Optional[Theme] = Query(Theme.light)):
@router.get("/avatars/{avatar_id:str}")
async def get_avatar(avatar_id: str):
"""Get the avatar for the user based on the avatar_id."""
if not re.match(r"^[a-zA-Z0-9_-]+$", avatar_id):
if not re.match(r"^[a-zA-Z0-9_ -]+$", avatar_id):
raise HTTPException(status_code=400, detail="Invalid avatar_id")

if avatar_id == "default":
Expand Down
18 changes: 18 additions & 0 deletions backend/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,24 @@ def test_get_avatar_custom(test_client: TestClient, monkeypatch: pytest.MonkeyPa
os.remove(custom_avatar_path)


def test_get_avatar_with_spaces(
test_client: TestClient, monkeypatch: pytest.MonkeyPatch
):
"""Test with custom avatar."""
custom_avatar_path = os.path.join(APP_ROOT, "public", "avatars", "my_assistant.png")
os.makedirs(os.path.dirname(custom_avatar_path), exist_ok=True)
with open(custom_avatar_path, "wb") as f:
f.write(b"fake image data")

response = test_client.get("/avatars/My Assistant")
assert response.status_code == 200
assert response.headers["content-type"].startswith("image/")
assert response.content == b"fake image data"

# Clean up
os.remove(custom_avatar_path)


def test_get_avatar_non_existent_favicon(
test_client: TestClient, monkeypatch: pytest.MonkeyPatch
):
Expand Down

0 comments on commit 970bde2

Please sign in to comment.