mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-18 18:51:13 +10:00
feat: beginnings of platform & redist management
This commit is contained in:
@ -5,7 +5,6 @@ import * as jdenticon from "jdenticon";
|
||||
import prisma from "~/server/internal/db/database";
|
||||
import libraryManager from "~/server/internal/library";
|
||||
import jsdom from "jsdom";
|
||||
import DOMPurify from 'dompurify';
|
||||
|
||||
export const ImportRedist = type({
|
||||
library: "string",
|
||||
@ -28,7 +27,8 @@ export default defineEventHandler(async (h3) => {
|
||||
const body = await handleFileUpload(h3, {}, ["internal:read"], 1);
|
||||
if (!body) throw createError({ statusCode: 400, message: "Body required." });
|
||||
|
||||
const [[id], rawOptions, pull, , add] = body;
|
||||
const [ids, rawOptions, pull, , add] = body;
|
||||
const id = ids.at(0);
|
||||
|
||||
const options = ImportRedist(rawOptions);
|
||||
if (options instanceof ArkErrors)
|
||||
@ -48,7 +48,7 @@ export default defineEventHandler(async (h3) => {
|
||||
|
||||
let svgContent = "";
|
||||
if (options.platform) {
|
||||
// This logic is duplicated on the client to make viewing there possible.
|
||||
// This logic is duplicated on the client to make viewing there possible.
|
||||
// TODO?: refactor into a single function. Not totally sure if this is a good idea though,
|
||||
// because they do different things
|
||||
const dom = new jsdom.JSDOM(options.platform.icon);
|
||||
@ -60,7 +60,7 @@ export default defineEventHandler(async (h3) => {
|
||||
});
|
||||
svg.removeAttribute("width");
|
||||
svg.removeAttribute("height");
|
||||
svgContent = DOMPurify.sanitize(svg.outerHTML, {USE_PROFILES: {svg: true, svgFilters: true}});
|
||||
svgContent = svg.outerHTML;
|
||||
}
|
||||
|
||||
const redist = await prisma.redist.create({
|
||||
|
||||
@ -7,9 +7,15 @@ export default defineEventHandler(async (h3) => {
|
||||
|
||||
const unimportedGames = await libraryManager.fetchUnimportedGames();
|
||||
const games = await libraryManager.fetchGamesWithStatus();
|
||||
const redists = await libraryManager.fetchRedistsWithStatus();
|
||||
const libraries = await libraryManager.fetchLibraries();
|
||||
|
||||
// Fetch other library data here
|
||||
|
||||
return { unimportedGames, games, hasLibraries: libraries.length > 0 };
|
||||
return {
|
||||
unimportedGames,
|
||||
games,
|
||||
redists,
|
||||
hasLibraries: libraries.length > 0,
|
||||
};
|
||||
});
|
||||
|
||||
19
server/api/v1/admin/redist/[id]/index.delete.ts
Normal file
19
server/api/v1/admin/redist/[id]/index.delete.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import aclManager from "~/server/internal/acls";
|
||||
import prisma from "~/server/internal/db/database";
|
||||
|
||||
export default defineEventHandler(async (h3) => {
|
||||
const allowed = await aclManager.allowSystemACL(h3, ["redist:delete"]);
|
||||
if (!allowed) throw createError({ statusCode: 403 });
|
||||
|
||||
const id = getRouterParam(h3, "id")!;
|
||||
|
||||
const { count } = await prisma.redist.deleteMany({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
});
|
||||
|
||||
if (count == 0) throw createError({ statusCode: 404 });
|
||||
|
||||
return;
|
||||
});
|
||||
16
server/api/v1/admin/redist/index.get.ts
Normal file
16
server/api/v1/admin/redist/index.get.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import aclManager from "~/server/internal/acls";
|
||||
import prisma from "~/server/internal/db/database";
|
||||
|
||||
export default defineEventHandler(async (h3) => {
|
||||
const allowed = await aclManager.allowSystemACL(h3, ["redist:read"]);
|
||||
if (!allowed) throw createError({ statusCode: 403 });
|
||||
|
||||
return await prisma.redist.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
mName: true,
|
||||
mShortDescription: true,
|
||||
mIconObjectId: true,
|
||||
},
|
||||
});
|
||||
});
|
||||
@ -73,6 +73,10 @@ export const systemACLDescriptions: ObjectFromList<typeof systemACLs> = {
|
||||
"game:image:new": "Upload an image for a game.",
|
||||
"game:image:delete": "Delete an image for a game.",
|
||||
|
||||
"redist:read": "Fetch redistributables on this instance.",
|
||||
"redist:update": "Update redistributables on this instance.",
|
||||
"redist:delete": "Delete redistributables on this instance.",
|
||||
|
||||
"company:read": "Fetch companies.",
|
||||
"company:create": "Create a new company.",
|
||||
"company:update": "Update existing companies.",
|
||||
|
||||
@ -67,6 +67,10 @@ export const systemACLs = [
|
||||
"game:image:new",
|
||||
"game:image:delete",
|
||||
|
||||
"redist:read",
|
||||
"redist:update",
|
||||
"redist:delete",
|
||||
|
||||
"company:read",
|
||||
"company:update",
|
||||
"company:create",
|
||||
|
||||
@ -128,29 +128,23 @@ class LibraryManager {
|
||||
}
|
||||
}
|
||||
|
||||
async fetchGamesWithStatus() {
|
||||
const games = await prisma.game.findMany({
|
||||
include: {
|
||||
versions: {
|
||||
select: {
|
||||
versionName: true,
|
||||
},
|
||||
},
|
||||
library: true,
|
||||
},
|
||||
orderBy: {
|
||||
mName: "asc",
|
||||
},
|
||||
});
|
||||
|
||||
async fetchLibraryObjectWithStatus<T>(
|
||||
objects: Array<
|
||||
{
|
||||
libraryId: string;
|
||||
libraryPath: string;
|
||||
versions: Array<unknown>;
|
||||
} & T
|
||||
>,
|
||||
) {
|
||||
return await Promise.all(
|
||||
games.map(async (e) => {
|
||||
objects.map(async (e) => {
|
||||
const versions = await this.fetchUnimportedGameVersions(
|
||||
e.libraryId ?? "",
|
||||
e.libraryPath,
|
||||
);
|
||||
return {
|
||||
game: e,
|
||||
value: e,
|
||||
status: versions
|
||||
? {
|
||||
noVersions: e.versions.length == 0,
|
||||
@ -162,6 +156,55 @@ class LibraryManager {
|
||||
);
|
||||
}
|
||||
|
||||
async fetchGamesWithStatus() {
|
||||
const games = await prisma.game.findMany({
|
||||
include: {
|
||||
versions: {
|
||||
select: {
|
||||
versionId: true,
|
||||
versionName: true,
|
||||
},
|
||||
},
|
||||
library: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
mName: "asc",
|
||||
},
|
||||
});
|
||||
|
||||
return await this.fetchLibraryObjectWithStatus(games);
|
||||
}
|
||||
|
||||
async fetchRedistsWithStatus() {
|
||||
const redists = await prisma.redist.findMany({
|
||||
include: {
|
||||
versions: {
|
||||
select: {
|
||||
versionId: true,
|
||||
versionName: true,
|
||||
},
|
||||
},
|
||||
library: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
platform: true,
|
||||
},
|
||||
orderBy: {
|
||||
mName: "asc",
|
||||
},
|
||||
});
|
||||
|
||||
return await this.fetchLibraryObjectWithStatus(redists);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches recommendations and extra data about the version. Doesn't actually check if it's been imported.
|
||||
* @param gameId
|
||||
|
||||
@ -1,11 +1,9 @@
|
||||
import { LibraryBackend } from "~/prisma/client/enums";
|
||||
import type { LibraryBackend } from "~/prisma/client/enums";
|
||||
import prisma from "../internal/db/database";
|
||||
import type { JsonValue } from "@prisma/client/runtime/library";
|
||||
import type { LibraryProvider } from "../internal/library/provider";
|
||||
import type { FilesystemProviderConfig } from "../internal/library/providers/filesystem";
|
||||
import { FilesystemProvider } from "../internal/library/providers/filesystem";
|
||||
import libraryManager from "../internal/library";
|
||||
import path from "path";
|
||||
import { FlatFilesystemProvider } from "../internal/library/providers/flat";
|
||||
import { logger } from "~/server/internal/logging";
|
||||
|
||||
@ -33,42 +31,6 @@ export default defineNitroPlugin(async () => {
|
||||
let successes = 0;
|
||||
const libraries = await prisma.library.findMany({});
|
||||
|
||||
// Add migration handler
|
||||
const legacyPath = process.env.LIBRARY;
|
||||
if (legacyPath && libraries.length == 0) {
|
||||
const options: typeof FilesystemProviderConfig.infer = {
|
||||
baseDir: path.resolve(legacyPath),
|
||||
};
|
||||
|
||||
const library = await prisma.library.create({
|
||||
data: {
|
||||
name: "Auto-created",
|
||||
backend: LibraryBackend.Filesystem,
|
||||
options,
|
||||
},
|
||||
});
|
||||
|
||||
libraries.push(library);
|
||||
|
||||
// Update all existing games
|
||||
await prisma.game.updateMany({
|
||||
where: {
|
||||
libraryId: null,
|
||||
},
|
||||
data: {
|
||||
libraryId: library.id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Delete all games that don't have a library provider after the legacy handler
|
||||
// (leftover from a bug)
|
||||
await prisma.game.deleteMany({
|
||||
where: {
|
||||
libraryId: null,
|
||||
},
|
||||
});
|
||||
|
||||
for (const library of libraries) {
|
||||
const constructor = libraryConstructors[library.backend];
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user