Files
hapi/web/src/lib/utils.ts
T
weishu 20c05a06ec fix: correct UTF-8 encoding in base64 decode for file paths fix #20
The old decodeBase64 implementation tried atob() first which returns
garbled text for UTF-8 content (e.g., Chinese characters), and only
fell back to correct UTF-8 decoding when atob() threw an exception
(which it doesn't for valid base64).

Changes:
- Add encodeBase64/decodeBase64 utilities to web/src/lib/utils.ts
  using TextEncoder/TextDecoder for proper UTF-8 support
- Update file.tsx to use shared decodeBase64 instead of buggy local impl
- Update files.tsx to use shared encodeBase64 instead of deprecated
  escape/unescape approach
2025-12-28 14:38:05 +08:00

31 lines
861 B
TypeScript

import { clsx, type ClassValue } from 'clsx'
import { twMerge } from 'tailwind-merge'
export function cn(...inputs: ClassValue[]): string {
return twMerge(clsx(inputs))
}
/**
* Decode base64 string to UTF-8 text
*/
export function decodeBase64(value: string): { text: string; ok: boolean } {
try {
const binaryString = atob(value)
const bytes = Uint8Array.from(binaryString, (char) => char.charCodeAt(0))
const text = new TextDecoder('utf-8').decode(bytes)
return { text, ok: true }
} catch {
return { text: '', ok: false }
}
}
/**
* Encode UTF-8 text to base64 string
*/
export function encodeBase64(value: string): string {
const bytes = new TextEncoder().encode(value)
const binaryString = Array.from(bytes, (byte) => String.fromCharCode(byte)).join('')
return btoa(binaryString)
}