feat: import redists

This commit is contained in:
DecDuck
2025-08-22 13:48:47 +10:00
parent 322af0b4ca
commit f1957a418c
22 changed files with 715 additions and 806 deletions

View File

@ -2,7 +2,7 @@ import aclManager from "~/server/internal/acls";
import libraryManager from "~/server/internal/library";
export default defineEventHandler(async (h3) => {
const allowed = await aclManager.allowSystemACL(h3, ["import:game:read"]);
const allowed = await aclManager.allowSystemACL(h3, ["import:game:read", "import:redist:read"]);
if (!allowed) throw createError({ statusCode: 403 });
const unimportedGames = await libraryManager.fetchUnimportedGames();

View File

@ -0,0 +1,3 @@
import handler from "../game/index.get";
export default handler;

View File

@ -0,0 +1,56 @@
import { ArkErrors, type } from "arktype";
import aclManager from "~/server/internal/acls";
import { handleFileUpload } from "~/server/internal/utils/handlefileupload";
import * as jdenticon from "jdenticon";
import prisma from "~/server/internal/db/database";
import libraryManager from "~/server/internal/library";
export const ImportRedist = type({
library: "string",
path: "string",
name: "string",
description: "string",
});
export default defineEventHandler(async (h3) => {
const allowed = await aclManager.allowSystemACL(h3, ["import:redist:new"]);
if (!allowed) throw createError({ statusCode: 403 });
const body = await handleFileUpload(h3, {}, ["internal:read"], 1);
if (!body)
throw createError({ statusCode: 400, statusMessage: "Body required." });
const [[id], rawOptions, pull,, add] = body;
const options = ImportRedist(rawOptions);
if (options instanceof ArkErrors)
throw createError({ statusCode: 400, statusMessage: options.summary });
const valid = await libraryManager.checkUnimportedGamePath(
options.library,
options.path,
);
if (!valid)
throw createError({
statusCode: 400,
statusMessage: "Invalid library or game.",
});
const icon = id ?? add(jdenticon.toPng(options.name, 512));
const redist = await prisma.redist.create({
data: {
libraryId: options.library,
libraryPath: options.path,
mName: options.name,
mShortDescription: options.description,
mIconObjectId: icon,
},
});
await pull();
return redist;
});

View File

@ -84,6 +84,8 @@ export const systemACLDescriptions: ObjectFromList<typeof systemACLs> = {
"import:game:read":
"Fetch games to be imported, and search the metadata for games.",
"import:game:new": "Import a game.",
"import:redist:read": "Fetch redists to be imported.",
"import:redist:new": "Import a redist.",
"tags:read": "Fetch all tags",
"tags:create": "Create a tag",

View File

@ -74,9 +74,10 @@ export const systemACLs = [
"import:version:read",
"import:version:new",
"import:game:read",
"import:game:new",
"import:redist:read",
"import:redist:new",
"user:read",
"user:delete",

View File

@ -336,7 +336,7 @@ class LibraryManager {
nonce: `version-create-${gameId}-${versionPath}`,
title: `'${game.mName}' ('${versionPath}') finished importing.`,
description: `Drop finished importing version ${versionPath} for ${game.mName}.`,
actions: [`View|/admin/library/${gameId}`],
actions: [`View|/admin/library/g/${gameId}`],
acls: ["system:import:version:read"],
});

View File

@ -1,5 +1,5 @@
import type { EventHandlerRequest, H3Event } from "h3";
import type { Dump, Pull } from "../objects/transactional";
import type { Dump, Pull, Register } from "../objects/transactional";
import { ObjectTransactionalHandler } from "../objects/transactional";
export async function handleFileUpload(
@ -7,7 +7,7 @@ export async function handleFileUpload(
metadata: { [key: string]: string },
permissions: Array<string>,
max = -1,
): Promise<[string[], { [key: string]: string }, Pull, Dump] | undefined> {
): Promise<[string[], { [key: string]: string }, Pull, Dump, Register] | undefined> {
const formData = await readMultipartFormData(h3);
if (!formData) return undefined;
const transactionalHandler = new ObjectTransactionalHandler();
@ -28,5 +28,5 @@ export async function handleFileUpload(
options[entry.name] = entry.data.toString("utf-8");
}
return [ids, options, pull, dump];
return [ids, options, pull, dump, add];
}