From d59bd1050be9337ac6391ac7126f3abf6bbd7175 Mon Sep 17 00:00:00 2001 From: Rory& Date: Sun, 28 Sep 2025 20:32:34 +0200 Subject: [PATCH] Use default ID on file upload requests --- src/api/routes/channels/#channel_id/attachments.ts | 14 ++++++++++++++ src/util/schemas/UploadAttachmentRequestSchema.ts | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/api/routes/channels/#channel_id/attachments.ts b/src/api/routes/channels/#channel_id/attachments.ts index 555eb0ad..0e0185d1 100644 --- a/src/api/routes/channels/#channel_id/attachments.ts +++ b/src/api/routes/channels/#channel_id/attachments.ts @@ -69,6 +69,20 @@ router.post( const cdnUrl = Config.get().cdn.endpointPublic; const batchId = `CLOUD_${user.id}_${randomString(128)}`; + + // validate IDs + const seenIds: (string | undefined)[] = []; + for (const file of payload.files) { + file.id ??= "0"; + if (seenIds.includes(file.id)) { + return res.status(400).json({ + code: 400, + message: `Duplicate attachment ID: ${file.id}`, + }); + } + seenIds.push(file.id); + } + const attachments = await Promise.all( payload.files.map(async (attachment) => { attachment.filename = attachment.filename.replaceAll(" ", "_").replace(/[^a-zA-Z0-9._]+/g, ""); diff --git a/src/util/schemas/UploadAttachmentRequestSchema.ts b/src/util/schemas/UploadAttachmentRequestSchema.ts index 06cb65f1..60bf89f6 100644 --- a/src/util/schemas/UploadAttachmentRequestSchema.ts +++ b/src/util/schemas/UploadAttachmentRequestSchema.ts @@ -22,7 +22,7 @@ export interface UploadAttachmentRequestSchema { } export interface UploadAttachmentRequest { - id: string; + id?: string; filename: string; file_size: number; is_clip?: boolean;