Skip to content

Commit

Permalink
Add send_file_download() (#119)
Browse files Browse the repository at this point in the history
* Add `send_file_download()`

* Add link remove

---------

Co-authored-by: Rohan Mathur <[email protected]>
  • Loading branch information
brentyi and THE-COB authored Oct 26, 2023
1 parent 72a1c53 commit a49be3a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/viser/_message_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import base64
import colorsys
import io
import mimetypes
import queue
import threading
import time
Expand Down Expand Up @@ -819,3 +820,17 @@ def add_3d_gui_container(
)
node_handle = SceneNodeHandle._make(self, name, wxyz, position, visible=visible)
return Gui3dContainerHandle(node_handle._impl, gui_api, container_id)

def send_file_download(self, filename: str, content: bytes) -> None:
"""Send a file for a client or clients to download."""
mime_type = mimetypes.guess_type(filename, strict=False)[0]
assert (
mime_type is not None
), f"Could not guess MIME type from filename {filename}!"
self._queue(
_messages.FileDownload(
filename,
content=content,
mime_type=mime_type,
)
)
9 changes: 9 additions & 0 deletions src/viser/_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,3 +565,12 @@ class GetRenderResponseMessage(Message):
"""Message from client->server carrying a render."""

payload: bytes


@dataclasses.dataclass
class FileDownload(Message):
"""Send a file for clients to download."""

filename: str
content: bytes
mime_type: str
11 changes: 10 additions & 1 deletion src/viser/client/src/WebsocketInterface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function useMessageHandler() {
rotation={
// There's redundancy here when we set the side to
// THREE.DoubleSide, where xy and yx should be the same.
//
//
// But it makes sense to keep this parameterization because
// specifying planes by xy seems more natural than the normal
// direction (z, +z, or -z), and it opens the possibility of
Expand Down Expand Up @@ -734,6 +734,15 @@ function useMessageHandler() {
);
return;
}
case "FileDownload": {
const blob = new Blob([message.content], { type: message.mime_type });
const link = document.createElement("a");
link.href = window.URL.createObjectURL(blob);
link.download = message.filename;
link.click();
link.remove();
return;
}
default: {
console.log("Received message did not match any known types:", message);
return;
Expand Down
13 changes: 12 additions & 1 deletion src/viser/client/src/WebsocketMessages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,16 @@ export interface GetRenderResponseMessage {
type: "GetRenderResponseMessage";
payload: Uint8Array;
}
/** Send a file for clients to download.
*
* (automatically generated)
*/
export interface FileDownload {
type: "FileDownload";
filename: string;
content: Uint8Array;
mime_type: string;
}

export type Message =
| ViewerCameraMessage
Expand Down Expand Up @@ -723,4 +733,5 @@ export type Message =
| CatmullRomSplineMessage
| CubicBezierSplineMessage
| GetRenderRequestMessage
| GetRenderResponseMessage;
| GetRenderResponseMessage
| FileDownload;

0 comments on commit a49be3a

Please sign in to comment.