From a48381bc3daa755b823e26ead1a173210d058d97 Mon Sep 17 00:00:00 2001 From: hugoalh Date: Sun, 8 Sep 2024 14:49:20 +0800 Subject: [PATCH] Improve file upload --- _payload.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/_payload.ts b/_payload.ts index adcf17af..ffc1077d 100644 --- a/_payload.ts +++ b/_payload.ts @@ -358,11 +358,16 @@ async function resolveFilesFormData(workspace: string, files: string[]): Promise for (let index = 0; index < files.length; index += 1) { const file: string = files[index]; const fileBasename: string = pathBasename(file); - attachments.push({ + const fileContentType: string | undefined = contentType(pathExtname(file)); + const attachment: JSONObject = { filename: fileBasename, id: index - }); - formData.append(`files[${index}]`, new Blob([await Deno.readFile(pathJoin(workspace, file))], { type: contentType(pathExtname(file)) }), fileBasename); + }; + if (typeof fileContentType !== "undefined") { + attachment.content_type = fileContentType; + } + attachments.push(attachment); + formData.append(`files[${index}]`, new Blob([await Deno.readFile(pathJoin(workspace, file))], { type: fileContentType }), fileBasename); } formData.append("attachments", JSON.stringify(attachments)); return formData;