fix: non-ascii characters compatible in {$src_raw_base64}

This commit is contained in:
Samler 2025-07-01 15:03:18 +08:00
parent 2ac5263363
commit b29ddea4f2
No known key found for this signature in database
GPG Key ID: BFDA5A582449E3B0
2 changed files with 15 additions and 1 deletions

View File

@ -34,6 +34,7 @@ import { Viewers, ViewersByID } from "../siteConfigSlice.ts";
import { AppThunk } from "../store.ts";
import { askSaveAs, askStaleVersionAction } from "./dialog.ts";
import { longRunningTaskWithSnackbar, refreshSingleFileSymbolicLinks } from "./file.ts";
import { base64Encode } from "../../util/base64.ts";
export interface ExpandedViewerSetting {
[key: string]: Viewer[];
@ -273,7 +274,7 @@ export function openCustomViewer(file: FileResponse, viewer: Viewer, preferredVe
const vars: { [key: string]: string } = {
src: encodeURIComponent(entityUrl.urls[0].url),
src_raw: entityUrl.urls[0].url,
src_raw_base64: btoa(entityUrl.urls[0].url),
src_raw_base64: base64Encode(entityUrl.urls[0].url),
name: encodeURIComponent(file.name),
version: preferredVersion ? preferredVersion : "",
id: file.id,

13
src/util/base64.ts Normal file
View File

@ -0,0 +1,13 @@
export function base64Encode(data: string | Uint8Array): string {
const bytes = typeof data === "string" ? new TextEncoder().encode(data) : data;
const binary = Array.from(bytes, (b) => String.fromCharCode(b)).join("");
return btoa(binary);
}
export function base64Decode(data: string): Uint8Array {
return Uint8Array.from(atob(data), (c) => c.charCodeAt(0));
}
export function base64DecodeToString(data: string): string {
return new TextDecoder().decode(base64Decode(data));
}