feat: locally store assets

This commit is contained in:
2026-03-13 17:16:54 +07:00 Unverified
parent e1415d76b1
commit 942ed655b5
5 changed files with 100 additions and 27 deletions
+6 -24
View File
@@ -7,7 +7,6 @@ import {
InvalidFormatError,
MaintenanceError,
RatelimitError,
TTLCache,
} from "enka.ts";
import { twj } from "tw-to-css";
import type { App } from "@";
@@ -17,9 +16,9 @@ import { featherImage } from "@/assets/feather";
import shadeSvg from "@/assets/shade.svg" with { type: "file" };
import fontFile from "@/assets/shinshin.woff2" with { type: "file" };
import { TalentSvg } from "@/assets/talent";
import { AvatarStore } from "@/type/enka";
import { AssetManager, getEnkaAvatarStore } from "@/lib/assets";
const cache = new TTLCache();
const assets = new AssetManager();
const font = file(fontFile);
const shade = file(shadeSvg);
@@ -65,21 +64,16 @@ export function card(app: App) {
...[...avatar.Consts, ...Object.values(avatar.Skills)].map(
async (path) => ({
src: path,
data: await fetch(
new URL(path, "https://enka.network"),
).then((res) => res.arrayBuffer()),
data: await assets.load(path),
}),
),
fetch(
new URL(
assets
.load(
avatar.SideIconName.replace(
"AvatarIcon_Side",
"Gacha_AvatarImg",
),
"https://enka.network",
),
)
.then((res) => res.arrayBuffer())
)
.then(async (buf) => ({
src: "/avatar",
data: await featherImage(buf, [0, 0.3, 0, 0]),
@@ -320,15 +314,3 @@ export function card(app: App) {
},
);
}
async function getEnkaAvatarStore(): Promise<AvatarStore> {
const cached = cache.get("enkaAvatarStore");
if (cached) return cached as AvatarStore;
const response = await fetch(
"https://github.com/EnkaNetwork/API-docs/raw/refs/heads/master/store/gi/avatars.json",
);
const data = await response.json();
cache.set("enkaAvatarStore", data, 86400); // 24 hours
return data;
}