Files
drop/server/api/v1/admin/game/image/index.post.ts
DecDuck 40e66def1e Multi-upload to image library #56 (#60)
* feat: support for file upload handler to track multiple files

* feat: update image upload endpoint to allow multiple files

* fix: lint
2025-06-01 16:06:56 +10:00

59 lines
1.4 KiB
TypeScript

import aclManager from "~/server/internal/acls";
import prisma from "~/server/internal/db/database";
import { handleFileUpload } from "~/server/internal/utils/handlefileupload";
export default defineEventHandler(async (h3) => {
const allowed = await aclManager.allowSystemACL(h3, ["game:image:new"]);
if (!allowed) throw createError({ statusCode: 403 });
const form = await readMultipartFormData(h3);
if (!form)
throw createError({
statusCode: 400,
statusMessage: "This endpoint requires multipart form data.",
});
const uploadResult = await handleFileUpload(h3, {}, ["internal:read"]);
if (!uploadResult)
throw createError({
statusCode: 400,
statusMessage: "Failed to upload file",
});
const [ids, options, pull, dump] = uploadResult;
if (ids.length == 0) {
dump();
throw createError({
statusCode: 400,
statusMessage: "Did not upload a file",
});
}
const gameId = options.id;
if (!gameId)
throw createError({
statusCode: 400,
statusMessage: "No game ID attached",
});
const hasGame = (await prisma.game.count({ where: { id: gameId } })) != 0;
if (!hasGame) {
dump();
throw createError({ statusCode: 400, statusMessage: "Invalid game ID" });
}
const result = await prisma.game.update({
where: {
id: gameId,
},
data: {
mImageLibraryObjectIds: {
push: ids,
},
},
});
await pull();
return result;
});