Skip to content

fix: base64-encode binary files in /file/content endpoint#2934

Closed
shuv1337 wants to merge 1 commit into
anomalyco:devfrom
shuv1337:server_bin_content
Closed

fix: base64-encode binary files in /file/content endpoint#2934
shuv1337 wants to merge 1 commit into
anomalyco:devfrom
shuv1337:server_bin_content

Conversation

@shuv1337

@shuv1337 shuv1337 commented Oct 2, 2025

Copy link
Copy Markdown
Contributor

Binary files were being corrupted when returned as JSON strings because the server converted binary data to UTF-8, replacing invalid UTF-8 bytes with replacement characters (�). This made images and other binary files unreadable in the web interface.

Changes:

  • Add 'encoding' field to File.Content schema (utf8 | base64)
  • Implement isBinaryFile() to detect binary files by extension and null bytes
  • Base64-encode binary files before JSON serialization
  • Return encoding type in API response for proper client-side decoding

This eliminates the need for client-side filesystem workarounds and properly supports all binary file types (images, PDFs, executables, etc.)

Binary files were being corrupted when returned as JSON strings because
the server converted binary data to UTF-8, replacing invalid UTF-8 bytes
with replacement characters (�). This made images and other binary files
unreadable in the web interface.

Changes:
- Add 'encoding' field to File.Content schema (utf8 | base64)
- Implement isBinaryFile() to detect binary files by extension and null bytes
- Base64-encode binary files before JSON serialization
- Return encoding type in API response for proper client-side decoding

This eliminates the need for client-side filesystem workarounds and
properly supports all binary file types (images, PDFs, executables, etc.)
Comment thread packages/opencode/src/file/index.ts
@rekram1-node

Copy link
Copy Markdown
Collaborator

taking a look now

@rekram1-node

Copy link
Copy Markdown
Collaborator

only thing that's a little weird is returning images as base64... I kinda wish response was diffed off mime type and it was a union

@shuv1337

Copy link
Copy Markdown
Contributor Author

only thing that's a little weird is returning images as base64... I kinda wish response was diffed off mime type and it was a union

I can look into other options, but yes this is primarily for rendering images in an opencode web ui:
image

@rekram1-node

Copy link
Copy Markdown
Collaborator

Hm ill take a look too, worst case I will make some edits to this

@shuv1337

Copy link
Copy Markdown
Contributor Author

Sounds good! I'm definitely not picky about how it's implemented. If I can get usable images back from the API, I'll be happy!

@shuv1337 shuv1337 closed this by deleting the head repository Oct 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants