downloadCtl.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 'use strict';
  2. import { getMainWindow } from 'ee-core/electron';
  3. import { downloadService } from '../service/download/downloadService';
  4. import { logger } from 'ee-core/log';
  5. /** 进度推送通道:界面订阅它刷新"下载中"的进度与状态 */
  6. export const DOWNLOAD_PROGRESS_CHANNEL = 'downloadCtl:progress';
  7. /** 把 in-flight 状态推给渲染进程(窗口还没建好时忽略) */
  8. function broadcast(item) {
  9. try {
  10. const win = getMainWindow();
  11. if (win && !win.isDestroyed?.()) win.webContents.send(DOWNLOAD_PROGRESS_CHANNEL, item);
  12. } catch {
  13. // 窗口已关闭:忽略
  14. }
  15. }
  16. /**
  17. * 下载管理器 IPC:通道名 controller/downloadCtl/<方法>(ee-core 按文件名自动注册)。
  18. *
  19. * 统一返回项目约定 {success, data|message}:失败(路径非法/记录不存在/文件被移走)不抛到 Electron 层,
  20. * 前端看 message 即可提示。
  21. */
  22. class DownloadCtl {
  23. /** 保存一次下载:params { fileName, mime?, data(ArrayBuffer|base64), source? } */
  24. async saveDownload(params) {
  25. try {
  26. return { success: true, data: downloadService.save(params) };
  27. } catch (e) {
  28. return { success: false, message: this.message(e, '下载失败') };
  29. }
  30. }
  31. /**
  32. * 后台下载:主进程流式拉取 URL 落盘并推送进度。
  33. * params { url, fileName?, headers?, source?, mime? }
  34. */
  35. async startDownload(params) {
  36. try {
  37. downloadService.setNotifier(broadcast);
  38. return { success: true, data: downloadService.start(params) };
  39. } catch (e) {
  40. return { success: false, message: this.message(e, '下载失败') };
  41. }
  42. }
  43. /** 下载清单 + 当前生效目录 */
  44. async listDownloads() {
  45. try {
  46. return { success: true, data: { dir: downloadService.getDownloadDir(), items: downloadService.list() } };
  47. } catch (e) {
  48. return { success: false, message: this.message(e, '读取下载列表失败') };
  49. }
  50. }
  51. async getDownloadDir() {
  52. try {
  53. return { success: true, data: { dir: downloadService.getDownloadDir() } };
  54. } catch (e) {
  55. return { success: false, message: this.message(e, '读取下载目录失败') };
  56. }
  57. }
  58. /** 设置下载目录;params { dir } 传空恢复系统默认 */
  59. async setDownloadDir(params) {
  60. try {
  61. return { success: true, data: { dir: downloadService.setDownloadDir(params?.dir) } };
  62. } catch (e) {
  63. return { success: false, message: this.message(e, '设置下载目录失败') };
  64. }
  65. }
  66. /** 用系统默认程序打开已下载的文件 */
  67. async openDownloadFile(params) {
  68. try {
  69. return { success: true, data: await downloadService.openFile(params?.id) };
  70. } catch (e) {
  71. return { success: false, message: this.message(e, '打开文件失败') };
  72. }
  73. }
  74. /** 在文件管理器里定位已下载的文件 */
  75. async revealDownload(params) {
  76. try {
  77. return { success: true, data: downloadService.revealFile(params?.id) };
  78. } catch (e) {
  79. return { success: false, message: this.message(e, '定位文件失败') };
  80. }
  81. }
  82. /** 打开下载目录 */
  83. async openDownloadDir() {
  84. try {
  85. return { success: true, data: await downloadService.openDir() };
  86. } catch (e) {
  87. return { success: false, message: this.message(e, '打开下载目录失败') };
  88. }
  89. }
  90. /** 只从清单移除记录,不删磁盘上的文件 */
  91. async removeDownload(params) {
  92. try {
  93. return { success: true, data: downloadService.remove(params?.id) };
  94. } catch (e) {
  95. return { success: false, message: this.message(e, '移除记录失败') };
  96. }
  97. }
  98. async clearDownloads() {
  99. try {
  100. return { success: true, data: downloadService.clear() };
  101. } catch (e) {
  102. return { success: false, message: this.message(e, '清空记录失败') };
  103. }
  104. }
  105. message(error, fallback) {
  106. const msg = error instanceof Error ? error.message : String(error);
  107. logger.warn('[downloadCtl]', fallback, msg);
  108. return msg || fallback;
  109. }
  110. }
  111. export default DownloadCtl;