upload images to games

This commit is contained in:
DecDuck
2024-10-12 12:09:14 +11:00
parent 27070b6a4c
commit 9b7ee4e746
11 changed files with 348 additions and 12 deletions
+31
View File
@@ -0,0 +1,31 @@
import { EventHandlerRequest, H3Event } from "h3";
import { Dump, ObjectTransactionalHandler, Pull } from "../objects/transactional";
export async function handleFileUpload(
h3: H3Event<EventHandlerRequest>,
metadata: { [key: string]: string },
permissions: Array<string>
): Promise<[string | undefined, {[key: string]: string}, Pull, Dump] | undefined> {
const formData = await readMultipartFormData(h3);
if (!formData) return undefined;
const transactionalHandler = new ObjectTransactionalHandler();
const [add, pull, dump] = transactionalHandler.new(metadata, permissions);
const options: { [key: string]: string } = {};
let id;
for (const entry of formData) {
if (entry.filename) {
// Only pick one file
if (id) continue;
// Add file to transaction handler so we can void it later if we error out
id = add(entry.data);
continue;
}
if (!entry.name) continue;
options[entry.name] = entry.data.toString("utf-8");
}
return [id, options, pull, dump];
}