feat: Added the base64-encoded url for magic vars (#273)

* feat: Added the base64-encoded url for magic vars

* fix: non-ascii characters compatible in {$src_raw_base64}
This commit is contained in:
Samler 2025-07-01 20:24:01 +08:00 committed by GitHub
parent e729ee8893
commit 2c7b1bd38a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 23 additions and 0 deletions

View File

@ -192,6 +192,7 @@
"maxSizeDes": "The maximum file size supported by this application. 0 means no limit. If the file exceeds this size, it will still be opened, but users will be warned.",
"srcEncodedVar": "URL-encoded file Blob temporary access URL",
"srcVar": "File blob temporary access URL",
"srcBase64Var": "Base64-encoded File blob temporary access URL",
"nameEncodedVar": "URL-encoded file name",
"versionEntityVar": "The Blob ID of the opened file version, empty means the latest version.",
"fileIdVar": "File ID",

View File

@ -192,6 +192,7 @@
"maxSizeDes": "このアプリがサポートする最大ファイルサイズです。「0」と入力すると制限なしになります。サイズ超過の場合もファイルを開こうとしますが、警告が表示されます。",
"srcEncodedVar": "URLエンコード済みのファイルBlob一時アクセスアドレス",
"srcVar": "ファイルBlob一時アクセスアドレス",
"srcBase64Var": "Base64エンコード済みのファイルBlob一時アクセスアドレス",
"nameEncodedVar": "URLエンコード済みのファイル名",
"versionEntityVar": "開いているファイルバージョンのBlob ID空欄の場合は最新バージョンが開かれています。",
"fileIdVar": "ファイルID",

View File

@ -192,6 +192,7 @@
"maxSizeDes": "此应用支持的最大文件大小,填写 0 表示不限制,超出大小时仍会尝试打开文件,但会警告用户。",
"srcEncodedVar": "经过 URL 编码后的文件 Blob 临时访问地址",
"srcVar": "文件 Blob 临时访问地址",
"srcBase64Var": "经过 Base64 编码后的文件 Blob 临时访问地址",
"nameEncodedVar": "经过 URL 编码后的文件名",
"versionEntityVar": "打开的文件版本 Blob ID为空时表示打开的是最新版本。",
"fileIdVar": "文件 ID",

View File

@ -59,6 +59,11 @@ const magicVars: MagicVar[] = [
value: "settings.srcVar",
example: "https://cloudreve.org/api/v4/file/content/zOie/0/text.txt?sign=xxx",
},
{
name: "{$src_raw_base64}",
value: "settings.srcBase64Var",
example: "aHR0cHM6Ly9jbG91ZHJldmUub3JnL2FwaS92NC9maWxlL2NvbnRlbnQvek9pZS8wL3RleHQudHh0P3NpZ249eHh4",
},
{
name: "{$name}",
value: "settings.nameEncodedVar",

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,6 +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: 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));
}