Files
EpicNext-Cms/src/lib/services/figure-import.ts
T
Simo ce4a3ba32c
CI / check (pull_request) Successful in 2m11s
CI / deploy (pull_request) Skipped
CI / e2e (pull_request) Skipped
Merge remote-tracking branch 'origin/main' into codex/housekeeping-rebuild-stepwise
2026-09-04 20:08:49 +02:00

197 lines
5.2 KiB
TypeScript

import { existsSync, promises as fs } from "node:fs";
import { logServerError } from "@/lib/server-log";
import { resolveFigureSwfUrl } from "@/lib/services/figure-source";
import { listFigureLibraries } from "@/lib/services/figuremap";
import { getGamedataRoot } from "@/lib/services/furni-asset-dirs";
import { downloadFile } from "@/lib/services/import/core/download";
import { siteSettings } from "@/lib/services/site-settings";
import { convertSwfToNitro } from "@/lib/services/swf-to-nitro";
import { getRuntimePath } from "@/lib/utils/runtime-path";
export interface FigureListItem {
lib: string;
revision: number;
nitroExists: boolean;
// First part the library provides ({type,id}) — used to render an avatar
// preview wearing it. Undefined if the library declares no parts.
previewType?: string;
previewId?: number;
}
export interface ImportFigureResult {
ok: boolean;
lib: string;
warnings: string[];
error?: string;
}
const FIGURE_NITRO_DIR_FALLBACK = getRuntimePath(
/*turbopackIgnore: true*/ process.cwd(),
"public/nitro-assets/bundled/figure",
);
const FIGURE_SWF_DIR_FALLBACK = getRuntimePath(
/*turbopackIgnore: true*/ process.cwd(),
"public/swf/gordon",
);
async function getFigureNitroDir(): Promise<string> {
const configured = (
(await siteSettings.get("figure_nitro_dir", "")) ?? ""
).trim();
if (configured) return configured;
const gamedataRoot = await getGamedataRoot();
if (gamedataRoot)
return getRuntimePath(
/*turbopackIgnore: true*/ gamedataRoot,
"bundled/figure",
);
return FIGURE_NITRO_DIR_FALLBACK;
}
async function getFigureSwfDir(): Promise<string> {
const configured = (
(await siteSettings.get("figure_swf_dir", "")) ?? ""
).trim();
return configured || FIGURE_SWF_DIR_FALLBACK;
}
export async function ensureFigureDirs(): Promise<void> {
const [nitroDir, swfDir] = await Promise.all([
getFigureNitroDir(),
getFigureSwfDir(),
]);
await Promise.all(
[nitroDir, swfDir]
.filter((dir) => !existsSync(/*turbopackIgnore: true*/ dir))
.map((dir) =>
fs.mkdir(/*turbopackIgnore: true*/ dir, { recursive: true }),
),
);
}
async function nitroPathFor(lib: string): Promise<string> {
return getRuntimePath(
/*turbopackIgnore: true*/ await getFigureNitroDir(),
`${lib}.nitro`,
);
}
export async function importSingleFigure(params: {
lib: string;
onProgress?: (status: string) => void;
signal?: AbortSignal;
}): Promise<ImportFigureResult> {
const { lib, onProgress, signal } = params;
signal?.throwIfAborted();
const warnings: string[] = [];
await ensureFigureDirs();
onProgress?.("downloading");
const swfPath = getRuntimePath(
/*turbopackIgnore: true*/ await getFigureSwfDir(),
`${lib}.swf`,
);
let swfUrl: string;
try {
swfUrl = await resolveFigureSwfUrl(lib);
} catch (err) {
return {
ok: false,
lib,
warnings,
error: `Could not resolve SWF URL: ${(err as Error).message}`,
};
}
const dl = await downloadFile(swfUrl, swfPath, {
validate: "swf",
signal,
});
if (!dl.ok)
return {
ok: false,
lib,
warnings,
error: `SWF download failed: ${swfUrl}`,
};
onProgress?.("converting");
signal?.throwIfAborted();
try {
const swfBuffer = await fs.readFile(/*turbopackIgnore: true*/ swfPath);
const result = convertSwfToNitro(swfBuffer, lib);
await fs.writeFile(
/*turbopackIgnore: true*/ await nitroPathFor(lib),
result.bundle,
);
if (result.warnings?.length) warnings.push(...result.warnings);
} catch (err) {
return {
ok: false,
lib,
warnings,
error: `SWF→Nitro conversion failed: ${(err as Error).message}`,
};
}
return { ok: true, lib, warnings };
}
export async function deleteImportedFigure(
lib: string,
): Promise<{ deletedFile: boolean }> {
const p = await nitroPathFor(lib);
if (existsSync(/*turbopackIgnore: true*/ p)) {
await fs
.unlink(/*turbopackIgnore: true*/ p)
.catch((error) => logServerError("figure.delete_failed", error, { lib }));
return { deletedFile: true };
}
return { deletedFile: false };
}
export async function getFigureList(params: {
search: string;
page: number;
perPage: number;
}): Promise<{
items: FigureListItem[];
meta: { page: number; perPage: number; total: number };
}> {
const { search, page, perPage } = params;
const libs = await listFigureLibraries();
const term = search.trim().toLowerCase();
const filtered = term
? libs.filter((l) => l.id.toLowerCase().includes(term))
: libs;
const total = filtered.length;
const start = (Math.max(page, 1) - 1) * perPage;
const nitroDir = await getFigureNitroDir();
const items = filtered.slice(start, start + perPage).map((l) => ({
lib: l.id,
revision: l.revision,
nitroExists: existsSync(
/*turbopackIgnore: true*/ getRuntimePath(nitroDir, `${l.id}.nitro`),
),
previewType: l.parts?.[0]?.type,
previewId: l.parts?.[0]?.id,
}));
return { items, meta: { page: Math.max(page, 1), perPage, total } };
}
export async function getFigureStats(): Promise<{
total: number;
imported: number;
missing: number;
}> {
const [libs, nitroDir] = await Promise.all([
listFigureLibraries(),
getFigureNitroDir(),
]);
const imported = libs.filter((l) =>
existsSync(
/*turbopackIgnore: true*/ getRuntimePath(nitroDir, `${l.id}.nitro`),
),
).length;
return { total: libs.length, imported, missing: libs.length - imported };
}