'use strict'; import { getMainWindow } from 'ee-core/electron'; import { downloadService } from '../service/download/downloadService'; import { logger } from 'ee-core/log'; /** 进度推送通道:界面订阅它刷新"下载中"的进度与状态 */ export const DOWNLOAD_PROGRESS_CHANNEL = 'downloadCtl:progress'; /** 把 in-flight 状态推给渲染进程(窗口还没建好时忽略) */ function broadcast(item) { try { const win = getMainWindow(); if (win && !win.isDestroyed?.()) win.webContents.send(DOWNLOAD_PROGRESS_CHANNEL, item); } catch { // 窗口已关闭:忽略 } } /** * 下载管理器 IPC:通道名 controller/downloadCtl/<方法>(ee-core 按文件名自动注册)。 * * 统一返回项目约定 {success, data|message}:失败(路径非法/记录不存在/文件被移走)不抛到 Electron 层, * 前端看 message 即可提示。 */ class DownloadCtl { /** 保存一次下载:params { fileName, mime?, data(ArrayBuffer|base64), source? } */ async saveDownload(params) { try { return { success: true, data: downloadService.save(params) }; } catch (e) { return { success: false, message: this.message(e, '下载失败') }; } } /** * 后台下载:主进程流式拉取 URL 落盘并推送进度。 * params { url, fileName?, headers?, source?, mime? } */ async startDownload(params) { try { downloadService.setNotifier(broadcast); return { success: true, data: downloadService.start(params) }; } catch (e) { return { success: false, message: this.message(e, '下载失败') }; } } /** 下载清单 + 当前生效目录 */ async listDownloads() { try { return { success: true, data: { dir: downloadService.getDownloadDir(), items: downloadService.list() } }; } catch (e) { return { success: false, message: this.message(e, '读取下载列表失败') }; } } async getDownloadDir() { try { return { success: true, data: { dir: downloadService.getDownloadDir() } }; } catch (e) { return { success: false, message: this.message(e, '读取下载目录失败') }; } } /** 设置下载目录;params { dir } 传空恢复系统默认 */ async setDownloadDir(params) { try { return { success: true, data: { dir: downloadService.setDownloadDir(params?.dir) } }; } catch (e) { return { success: false, message: this.message(e, '设置下载目录失败') }; } } /** 用系统默认程序打开已下载的文件 */ async openDownloadFile(params) { try { return { success: true, data: await downloadService.openFile(params?.id) }; } catch (e) { return { success: false, message: this.message(e, '打开文件失败') }; } } /** 在文件管理器里定位已下载的文件 */ async revealDownload(params) { try { return { success: true, data: downloadService.revealFile(params?.id) }; } catch (e) { return { success: false, message: this.message(e, '定位文件失败') }; } } /** 打开下载目录 */ async openDownloadDir() { try { return { success: true, data: await downloadService.openDir() }; } catch (e) { return { success: false, message: this.message(e, '打开下载目录失败') }; } } /** 只从清单移除记录,不删磁盘上的文件 */ async removeDownload(params) { try { return { success: true, data: downloadService.remove(params?.id) }; } catch (e) { return { success: false, message: this.message(e, '移除记录失败') }; } } async clearDownloads() { try { return { success: true, data: downloadService.clear() }; } catch (e) { return { success: false, message: this.message(e, '清空记录失败') }; } } message(error, fallback) { const msg = error instanceof Error ? error.message : String(error); logger.warn('[downloadCtl]', fallback, msg); return msg || fallback; } } export default DownloadCtl;