mirror of
https://github.com/Drop-OSS/drop.git
synced 2026-01-31 15:37:09 +01:00
* 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:
@@ -20,8 +20,8 @@ export default defineEventHandler(async (h3) => {
|
||||
statusMessage: "Failed to upload file",
|
||||
});
|
||||
|
||||
const [id, options, pull, dump] = uploadResult;
|
||||
if (!id) {
|
||||
const [ids, options, pull, dump] = uploadResult;
|
||||
if (ids.length == 0) {
|
||||
dump();
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
@@ -48,7 +48,7 @@ export default defineEventHandler(async (h3) => {
|
||||
},
|
||||
data: {
|
||||
mImageLibraryObjectIds: {
|
||||
push: id,
|
||||
push: ids,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -14,14 +14,16 @@ export default defineEventHandler(async (h3) => {
|
||||
statusMessage: "This endpoint requires multipart form data.",
|
||||
});
|
||||
|
||||
const uploadResult = await handleFileUpload(h3, {}, ["internal:read"]);
|
||||
const uploadResult = await handleFileUpload(h3, {}, ["internal:read"], 1);
|
||||
if (!uploadResult)
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: "Failed to upload file",
|
||||
});
|
||||
|
||||
const [id, options, pull, dump] = uploadResult;
|
||||
const [ids, options, pull, dump] = uploadResult;
|
||||
|
||||
const id = ids.at(0);
|
||||
|
||||
// handleFileUpload reads the rest of the options for us.
|
||||
const name = options.name;
|
||||
|
||||
@@ -14,19 +14,20 @@ export default defineEventHandler(async (h3) => {
|
||||
statusMessage: "This endpoint requires multipart form data.",
|
||||
});
|
||||
|
||||
const uploadResult = await handleFileUpload(h3, {}, ["internal:read"]);
|
||||
const uploadResult = await handleFileUpload(h3, {}, ["internal:read"], 1);
|
||||
if (!uploadResult)
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: "Failed to upload file",
|
||||
});
|
||||
|
||||
const [imageId, options, pull, _dump] = uploadResult;
|
||||
const [imageIds, options, pull, _dump] = uploadResult;
|
||||
|
||||
const title = options.title;
|
||||
const description = options.description;
|
||||
const content = options.content;
|
||||
const tags = options.tags ? (JSON.parse(options.tags) as string[]) : [];
|
||||
const imageId = imageIds.at(0);
|
||||
|
||||
if (!title || !description || !content)
|
||||
throw createError({
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user