Skip to content

refactor: extract base64ToBlob utility, fix MIME resolution in download#1356

Merged
ringringlin merged 3 commits intomainfrom
zynx/refactor/extract-base64-utils
Mar 17, 2026
Merged

refactor: extract base64ToBlob utility, fix MIME resolution in download#1356
ringringlin merged 3 commits intomainfrom
zynx/refactor/extract-base64-utils

Conversation

@piorpua
Copy link
Copy Markdown
Contributor

@piorpua piorpua commented Mar 17, 2026

Follow-up to #1324.

Summary

  • Extract duplicated atob decode logic and MIME_MAP into src/renderer/utils/base64.ts (base64ToBlob + BINARY_MIME_MAP), eliminating the DRY violation introduced in fix: binary file download fails with 0KB in WebUI mode + add context menu download #1324
  • Fix MIME type resolution in useWorkspaceFileOps.ts: was regex-extracting from data: URL prefix (returns application/octet-stream for binary files); now uses extension-based lookup via BINARY_MIME_MAP, consistent with PreviewPanel.tsx
  • Remove console.error residual from useWorkspaceFileOps.ts

Test plan

  • Run bun run test tests/unit/base64.test.ts — 6 tests should pass
  • Download an Excel/PDF/PPT/Word file from PreviewPanel toolbar — verify file opens correctly
  • Right-click a binary file in workspace tree → Download — verify correct MIME type and file opens correctly
  • Run bunx tsc --noEmit — no type errors

zynx added 3 commits March 17, 2026 13:58
…e download

- Add src/renderer/utils/base64.ts with base64ToBlob() and BINARY_MIME_MAP
- Remove duplicated atob decode logic from PreviewPanel and useWorkspaceFileOps
- Fix MIME type resolution in useWorkspaceFileOps to use extension lookup
  instead of parsing the data URL prefix (which returns octet-stream for
  binary files like Excel/PDF)
- Remove console.error residual from useWorkspaceFileOps
- Add unit tests for base64ToBlob and BINARY_MIME_MAP (6 tests)
- Register base64.ts in vitest coverage.include
Extract shared download.ts with downloadFileFromPath and
downloadTextContent, replacing duplicated blob/link logic in both
PreviewPanel and useWorkspaceFileOps.

Fix ZIP (and other archive) downloads from PreviewPanel toolbar that
previously fell through to the text branch, producing corrupted files.
The root cause was that contentType for unknown extensions defaults to
'code', so the old binary check (excel/pdf/ppt/word only) missed zip.

Changes:
- Add zip/tar/gz/bz2/7z/rar MIME entries to BINARY_MIME_MAP
- New src/renderer/utils/download.ts: downloadFileFromPath reads raw
  bytes via getImageBase64+atob (CSP-safe); downloadTextContent wraps
  Blob creation; both share a private triggerBlobDownload helper
- PreviewPanel.handleDownload: any file with metadata.filePath goes
  through downloadFileFromPath; pure base64 image keeps fetch path;
  text/code uses downloadTextContent
- useWorkspaceFileOps.handleDownloadFile: simplified to one-liner call
- Tests: add archive MIME assertions to base64.test.ts; new
  download.dom.test.ts with 6 cases covering MIME resolution, click
  trigger, URL revocation, and error propagation
@ringringlin ringringlin merged commit 984cc46 into main Mar 17, 2026
17 checks passed
@piorpua piorpua deleted the zynx/refactor/extract-base64-utils branch March 17, 2026 07:03
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