| 52 | } |
| 53 | |
| 54 | async function buildFormDataBody(body, senderUrl) { |
| 55 | let host = senderUrl.split("/#/")[0]; |
| 56 | if (host.endsWith("/")) { |
| 57 | host = host.slice(0, -1); |
| 58 | } |
| 59 | const formData = new FormData(); |
| 60 | if (!body) { |
| 61 | return formData; |
| 62 | } |
| 63 | for (const part of body) { |
| 64 | if (part.isEnabled === false) { |
| 65 | continue; |
| 66 | } |
| 67 | if (part.type === "file") { |
| 68 | const fileId = part.file?.id; |
| 69 | if (!fileId) { |
| 70 | continue; |
| 71 | } |
| 72 | const filename = part.file?.name || "unkown-filename"; |
| 73 | const res = await fetch(`${host}/api/files/${fileId}`); |
| 74 | if (res.status !== 200) { |
| 75 | throw new Error(`Failed to fetch file: ${filename}`); |
| 76 | } |
| 77 | const blob = await res.blob(); |
| 78 | const file = new File([blob], filename, { type: blob.type }); |
| 79 | formData.append(part.key, file); |
| 80 | } else { |
| 81 | formData.append(part.key, part.value); |
| 82 | } |
| 83 | } |
| 84 | return formData; |
| 85 | } |
| 86 | |
| 87 | function connectWebsocket(request, senderTabId, sendResponse) { |
| 88 | const wsId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); |