Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: Use decoding as a heuristic for binary data, and download accordingly (#8858)

Dylan Madisetti285f02c36785
diff --git a/frontend/src/components/editor/file-tree/file-explorer.tsx b/frontend/src/components/editor/file-tree/file-explorer.tsx
index 6ad29e6e47..0a05cfa226 100644
--- a/frontend/src/components/editor/file-tree/file-explorer.tsx
+++ b/frontend/src/components/editor/file-tree/file-explorer.tsx
@@ -62,9 +62,11 @@ import type { FileInfo } from "@/core/network/types";
import { isWasm } from "@/core/wasm/utils";
import { useAsyncData } from "@/hooks/useAsyncData";
import { ErrorBanner } from "@/plugins/impl/common/error-banner";
+import { deserializeBlob } from "@/utils/blob";
import { cn } from "@/utils/cn";
import { copyToClipboard } from "@/utils/copy";
import { downloadBlob } from "@/utils/download";
+import { type Base64String, base64ToDataURL } from "@/utils/json/base64";
import { openNotebook } from "@/utils/links";
import type { FilePath } from "@/utils/paths";
import { fileSplit } from "@/utils/pathUtils";
@@ -650,8 +652,20 @@ const Node = ({ node, style, dragHandle }: NodeRendererProps<FileInfo>) => {
<DropdownMenuItem
onSelect={async () => {
const details = await sendFileDetails({ path: node.data.path });
- const contents = details.contents || "";
- downloadBlob(new Blob([contents]), node.data.name);
+ if (details.isBase64 && details.contents) {
+ const blob = deserializeBlob(
+ base64ToDataURL(
+ details.contents as Base64String,
+ details.mimeType || "application/octet-stream",
+ ),
+ );
+ downloadBlob(blob, node.data.name);
+ } else {
+ downloadBlob(
+ new Blob([details.contents || ""]),
+ node.data.name,
+ );
+ }
}}
>
<DownloadIcon className={ic} />
diff --git a/frontend/src/components/editor/file-tree/file-viewer.tsx b/frontend/src/components/editor/file-tree/file-viewer.tsx
index c5a29a165b..76d80c10cf 100644
--- a/frontend/src/components/editor/file-tree/file-viewer.tsx
+++ b/frontend/src/components/editor/file-tree/file-viewer.tsx
@@ -21,6 +21,7 @@ import { filenameAtom } from "@/core/saving/file-state";
import { isWasm } from "@/core/wasm/utils";
import { useAsyncData } from "@/hooks/useAsyncData";
import { ErrorBanner } from "@/plugins/impl/common/error-banner";
+import { deserializeBlob } from "@/utils/blob";
import { copyToClipboard } from "@/utils/copy";
import { downloadBlob, downloadByURL } from "@/utils/download";
import { type Base64String, base64ToDataURL } from "@/utils/json/base64";
@@ -121,9 +122,22 @@ export const FileViewer: React.FC<Props> = ({ file, onOpenNotebook }) => {
}
const handleDownload = () => {
- if (isMediaMime(mimeType)) {
- const dataURL = base64ToDataURL(data.contents as Base64String, mimeType);
- downloadByURL(dataURL, data.file.name);
+ if (data.isBase64 && data.contents) {
+ if (isMediaMime(mimeType)) {
+ const dataURL = base64ToDataURL(
+ data.contents as Base64String,
+ mimeType,
+ );
+ downloadByURL(dataURL, data.file.name);
+ } else {
+ const blob = deserializeBlob(
+ base64ToDataURL(
+ data.contents as Base64String,
+ data.mimeType || "application/octet-stream",
+ ),
+ );
+ downloadBlob(blob, data.file.name);
+ }
return;
}
diff --git a/marimo/_server/api/endpoints/file_explorer.py b/marimo/_server/api/endpoints/file_explorer.py
index 7c7f0a6789..1566f72baa 100644
--- a/marimo/_server/api/endpoints/file_explorer.py
+++ b/marimo/_server/api/endpoints/file_explorer.py
@@ -95,8 +95,6 @@ async def file_details(
$ref: "#/components/schemas/FileDetailsResponse"
"""
body = await parse_request(request, cls=FileDetailsRequest)
- # This fails if the file isn't encoded as utf-8
- # TODO: support returning raw bytes
return file_system.get_details(body.path)
diff --git a/marimo/_server/files/file_system.py b/marimo/_server/files/file_system.py
index dee1836154..a72aa00a4e 100644
--- a/marimo/_server/files/file_system.py
+++ b/marimo/_server/files/file_system.py
@@ -29,8 +29,11 @@ class FileSystem(ABC):
pass
@abstractmethod
- def open_file(self, path: str) -> str:
- """Open and read the content of a file."""
+ def open_file(self, path: str, encoding: str | None = None) -> str | bytes:
+ """Open and read the content of a file.
+
+ Returns str for text files, bytes for binary files.
+ """
pass
@abstractmethod
diff --git a/marimo/_server/files/os_file_system.py b/marimo/_server/files/os_file_system.py
index b990eb880f..15cb8235de 100644
--- a/marimo/_server/files/os_file_system.py
+++ b/marimo/_server/files/os_file_system.py
@@ -96,15 +96,25 @@ class OSFileSystem(FileSystem):
contents: str | None = None,
) -> FileDetailsResponse:
file_info = self._get_file_info(path)
+ is_base64 = False
+ actual_contents: str | bytes | None
if file_info.is_directory:
actual_contents = None
elif contents is not None:
actual_contents = contents
else:
actual_contents = self.open_file(path, encoding=encoding)
+ if isinstance(actual_contents, bytes):
+ actual_contents = base64.b64encode(actual_contents).decode(
+ "utf-8"
+ )
+ is_base64 = True
mime_type = mimetypes.guess_type(path)[0]
return FileDetailsResponse(
- file=file_info, contents=actual_contents, mime_type=mime_type
+ file=file_info,
+ contents=actual_contents,
+ mime_type=mime_type,
+ is_base64=is_base64,
)
def _is_marimo_file(self, path: str) -> bool:
@@ -116,13 +126,12 @@ class OSFileSystem(FileSystem):
return is_marimo_app(path)
- def open_file(self, path: str, encoding: str | None = None) -> str:
+ def open_file(self, path: str, encoding: str | None = None) -> str | bytes:
file_path = Path(path)
try:
- return file_path.read_text(encoding=encoding)
+ return file_path.read_text(encoding=encoding or "utf-8")
except UnicodeDecodeError:
- # If its a UnicodeDecodeError, try as bytes and convert to base64
- return base64.b64encode(file_path.read_bytes()).decode("utf-8")
+ return file_path.read_bytes()
def create_file_or_directory(
self,
diff --git a/marimo/_server/models/files.py b/marimo/_server/models/files.py
index 7375547d6d..503cbd968a 100644
--- a/marimo/_server/models/files.py
+++ b/marimo/_server/models/files.py
@@ -96,6 +96,7 @@ class FileDetailsResponse(msgspec.Struct, rename="camel"):
file: FileInfo
contents: Optional[str] = None
mime_type: Optional[str] = None
+ is_base64: bool = False
class FileCreateResponse(BaseResponse):
diff --git a/marimo/_smoke_tests/issues/5361.py b/marimo/_smoke_tests/issues/5361.py
new file mode 100644
index 0000000000..67079d8903
--- /dev/null
+++ b/marimo/_smoke_tests/issues/5361.py
@@ -0,0 +1,73 @@
+import marimo
+
+__generated_with = "0.13.0"
+app = marimo.App(width="medium")
+
+
+def _():
+ import marimo as mo
+ import cProfile
+ import struct
+ import os
+
+ mo.md("# Binary File Download Test (GH-5361)\n\nUse the **file explorer** panel on the left to navigate into the smoke test directory and try downloading the sample files. Binary files (`.prof`, `.bin`, `.pkl`, `.png`) should download as valid binary, not as base64 text.")
+ return cProfile, mo, os, struct
+
+
+def _(cProfile, os, struct):
+ import pickle
+ import zlib
+
+ test_dir = os.path.dirname(os.path.abspath(__file__))
+
+ # 1. cProfile .prof file
+ prof_path = os.path.join(test_dir, "sample.prof")
+ profiler = cProfile.Profile()
+ profiler.enable()
+ sum(range(10000))
+ profiler.disable()
+ profiler.dump_stats(prof_path)
+
+ # 2. Generic binary file with non-UTF8 bytes
+ bin_path = os.path.join(test_dir, "sample.bin")
+ with open(bin_path, "wb") as f:
+ f.write(struct.pack("!4sIf16s", b"MAGIC", 42, 3.14, b"\xff\xfe\xfd" + b"\x00" * 13))
+
+ # 3. Pickle file
+ pkl_path = os.path.join(test_dir, "sample.pkl")
+ with open(pkl_path, "wb") as f:
+ pickle.dump({"key": "value", "numbers": [1, 2, 3], "bytes": b"\xff\xfe"}, f)
+
+ # 4. A plain text file (control: should still work)
+ txt_path = os.path.join(test_dir, "sample.txt")
+ with open(txt_path, "w") as f:
+ f.write("This is a plain text file.\nIt should download correctly.\n")
+
+ # 5. A minimal valid PNG (1x1 red pixel) — tests the isMediaMime/dataURL path
+ def _make_png():
+ def chunk(ctype, data):
+ c = ctype + data
+ return struct.pack(">I", len(data)) + c + struct.pack(">I", zlib.crc32(c) & 0xFFFFFFFF)
+
+ return (
+ b"\x89PNG\r\n\x1a\n"
+ + chunk(b"IHDR", struct.pack(">IIBBBBB", 1, 1, 8, 2, 0, 0, 0))
+ + chunk(b"IDAT", zlib.compress(b"\x00\xff\x00\x00"))
+ + chunk(b"IEND", b"")
+ )
+
+ png_path = os.path.join(test_dir, "sample.png")
+ with open(png_path, "wb") as f:
+ f.write(_make_png())
+
+ print(f"Created test files in {test_dir}:")
+ for name in ["sample.prof", "sample.bin", "sample.pkl", "sample.txt", "sample.png"]:
+ p = os.path.join(test_dir, name)
+ print(f" {name}: {os.path.getsize(p)} bytes")
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/packages/openapi/api.yaml b/packages/openapi/api.yaml
index a0978a8f2d..02beee20ab 100644
--- a/packages/openapi/api.yaml
+++ b/packages/openapi/api.yaml
@@ -1708,6 +1708,9 @@ components:
default: null
file:
$ref: '#/components/schemas/FileInfo'
+ isBase64:
+ default: false
+ type: boolean
mimeType:
anyOf:
- type: string
diff --git a/packages/openapi/src/api.ts b/packages/openapi/src/api.ts
index 5c68e20e42..4bd142a730 100644
--- a/packages/openapi/src/api.ts
+++ b/packages/openapi/src/api.ts
@@ -4298,6 +4298,8 @@ export interface components {
/** @default null */
contents?: string | null;
file: components["schemas"]["FileInfo"];
+ /** @default false */
+ isBase64?: boolean;
/** @default null */
mimeType?: string | null;
};
diff --git a/tests/_server/api/endpoints/test_file_explorer.py b/tests/_server/api/endpoints/test_file_explorer.py
index dda5d9a152..086a9fdc91 100644
--- a/tests/_server/api/endpoints/test_file_explorer.py
+++ b/tests/_server/api/endpoints/test_file_explorer.py
@@ -46,7 +46,28 @@ def test_file_details(client: TestClient) -> None:
)
assert response.status_code == 200, response.text
assert response.headers["content-type"] == "application/json"
- assert "file" in response.json()
+ data = response.json()
+ assert "file" in data
+ assert data["isBase64"] is False
+
+
+def test_file_details_binary(client: TestClient) -> None:
+ import base64
+
+ bin_path = os.path.join(test_dir, "binary_file.bin")
+ raw_bytes = b"\xff\xfe\xfd\x00\x80"
+ with open(bin_path, "wb") as f:
+ f.write(raw_bytes)
+ response = client.post(
+ "/api/files/file_details",
+ headers=HEADERS,
+ json={"path": bin_path},
+ )
+ assert response.status_code == 200, response.text
+ data = response.json()
+ assert data["isBase64"] is True
+ assert data["contents"] == base64.b64encode(raw_bytes).decode("utf-8")
+ os.remove(bin_path)
def test_create_and_delete_file_or_directory(client: TestClient) -> None:
diff --git a/tests/_server/files/test_os_file_system.py b/tests/_server/files/test_os_file_system.py
index dfb43343d4..e2c32fbd2b 100644
--- a/tests/_server/files/test_os_file_system.py
+++ b/tests/_server/files/test_os_file_system.py
@@ -176,8 +176,18 @@ def test_open_file(test_dir: Path, fs: OSFileSystem) -> None:
test_content = "Hello, World!"
file_path = test_dir / test_file_name
file_path.write_text(test_content)
- content = fs.open_file(str(file_path))
- assert content == test_content
+ result = fs.open_file(str(file_path))
+ assert result == test_content
+ assert isinstance(result, str)
+
+
+def test_open_file_binary(test_dir: Path, fs: OSFileSystem) -> None:
+ file_path = test_dir / "binary.bin"
+ raw_bytes = b"\xff\xfe\xfd"
+ file_path.write_bytes(raw_bytes)
+ result = fs.open_file(str(file_path))
+ assert result == raw_bytes
+ assert isinstance(result, bytes)
def test_delete_file(test_dir: Path, fs: OSFileSystem) -> None:
@@ -273,10 +283,29 @@ def test_get_details_nonexistent_file(fs: OSFileSystem) -> None:
def test_get_details_binary_file(test_dir: Path, fs: OSFileSystem) -> None:
file_path = test_dir / "binfile.bin"
+ # These bytes are valid UTF-8, so no base64 encoding needed
file_path.write_bytes(b"\x00\x01\x02\x03")
result = fs.get_details(str(file_path))
assert result.contents == "\x00\x01\x02\x03"
+ assert result.is_base64 is False
+
+
+def test_get_details_non_utf8_binary_file(
+ test_dir: Path, fs: OSFileSystem
+) -> None:
+ """Binary files that can't be read as UTF-8 are base64-encoded."""
+ import base64
+
+ file_path = test_dir / "data.prof"
+ raw_bytes = b"\xff\xfe\xfd\x00\x01\x80\x81"
+ file_path.write_bytes(raw_bytes)
+ result = fs.get_details(str(file_path))
+
+ assert result.is_base64 is True
+ assert result.contents == base64.b64encode(raw_bytes).decode("utf-8")
+ # Verify round-trip: decoding base64 gives back original bytes
+ assert base64.b64decode(result.contents) == raw_bytes
def test_get_details_empty_string_contents(
@@ -318,13 +347,16 @@ def test_get_details_non_utf8_encoding_and_contents(
file_path.write_bytes(content)
result = fs.get_details(str(file_path), encoding="latin-1")
assert result.contents == "café"
+ assert result.is_base64 is False
base64_content = __import__("base64").b64encode(content).decode("utf-8")
result_utf8 = fs.get_details(str(file_path), encoding="utf-8")
assert result_utf8.contents == base64_content
+ assert result_utf8.is_base64 is True
result2 = fs.get_details(
str(file_path), encoding="utf-8", contents="override"
)
assert result2.contents == "override"
+ assert result2.is_base64 is False
class TestIsMarimoFile: