fix: mirror manual furni uploads to live assets
This commit is contained in:
1 parent
01570874a8
commit
86cd43def9
2 files changed
+193
-2
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -2,7 +2,7 @@ import { existsSync, promises as fs } from "node:fs";
|
|||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { eq, sql } from "drizzle-orm";
|
import { eq, sql } from "drizzle-orm";
|
||||||
import { db, ItemsBase } from "@/lib/db";
|
import { db, ItemsBase } from "@/lib/db";
|
||||||
import { getFurniAssetDirs } from "@/lib/services/furni-asset-dirs";
|
import { getFurniAssetWriteTargets } from "@/lib/services/furni-asset-dirs";
|
||||||
import { appendFurniEntry, buildFurniEntry } from "@/lib/services/furni-data";
|
import { appendFurniEntry, buildFurniEntry } from "@/lib/services/furni-data";
|
||||||
import {
|
import {
|
||||||
allocateCatalogItemId,
|
allocateCatalogItemId,
|
||||||
@@ -43,6 +43,36 @@ const MIGRATIONS_DIR = path.resolve(
|
|||||||
|
|
||||||
let itemsBaseIdAllocChain: Promise<unknown> = Promise.resolve();
|
let itemsBaseIdAllocChain: Promise<unknown> = Promise.resolve();
|
||||||
|
|
||||||
|
function pathKey(value: string): string {
|
||||||
|
const normalized = path.normalize(value);
|
||||||
|
return process.platform === "win32" ? normalized.toLowerCase() : normalized;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function mirrorUploadedAsset(
|
||||||
|
sourcePath: string,
|
||||||
|
fileName: string,
|
||||||
|
dirs: string[],
|
||||||
|
warnings: string[],
|
||||||
|
copiedPaths: string[],
|
||||||
|
): Promise<void> {
|
||||||
|
const sourceKey = pathKey(sourcePath);
|
||||||
|
const seen = new Set<string>();
|
||||||
|
for (const dir of dirs) {
|
||||||
|
const targetPath = path.join(/*turbopackIgnore: true*/ dir, fileName);
|
||||||
|
const targetKey = pathKey(targetPath);
|
||||||
|
if (targetKey === sourceKey || seen.has(targetKey)) continue;
|
||||||
|
seen.add(targetKey);
|
||||||
|
try {
|
||||||
|
await fs.copyFile(sourcePath, targetPath);
|
||||||
|
copiedPaths.push(targetPath);
|
||||||
|
} catch (err) {
|
||||||
|
warnings.push(
|
||||||
|
`asset mirror failed (${targetPath}): ${(err as Error).message}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function allocateItemsBaseId<T>(
|
async function allocateItemsBaseId<T>(
|
||||||
insertFn: (nextId: number) => Promise<T>,
|
insertFn: (nextId: number) => Promise<T>,
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
@@ -200,7 +230,8 @@ export async function uploadSingleFurni(params: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await ensureDirectories();
|
await ensureDirectories();
|
||||||
const { iconDir, nitroDir } = await getFurniAssetDirs();
|
const assetTargets = await getFurniAssetWriteTargets();
|
||||||
|
const { iconDir, nitroDir } = assetTargets;
|
||||||
|
|
||||||
const nitroFileName = `${classname}.nitro`;
|
const nitroFileName = `${classname}.nitro`;
|
||||||
const nitroPath = path.join(
|
const nitroPath = path.join(
|
||||||
@@ -225,6 +256,24 @@ export async function uploadSingleFurni(params: {
|
|||||||
await fs.writeFile(iconPath, iconBuffer);
|
await fs.writeFile(iconPath, iconBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const mirroredPaths: string[] = [];
|
||||||
|
await mirrorUploadedAsset(
|
||||||
|
nitroPath,
|
||||||
|
nitroFileName,
|
||||||
|
assetTargets.mirrorDirs.map((target) => target.nitroDir),
|
||||||
|
warnings,
|
||||||
|
mirroredPaths,
|
||||||
|
);
|
||||||
|
if (iconPath && iconFileName) {
|
||||||
|
await mirrorUploadedAsset(
|
||||||
|
iconPath,
|
||||||
|
iconFileName,
|
||||||
|
assetTargets.mirrorDirs.map((target) => target.iconDir),
|
||||||
|
warnings,
|
||||||
|
mirroredPaths,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const xdim = userXdim ?? Number(nitroMeta?.xdim ?? 1);
|
const xdim = userXdim ?? Number(nitroMeta?.xdim ?? 1);
|
||||||
const ydim = userYdim ?? Number(nitroMeta?.ydim ?? 1);
|
const ydim = userYdim ?? Number(nitroMeta?.ydim ?? 1);
|
||||||
|
|
||||||
@@ -248,6 +297,9 @@ export async function uploadSingleFurni(params: {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
await fs.unlink(nitroPath).catch(() => {});
|
await fs.unlink(nitroPath).catch(() => {});
|
||||||
if (iconPath) await fs.unlink(iconPath).catch(() => {});
|
if (iconPath) await fs.unlink(iconPath).catch(() => {});
|
||||||
|
await Promise.all(
|
||||||
|
mirroredPaths.map((file) => fs.unlink(file).catch(() => {})),
|
||||||
|
);
|
||||||
return {
|
return {
|
||||||
ok: false,
|
ok: false,
|
||||||
classname,
|
classname,
|
||||||
|
|||||||
Reference in new issue
Block a user