Supporting chunks as strings

This commit is contained in:
Darren Burns
2024-08-14 14:19:28 +01:00
parent f6be9157ce
commit fc946f6008
4 changed files with 11 additions and 7 deletions

View File

@@ -18,7 +18,9 @@ class ScreenshotApp(App[None]):
def action_deliver_screenshot(self) -> None:
screenshot_string = self.export_screenshot()
string_io = io.StringIO(screenshot_string)
self.deliver_text(string_io)
self.deliver_text(
string_io, save_filename="screenshot.svg", open_method="browser"
)
app = ScreenshotApp()

View File

@@ -13,8 +13,6 @@ import logging
from importlib.metadata import version
import uuid
import rich.repr
from textual_serve.download_manager import DownloadManager
log = logging.getLogger("textual-serve")
@@ -342,5 +340,5 @@ class AppService:
if unpacked[0] == "deliver_chunk":
# If we receive a chunk, hand it to the download manager to
# handle distribution to the browser.
_, delivery_key, chunk_bytes = unpacked
await self._download_manager.chunk_received(delivery_key, chunk_bytes)
_, delivery_key, chunk = unpacked
await self._download_manager.chunk_received(delivery_key, chunk)

View File

@@ -108,7 +108,7 @@ class DownloadManager:
incoming_chunks.task_done()
yield chunk
async def chunk_received(self, delivery_key: str, chunk: bytes) -> None:
async def chunk_received(self, delivery_key: str, chunk: bytes | str) -> None:
"""Handle a chunk received from the app service for a download.
Args:
@@ -116,6 +116,8 @@ class DownloadManager:
chunk: The chunk that was received.
"""
download = self._active_downloads[delivery_key]
if isinstance(chunk, str):
chunk = chunk.encode(download.encoding or "utf-8")
await download.incoming_chunks.put(chunk)
async def _get_app_service(self, delivery_key: str) -> "AppService":

View File

@@ -170,7 +170,9 @@ class Server:
content_type += f"; charset={download_meta.encoding}"
response.headers["Content-Type"] = content_type
disposition = "inline" if download_meta.open_method == "download" else "inline"
disposition = (
"inline" if download_meta.open_method == "browser" else "attachment"
)
response.headers["Content-Disposition"] = (
f"{disposition}; filename={download_meta.file_name}"
)