mirror of
https://github.com/Textualize/textual-serve.git
synced 2025-10-17 02:50:37 +03:00
Supporting chunks as strings
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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":
|
||||
|
||||
@@ -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}"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user