* 开始下载任务 * @param strategy 下载策略
(strategy: DownloadStrategy)
| 546 | * @param strategy 下载策略 |
| 547 | */ |
| 548 | private async startTask(strategy: DownloadStrategy) { |
| 549 | this.activeDownloads.add(strategy.id); |
| 550 | const dataStore = useDataStore(); |
| 551 | dataStore.updateDownloadStatus(strategy.id, "downloading"); |
| 552 | |
| 553 | try { |
| 554 | await strategy.prepare(); |
| 555 | const config = strategy.getDownloadConfig(); |
| 556 | |
| 557 | if (isElectron) { |
| 558 | if (!strategy.downloadUrl) throw new Error("Download URL missing"); |
| 559 | |
| 560 | const downloadResult = await window.electron.ipcRenderer.invoke( |
| 561 | "download-file", |
| 562 | strategy.downloadUrl, |
| 563 | config, |
| 564 | ); |
| 565 | |
| 566 | if (downloadResult.status === "success" || downloadResult.status === "skipped") { |
| 567 | await strategy.postProcess(downloadResult.path || config.path); // IPC 返回结果通常包含路径 |
| 568 | dataStore.removeDownloadingSong(strategy.id); |
| 569 | window.$message.success(`${strategy.name} 下载完成`); |
| 570 | } else { |
| 571 | if (downloadResult.status === "cancelled") { |
| 572 | // 已取消,无需处理 |
| 573 | } else { |
| 574 | throw new Error(downloadResult.message || "下载失败"); |
| 575 | } |
| 576 | } |
| 577 | } else { |
| 578 | // 浏览器端兜底处理 |
| 579 | if (!strategy.downloadUrl) throw new Error("Download URL missing"); |
| 580 | saveAs(strategy.downloadUrl, config.fileName + "." + config.fileType); |
| 581 | dataStore.removeDownloadingSong(strategy.id); |
| 582 | } |
| 583 | } catch (error: any) { |
| 584 | console.error(`Error processing task ${strategy.name} (ID: ${strategy.id}):`, error); |
| 585 | if (error?.message) console.error("Error message:", error.message); |
| 586 | |
| 587 | dataStore.markDownloadFailed(strategy.id); |
| 588 | window.$message.error(error.message || "下载出错"); |
| 589 | } finally { |
| 590 | this.activeDownloads.delete(strategy.id); |
| 591 | this.processQueue(); |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | export const downloadManager = new DownloadManager(); |
no test coverage detected