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
This commit is contained in:
DecDuck
2025-06-01 16:06:56 +10:00
committed by GitHub
parent 3e5c3678d5
commit 40e66def1e
6 changed files with 42 additions and 22 deletions

View File

@ -6,23 +6,21 @@ export async function handleFileUpload(
h3: H3Event<EventHandlerRequest>,
metadata: { [key: string]: string },
permissions: Array<string>,
): Promise<
[string | undefined, { [key: string]: string }, Pull, Dump] | undefined
> {
max = -1,
): Promise<[string[], { [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;
const ids = [];
for (const entry of formData) {
if (entry.filename) {
// Only pick one file
if (id) continue;
if (max > 0 && ids.length >= max) continue;
// Add file to transaction handler so we can void it later if we error out
id = add(entry.data);
ids.push(add(entry.data));
continue;
}
if (!entry.name) continue;
@ -30,5 +28,5 @@ export async function handleFileUpload(
options[entry.name] = entry.data.toString("utf-8");
}
return [id, options, pull, dump];
return [ids, options, pull, dump];
}