Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions backend/chainlit/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class ElementDict(TypedDict, total=False):
playerConfig: Optional[dict]
forId: Optional[str]
mime: Optional[str]
downloadName: Optional[str]


@dataclass
Expand Down Expand Up @@ -96,6 +97,8 @@ class Element:
language: Optional[str] = None
# Mime type, inferred based on content if not provided
mime: Optional[str] = None
# Custom download filename. If set, this name is used when the file is downloaded.
download_name: Optional[str] = None

def __post_init__(self) -> None:
self.persisted = False
Expand Down Expand Up @@ -123,6 +126,7 @@ def to_dict(self) -> ElementDict:
"language": getattr(self, "language", None),
"forId": getattr(self, "for_id", None),
"mime": getattr(self, "mime", None),
"downloadName": getattr(self, "download_name", None),
}
)
return _dict
Expand All @@ -149,6 +153,7 @@ def from_dict(cls, e_dict: ElementDict):
chainlit_key = e_dict.get("chainlitKey")
display = e_dict.get("display", "inline")
mime_type = e_dict.get("mime", "")
download_name = e_dict.get("downloadName")

# Common parameters for all element types
common_params = {
Expand All @@ -162,6 +167,7 @@ def from_dict(cls, e_dict: ElementDict):
"chainlit_key": chainlit_key,
"display": display,
"mime": mime_type,
"download_name": download_name,
}

if type == "image":
Expand Down Expand Up @@ -218,6 +224,7 @@ async def _create(self, persist=True) -> bool:
path=self.path,
content=self.content,
mime=self.mime or "",
download_name=self.download_name,
)
self.chainlit_key = file_dict["id"]

Expand Down
8 changes: 7 additions & 1 deletion backend/chainlit/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1657,7 +1657,13 @@ async def get_file(

if file_id in session.files:
file = session.files[file_id]
return FileResponse(file["path"], media_type=file["type"])
filename = file.get("download_name") or file.get("name")
return FileResponse(
file["path"],
media_type=file["type"],
filename=filename,
content_disposition_type="inline",
)
else:
raise HTTPException(status_code=404, detail="File not found")

Expand Down
2 changes: 2 additions & 0 deletions backend/chainlit/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ async def persist_file(
mime: str,
path: Optional[str] = None,
content: Optional[Union[bytes, str]] = None,
download_name: Optional[str] = None,
) -> FileReference:
if not path and not content:
raise ValueError(
Expand Down Expand Up @@ -140,6 +141,7 @@ async def persist_file(
"name": name,
"type": mime,
"size": file_size,
"download_name": download_name,
}

return {"id": file_id}
Expand Down