(link = false)
| 510 | } |
| 511 | |
| 512 | async function download(link = false) |
| 513 | { |
| 514 | link = link || downloadLink; |
| 515 | |
| 516 | if(link) |
| 517 | { |
| 518 | const downloadOpdsFolder = relative.resolve(config.downloadOpdsFolder); |
| 519 | fileManager.macosStartAccessingSecurityScopedResource(downloadOpdsFolder); |
| 520 | |
| 521 | const button = document.querySelector('.dialog .file-info-odps-button-'+link.button); |
| 522 | events.buttonLoading(button, true); |
| 523 | |
| 524 | const fileName = getFileName(link); |
| 525 | const downloadPath = fileManager.genearteFilePath(downloadOpdsFolder, fileName); |
| 526 | const _downloadPath = escapeQuotes(escapeBackSlash(downloadPath)); |
| 527 | |
| 528 | if(!fs.existsSync(downloadPath)) |
| 529 | { |
| 530 | const response = await auth.fetch(link.url); |
| 531 | |
| 532 | if(response.ok) |
| 533 | { |
| 534 | const len = parseInt(response.headers.get('content-length'), 10); |
| 535 | |
| 536 | const reader = response.body.getReader(); |
| 537 | const fileStream = fs.createWriteStream(downloadPath); |
| 538 | |
| 539 | let downloaded = 0; |
| 540 | |
| 541 | while(true) |
| 542 | { |
| 543 | const {done, value} = await reader.read(); |
| 544 | |
| 545 | if(done) |
| 546 | break; |
| 547 | |
| 548 | fileStream.write(value); |
| 549 | downloaded += value.byteLength; |
| 550 | |
| 551 | events.buttonLoading(button, downloaded / len); |
| 552 | } |
| 553 | |
| 554 | events.buttonLoading(button, 1); |
| 555 | |
| 556 | let resolve; |
| 557 | |
| 558 | const promise = new Promise(function(_resolve){ |
| 559 | |
| 560 | resolve = _resolve; |
| 561 | |
| 562 | }); |
| 563 | |
| 564 | fileStream.end(resolve); |
| 565 | |
| 566 | await promise; |
| 567 | } |
| 568 | else |
| 569 | { |
no test coverage detected