Files
EpicNext-Cms/src/lib/services/pet-import.ts
T
openhands adffac7360
Gitea Actions Runner Test / test-job (push) Successful in 1s
CI / check (push) Successful in 29s
CI / tests-unit (push) Failing after 1m37s
CI / tests-integration (push) Successful in 1m37s
CI / tests-ui (push) Successful in 2m17s
CI / preflight (push) Skipped
CI / deploy (push) Skipped
feat(catalog): store furniture bundles as .hab instead of .nitro
Every bundle the CMS writes — upload, clone, sync, repair and the pet /
effect / figure importers — now lands as `<classname>.hab`, the extension
this deployment's renderer asks for. `.hab` and `.nitro` are the same
container, so an upload of either extension is accepted.

Resolution goes through one module, src/lib/furni/bundle-file.ts, so
nothing has to know the extension twice. Every existence check probes
`.hab` first and falls back to `.nitro`: the on-disk asset set is still
predominantly `.nitro`, and without the fallback Studio would report every
imported item as missing and the cleanup scan would classify 18k live
bundles as fake leftovers. Downloads are unchanged — Habbo's CDN and every
configured clone source still serve `.nitro`, so the conversion happens on
write, not on request.

Deliberately unchanged: the staged-attachment store in furni-attachment.ts
keys on a UUID and never reaches the client, so renaming it would break
in-flight recovery jobs.

Adds scripts/migrate-nitro-to-hab.ts to rename the existing asset set. It
refuses to run without --dry-run or --yes, never overwrites an existing
.hab, never deletes, and is idempotent.

Note: renderer-config.json lives outside this repo and was patched to
.hab separately; that file is served with a 30-day max-age, so returning
clients need a cms-client cache purge to pick the change up.
2026-10-10 17:09:36 +02:00

143 lines
3.7 KiB
TypeScript

import { existsSync, promises as fs } from "node:fs";
import {
bundleExistsInDir,
bundleWritePathInDir,
existingBundlePaths,
} from "@/lib/furni/bundle-file";
import { downloadFile } from "@/lib/services/import/core/download";
import { CANONICAL_PETS, resolvePetSwfUrl } from "@/lib/services/pet-source";
import { convertSwfToNitro } from "@/lib/services/swf-to-nitro";
import { getRuntimePath } from "@/lib/utils/runtime-path";
export interface PetListItem {
lib: string;
nitroExists: boolean;
}
export interface ImportPetResult {
ok: boolean;
lib: string;
warnings: string[];
error?: string;
}
export const PET_NITRO_DIR = getRuntimePath(
/*turbopackIgnore: true*/ process.cwd(),
"public/nitro-assets/bundled/pet",
);
export const PET_SWF_DIR = getRuntimePath(
/*turbopackIgnore: true*/ process.cwd(),
"public/swf/gordon",
);
export async function ensurePetDirs(): Promise<void> {
await Promise.all(
[PET_NITRO_DIR, PET_SWF_DIR]
.filter((dir) => !existsSync(/*turbopackIgnore: true*/ dir))
.map((dir) =>
fs.mkdir(/*turbopackIgnore: true*/ dir, { recursive: true }),
),
);
}
// Imports write `.hab`, the extension the client requests. A pet whose bundle
// predates that (`.nitro`) still counts as imported until it is re-imported.
function nitroPathFor(lib: string): string {
return bundleWritePathInDir(/*turbopackIgnore: true*/ PET_NITRO_DIR, lib);
}
function petBundleExists(lib: string): boolean {
return bundleExistsInDir(/*turbopackIgnore: true*/ PET_NITRO_DIR, lib);
}
export async function importSinglePet(params: {
lib: string;
onProgress?: (status: string) => void;
}): Promise<ImportPetResult> {
const { lib, onProgress } = params;
const warnings: string[] = [];
await ensurePetDirs();
onProgress?.("downloading");
const swfPath = getRuntimePath(
/*turbopackIgnore: true*/ PET_SWF_DIR,
`${lib}.swf`,
);
let swfUrl: string;
try {
swfUrl = await resolvePetSwfUrl(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" });
if (!dl.ok)
return {
ok: false,
lib,
warnings,
error: `SWF download failed: ${swfUrl}`,
};
onProgress?.("converting");
try {
const swfBuffer = await fs.readFile(/*turbopackIgnore: true*/ swfPath);
const result = await convertSwfToNitro(swfBuffer, lib);
await fs.writeFile(
/*turbopackIgnore: true*/ 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 deleteImportedPet(
lib: string,
): Promise<{ deletedFile: boolean }> {
// Remove both conventions so a re-import cannot resurrect a stale bundle.
const paths = existingBundlePaths(
[/*turbopackIgnore: true*/ PET_NITRO_DIR],
lib,
);
if (paths.length === 0) return { deletedFile: false };
for (const p of paths) {
await fs.unlink(/*turbopackIgnore: true*/ p).catch(() => {});
}
return { deletedFile: true };
}
export function getPetList(search: string): PetListItem[] {
const term = search.trim().toLowerCase();
const list = CANONICAL_PETS.map((lib) => ({
lib,
nitroExists: petBundleExists(lib),
}));
return term ? list.filter((p) => p.lib.toLowerCase().includes(term)) : list;
}
export function getPetStats(): {
total: number;
imported: number;
missing: number;
} {
const imported = CANONICAL_PETS.filter((lib) => petBundleExists(lib)).length;
return {
total: CANONICAL_PETS.length,
imported,
missing: CANONICAL_PETS.length - imported,
};
}