(url, filePath, httpHeaders, httpOptions, needBinary)
| 438 | } |
| 439 | |
| 440 | function uploadHttpFile (url, filePath, httpHeaders, httpOptions, needBinary) { |
| 441 | return new Promise((accept, reject) => { |
| 442 | const parsed = new URL(url); |
| 443 | const protocol = (parsed.protocol === 'https:' ? https : http); |
| 444 | |
| 445 | const file = fs.createReadStream(filePath); |
| 446 | const stat = fs.statSync(filePath); |
| 447 | |
| 448 | const options = Object.assign({ |
| 449 | host: parsed.host, |
| 450 | path: parsed.pathname + parsed.search, |
| 451 | method: 'POST', |
| 452 | headers: Object.assign({ |
| 453 | 'Content-Type': 'application/octet-stream', |
| 454 | 'Content-Length': stat.size |
| 455 | }, httpHeaders || {}) |
| 456 | }, httpOptions || {}); |
| 457 | |
| 458 | const req = protocol.request(options, (res) => { |
| 459 | toUtf8(res, (contentType, content) => { |
| 460 | accept({statusCode: res.statusCode, statusMessage: res.statusMessage, content, contentType}); |
| 461 | }, needBinary); |
| 462 | }); |
| 463 | |
| 464 | req.on('error', reject); |
| 465 | file.pipe(req); |
| 466 | }); |
| 467 | } |
| 468 | |
| 469 | function toUtf8 (res, onDone, needBinary) { |
| 470 | const contentType = (res.headers['content-type'] || '').split(';'); |
no test coverage detected