(image)
| 178 | } |
| 179 | |
| 180 | async function startImageUpload(image) { |
| 181 | if (!image || !S.ws || S.ws.readyState !== WebSocket.OPEN) { |
| 182 | throw new Error('Connection unavailable'); |
| 183 | } |
| 184 | |
| 185 | image.status = 'uploading'; |
| 186 | image.progress = 0; |
| 187 | image.uploadedBytes = 0; |
| 188 | updateImagePreviewUi(); |
| 189 | |
| 190 | const totalChunks = Math.max(1, Math.ceil(image.file.size / IMAGE_CHUNK_BYTES)); |
| 191 | let waitForStatus = waitForUploadStatus(image.uploadId, ['ready_for_chunks']); |
| 192 | S.ws.send(JSON.stringify({ |
| 193 | type: 'image_upload_init', |
| 194 | uploadId: image.uploadId, |
| 195 | totalBytes: image.file.size, |
| 196 | totalChunks, |
| 197 | mediaType: image.mediaType, |
| 198 | name: image.name, |
| 199 | })); |
| 200 | await waitForStatus; |
| 201 | |
| 202 | for (let index = 0; index < totalChunks; index++) { |
| 203 | const start = index * IMAGE_CHUNK_BYTES; |
| 204 | const end = Math.min(image.file.size, start + IMAGE_CHUNK_BYTES); |
| 205 | const base64 = await fileChunkToBase64(image.file.slice(start, end)); |
| 206 | waitForStatus = waitForUploadStatus(image.uploadId, ['uploading'], msg => msg.chunkIndex === index); |
| 207 | S.ws.send(JSON.stringify({ |
| 208 | type: 'image_upload_chunk', |
| 209 | uploadId: image.uploadId, |
| 210 | index, |
| 211 | base64, |
| 212 | })); |
| 213 | await waitForStatus; |
| 214 | } |
| 215 | |
| 216 | waitForStatus = waitForUploadStatus(image.uploadId, ['uploaded']); |
| 217 | S.ws.send(JSON.stringify({ type: 'image_upload_complete', uploadId: image.uploadId })); |
| 218 | await waitForStatus; |
| 219 | |
| 220 | const currentImage = pendingImage; |
| 221 | if (currentImage && currentImage.uploadId === image.uploadId && currentImage.submitQueued) { |
| 222 | await submitPendingImageUpload(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | export function initImageUpload() { |
| 227 | $('btn-image').addEventListener('click', () => { |
no test coverage detected