silverbullet/plug-api/syscalls/asset.ts

36 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-02-29 22:23:05 +08:00
import { base64DecodeDataUrl } from "../../lib/crypto.ts";
import { syscall } from "../syscall.ts";
import type { FileMeta } from "@silverbulletmd/silverbullet/types";
2022-10-14 21:11:33 +08:00
2024-08-07 19:27:25 +08:00
/**
* Reads an asset embedded in a plug (via the `assets` field in the plug manifest).
* @param plugName name of the plug to read asset from
* @param name name of the asset to read
* @param encoding either "utf8" or "dataurl"
* @returns the content of the asset in the requested encoding
*/
2022-10-14 21:11:33 +08:00
export async function readAsset(
plugName: string,
2022-10-14 21:11:33 +08:00
name: string,
encoding: "utf8" | "dataurl" = "utf8",
): Promise<string> {
const dataUrl = await syscall("asset.readAsset", plugName, name) as string;
2022-10-14 21:11:33 +08:00
switch (encoding) {
case "utf8":
2022-10-19 15:52:29 +08:00
return new TextDecoder().decode(base64DecodeDataUrl(dataUrl));
2022-10-14 21:11:33 +08:00
case "dataurl":
2022-10-14 21:53:51 +08:00
return dataUrl;
2022-10-14 21:11:33 +08:00
}
}
export async function listFiles(plugName: string): Promise<FileMeta[]> {
return await syscall("asset.listFiles", plugName);
}
export async function getFileMeta(
plugName: string,
name: string,
): Promise<FileMeta> {
return await syscall("asset.getFileMeta", plugName, name);
}