(url, formData, onProgress)
| 303 | } |
| 304 | |
| 305 | function xhrJson(url, formData, onProgress) { |
| 306 | return new Promise((resolve, reject) => { |
| 307 | const xhr = new XMLHttpRequest(); |
| 308 | xhr.open('POST', url, true); |
| 309 | if (typeof onProgress === 'function') { |
| 310 | xhr.upload.addEventListener('progress', onProgress); |
| 311 | } |
| 312 | xhr.addEventListener('load', function () { |
| 313 | let data = null; |
| 314 | let rawMessage = ''; |
| 315 | try { |
| 316 | data = JSON.parse(xhr.responseText || '{}'); |
| 317 | } catch (e) { |
| 318 | rawMessage = String(xhr.responseText || '').replace(/\s+/g, ' ').trim(); |
| 319 | if (rawMessage.length > 180) { |
| 320 | rawMessage = rawMessage.slice(0, 180) + '...'; |
| 321 | } |
| 322 | data = { error: rawMessage || tx('share_upload_failed', null, 'Upload failed.') }; |
| 323 | } |
| 324 | if (xhr.status >= 200 && xhr.status < 300 && !data.error) { |
| 325 | resolve(data); |
| 326 | return; |
| 327 | } |
| 328 | const msg = data && data.error |
| 329 | ? String(data.error) |
| 330 | : tx('share_upload_failed_http', { |
| 331 | code: xhr.status, |
| 332 | reason: tx('share_upload_failed', null, 'Upload failed.'), |
| 333 | }, 'Upload failed (HTTP ' + xhr.status + ').'); |
| 334 | const err = new Error(msg); |
| 335 | err.status = xhr.status || 0; |
| 336 | reject(err); |
| 337 | }); |
| 338 | xhr.addEventListener('error', function () { |
| 339 | const err = new Error(tx('share_upload_network_error', null, 'Network error. Please check your connection and try again.')); |
| 340 | err.status = 0; |
| 341 | reject(err); |
| 342 | }); |
| 343 | xhr.send(formData); |
| 344 | }); |
| 345 | } |
| 346 | |
| 347 | const queue = []; |
| 348 | const rows = new Map(); |
no test coverage detected