upload images to games
This commit is contained in:
@@ -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];
|
||||
}
|
||||
Reference in New Issue
Block a user