feat: add .nitro upload + sync-all + repair-icons features
- Upload .nitro bundles directly with full DB, catalog, and furnidata integration - Generate SQL migration files on upload (optional) - Auto-sync missing furniture from all configured clone sources (SSE batch) - Repair missing icons by extracting from local .nitro or downloading from sources - All behind ASSETS_IMPORT permission
This commit is contained in:
1 parent
c9144dc293
commit
804daeffca
13 files changed
+1484
-1
No files matched your search
@@ -0,0 +1,153 @@
|
||||
import { withAdmin } from "@/lib/api-handler";
|
||||
import { apiError } from "@/lib/api-response";
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { logAudit } from "@/lib/services/audit";
|
||||
import {
|
||||
cloneSingleFurni,
|
||||
fetchSourceFurnidata,
|
||||
} from "@/lib/services/clone-import";
|
||||
import { listSources } from "@/lib/services/clone-sources";
|
||||
import { runSseBatch } from "@/lib/services/import/core/sse-batch";
|
||||
|
||||
function sseEvent(data: unknown): string {
|
||||
return `data: ${JSON.stringify(data)}\n\n`;
|
||||
}
|
||||
|
||||
export const POST = withAdmin(
|
||||
{ permission: PERMS.ASSETS_IMPORT },
|
||||
async (request, ctx) => {
|
||||
const body = await request.json().catch(() => ({}));
|
||||
const sourceIdFilter: string | undefined = body.sourceId || undefined;
|
||||
|
||||
const allSources = await listSources();
|
||||
const sources = sourceIdFilter
|
||||
? allSources.filter((s) => s.id === sourceIdFilter)
|
||||
: allSources;
|
||||
|
||||
if (sources.length === 0) {
|
||||
return new Response(sseEvent({ type: "error", message: "No sources configured" }), {
|
||||
status: 400,
|
||||
headers: { "Content-Type": "text/event-stream" },
|
||||
});
|
||||
}
|
||||
|
||||
const allItems: Array<{
|
||||
classname: string;
|
||||
sourceName: string;
|
||||
sourceId: string;
|
||||
}> = [];
|
||||
|
||||
for (const source of sources) {
|
||||
let entries: Awaited<ReturnType<typeof fetchSourceFurnidata>>;
|
||||
try {
|
||||
entries = await fetchSourceFurnidata(source.furnidataUrl);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
|
||||
const classnames = entries.map((e) => e.classname);
|
||||
if (classnames.length === 0) continue;
|
||||
|
||||
const placeholders = classnames.map(() => "?").join(",");
|
||||
const rows = await prisma.$queryRawUnsafe<Array<{ item_name: string }>>(
|
||||
`SELECT item_name FROM items_base WHERE item_name IN (${placeholders})`,
|
||||
...classnames,
|
||||
);
|
||||
const have = new Set(rows.map((r) => r.item_name));
|
||||
|
||||
for (const entry of entries) {
|
||||
if (!have.has(entry.classname)) {
|
||||
allItems.push({
|
||||
classname: entry.classname,
|
||||
sourceName: source.name,
|
||||
sourceId: source.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (allItems.length === 0) {
|
||||
return new Response(
|
||||
sseEvent({
|
||||
type: "batch_complete",
|
||||
succeeded: 0,
|
||||
failed: 0,
|
||||
skipped: true,
|
||||
message: "All sources are up to date",
|
||||
}),
|
||||
{ headers: { "Content-Type": "text/event-stream" } },
|
||||
);
|
||||
}
|
||||
|
||||
if (allItems.length > 2000) {
|
||||
return new Response(
|
||||
sseEvent({
|
||||
type: "error",
|
||||
message: `Too many items to sync (${allItems.length}). Sync individual sources instead.`,
|
||||
}),
|
||||
{
|
||||
status: 400,
|
||||
headers: { "Content-Type": "text/event-stream" },
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
return runSseBatch({
|
||||
items: allItems,
|
||||
concurrency: 2,
|
||||
labelOf: (it) => `${it.sourceName}/${it.classname}`,
|
||||
worker: async (it, _index, report) => {
|
||||
const allSrc = await listSources();
|
||||
const source = allSrc.find((s) => s.id === it.sourceId);
|
||||
if (!source) {
|
||||
return { ok: false, error: "source not found" };
|
||||
}
|
||||
|
||||
let allEntries: Awaited<ReturnType<typeof fetchSourceFurnidata>>;
|
||||
try {
|
||||
allEntries = await fetchSourceFurnidata(source.furnidataUrl);
|
||||
} catch (err) {
|
||||
return {
|
||||
ok: false,
|
||||
error: `furnidata fetch failed: ${(err as Error).message}`,
|
||||
};
|
||||
}
|
||||
|
||||
const entry = allEntries.find((e) => e.classname === it.classname);
|
||||
if (!entry) {
|
||||
return {
|
||||
ok: false,
|
||||
error: "classname not found in source furnidata",
|
||||
};
|
||||
}
|
||||
|
||||
const result = await cloneSingleFurni({
|
||||
source,
|
||||
entry,
|
||||
onProgress: report,
|
||||
});
|
||||
|
||||
if (result.ok) {
|
||||
logAudit({
|
||||
userId: ctx.session.user.id,
|
||||
action: "auto_sync_furni",
|
||||
target: "FurniAsset",
|
||||
targetId: 0,
|
||||
after: {
|
||||
classname: it.classname,
|
||||
sourceId: it.sourceId,
|
||||
sourceName: it.sourceName,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
ok: result.ok,
|
||||
warnings: result.warnings,
|
||||
error: result.error,
|
||||
};
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,33 @@
|
||||
import { withAdmin } from "@/lib/api-handler";
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
import { repairMissingIcons } from "@/lib/services/repair-icons";
|
||||
|
||||
export const POST = withAdmin({ permission: PERMS.ASSETS_IMPORT }, async () => {
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
const stream = new ReadableStream({
|
||||
async start(controller) {
|
||||
const send = (data: unknown) => {
|
||||
try {
|
||||
controller.enqueue(
|
||||
encoder.encode(`data: ${JSON.stringify(data)}\n\n`),
|
||||
);
|
||||
} catch {
|
||||
/* stream closed */
|
||||
}
|
||||
};
|
||||
|
||||
await repairMissingIcons(send);
|
||||
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(stream, {
|
||||
headers: {
|
||||
"Content-Type": "text/event-stream",
|
||||
"Cache-Control": "no-cache",
|
||||
Connection: "keep-alive",
|
||||
},
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,92 @@
|
||||
import { withAdmin } from "@/lib/api-handler";
|
||||
import { apiError, apiOk } from "@/lib/api-response";
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
import { logAudit } from "@/lib/services/audit";
|
||||
import { uploadSingleFurni } from "@/lib/services/upload-import";
|
||||
|
||||
export const POST = withAdmin(
|
||||
{ permission: PERMS.ASSETS_IMPORT },
|
||||
async (request, ctx) => {
|
||||
const formData = await request.formData();
|
||||
|
||||
const classname = (formData.get("classname") as string)?.trim();
|
||||
const name = (formData.get("name") as string)?.trim();
|
||||
const description = (formData.get("description") as string)?.trim() || "";
|
||||
const itemType = formData.get("itemType") as string;
|
||||
const xdim = formData.get("xdim")
|
||||
? Number(formData.get("xdim"))
|
||||
: undefined;
|
||||
const ydim = formData.get("ydim")
|
||||
? Number(formData.get("ydim"))
|
||||
: undefined;
|
||||
const canstandon = formData.get("canstandon") === "true";
|
||||
const cansiton = formData.get("cansiton") === "true";
|
||||
const canlayon = formData.get("canlayon") === "true";
|
||||
const interactionType =
|
||||
(formData.get("interactionType") as string)?.trim() || "default";
|
||||
const customparams = (formData.get("customparams") as string)?.trim() || "";
|
||||
const generateSql = formData.get("generateSql") === "true";
|
||||
const nitroFile = formData.get("nitro") as File | null;
|
||||
const iconFile = formData.get("icon") as File | null;
|
||||
|
||||
if (!classname) return apiError("classname is required", 400);
|
||||
if (!name) return apiError("name is required", 400);
|
||||
if (!itemType || !["s", "i"].includes(itemType))
|
||||
return apiError("itemType must be 's' or 'i'", 400);
|
||||
if (!nitroFile || nitroFile.size === 0)
|
||||
return apiError(".nitro file is required", 400);
|
||||
|
||||
if (nitroFile.size > 50 * 1024 * 1024)
|
||||
return apiError(".nitro file too large (max 50MB)", 400);
|
||||
|
||||
const nitroBuffer = Buffer.from(await nitroFile.arrayBuffer());
|
||||
|
||||
let iconBuffer: Buffer | null = null;
|
||||
if (iconFile && iconFile.size > 0) {
|
||||
if (iconFile.size > 2 * 1024 * 1024)
|
||||
return apiError("Icon file too large (max 2MB)", 400);
|
||||
iconBuffer = Buffer.from(await iconFile.arrayBuffer());
|
||||
}
|
||||
|
||||
const result = await uploadSingleFurni({
|
||||
classname,
|
||||
name,
|
||||
description,
|
||||
itemType: itemType as "s" | "i",
|
||||
xdim,
|
||||
ydim,
|
||||
canstandon,
|
||||
cansiton,
|
||||
canlayon,
|
||||
interactionType,
|
||||
customparams,
|
||||
nitroBuffer,
|
||||
iconBuffer,
|
||||
generateSql,
|
||||
});
|
||||
|
||||
if (!result.ok) {
|
||||
return apiError(result.error || "Upload failed", 400);
|
||||
}
|
||||
|
||||
logAudit({
|
||||
userId: ctx.session.user.id,
|
||||
action: "furni_upload",
|
||||
target: "FurniAsset",
|
||||
targetId: result.itemId ?? 0,
|
||||
after: {
|
||||
classname: result.classname,
|
||||
itemId: result.itemId,
|
||||
catalogItemId: result.catalogItemId,
|
||||
},
|
||||
});
|
||||
|
||||
return apiOk({
|
||||
itemId: result.itemId,
|
||||
catalogItemId: result.catalogItemId,
|
||||
classname: result.classname,
|
||||
warnings: result.warnings,
|
||||
sqlFile: result.sqlFile,
|
||||
});
|
||||
},
|
||||
);
|
||||
Reference in new issue
Block a user