From f6850fc795905afb942171fbe2aca74a7ba449d2 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Thu, 14 Aug 2025 16:50:42 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- electron/electron-down.js | 174 ++++++++++++++++++++++++++++++++++++++ electron/electron.js | 6 +- electron/package.json | 4 +- 3 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 electron/electron-down.js diff --git a/electron/electron-down.js b/electron/electron-down.js new file mode 100644 index 000000000..9b8fc6ea8 --- /dev/null +++ b/electron/electron-down.js @@ -0,0 +1,174 @@ +const loger = require("electron-log"); +const Store = require('electron-store'); +const store = new Store({ + name: 'download-manager', + defaults: { + downloadHistory: [], + } +}); +const electronDl = require("@dootask/electron-dl").default; + +class DownloadManager { + constructor() { + this.downloadHistory = store.get('downloadHistory', []); + } + + /** + * 转换下载项格式 + * @param {Electron.DownloadItem} downloadItem + */ + convertItem(downloadItem) { + return { + filename: downloadItem.getFilename(), + path: downloadItem.getSavePath(), + url: downloadItem.getURL(), + urls: downloadItem.getURLChain(), + mine: downloadItem.getMimeType(), + received: downloadItem.getReceivedBytes(), + total: downloadItem.getTotalBytes(), + percent: downloadItem.getPercentComplete(), + speed: downloadItem.getCurrentBytesPerSecond(), + state: downloadItem.getState(), + paused: downloadItem.isPaused(), + startTime: downloadItem.getStartTime(), + endTime: downloadItem.getEndTime(), + } + } + + /** + * 添加下载项 + * @param {Electron.DownloadItem} downloadItem + */ + addDownloadItem(downloadItem) { + this.downloadHistory.unshift({ + ...this.convertItem(downloadItem), + _source: downloadItem, + }); + store.set('downloadHistory', this.downloadHistory.slice(0, 100)); // 限制最多100个下载项 + loger.info(`Download item added: ${downloadItem.getSavePath()}`); + } + + /** + * 更新下载项 + * @param {string} path + */ + updateDownloadItem(path) { + const item = this.downloadHistory.find(d => d.path === path) + if (!item) { + loger.warn(`Download item not found for path: ${path}`); + return; + } + const downloadItem = item._source; + if (!downloadItem) { + loger.warn(`Download item not found for path: ${path}`); + return; + } + Object.assign(item, this.convertItem(downloadItem)) + store.set('downloadHistory', this.downloadHistory); + loger.info(`Download item updated: ${path} - ${item.state} (${item.percent}%)`); + } + + /** + * 暂停下载项 + * @param {string} path + */ + pauseDownloadItem(path) { + const item = this.downloadHistory.find(d => d.path === path) + if (!item) { + loger.warn(`Download item not found for path: ${path}`); + return; + } + const downloadItem = item._source; + if (!downloadItem) { + loger.warn(`Download item not found for path: ${path}`); + return; + } + downloadItem.pause(); + this.updateDownloadItem(path); + } + + /** + * 恢复下载项 + * @param {string} path + */ + resumeDownloadItem(path) { + const item = this.downloadHistory.find(d => d.path === path) + if (!item) { + loger.warn(`Download item not found for path: ${path}`); + return; + } + const downloadItem = item._source; + if (!downloadItem) { + loger.warn(`Download item not found for path: ${path}`); + return; + } + downloadItem.resume(); + this.updateDownloadItem(path); + } + + /** + * 取消下载项 + * @param {string} path + */ + cancelDownloadItem(path) { + const item = this.downloadHistory.find(d => d.path === path) + if (!item) { + loger.warn(`Download item not found for path: ${path}`); + return; + } + const downloadItem = item._source; + if (!downloadItem) { + loger.warn(`Download item not found for path: ${path}`); + return; + } + downloadItem.cancel(); + this.updateDownloadItem(path); + } + + /** + * 取消所有下载项 + */ + cancelAllDownloadItems() { + this.downloadHistory.forEach(item => { + this.cancelDownloadItem(item.path); + }); + } + + /** + * 清空下载历史 + */ + clearHistory() { + this.downloadHistory = []; + store.set('downloadHistory', []); + } +} + +const downloadManager = new DownloadManager(); + +function initialize(options = {}) { + // 下载配置 + electronDl({ + showBadge: false, + showProgressBar: false, + + ...options, + + onStarted: (item) => { + downloadManager.addDownloadItem(item); + }, + onProgress: (item) => { + downloadManager.updateDownloadItem(item.path); + }, + onCancel: (item) => { + downloadManager.updateDownloadItem(item.getSavePath()) + }, + onCompleted: (item) => { + downloadManager.updateDownloadItem(item.path); + } + }); +} + + +module.exports = { + initialize +} diff --git a/electron/electron.js b/electron/electron.js index 6f502c914..582603365 100644 --- a/electron/electron.js +++ b/electron/electron.js @@ -40,6 +40,7 @@ const PDFDocument = require('pdf-lib').PDFDocument; // 本地模块和配置 const utils = require('./utils'); const config = require('./package.json'); +const electronDown = require("./electron-down"); const electronMenu = require("./electron-menu"); // 实例初始化 @@ -122,6 +123,9 @@ if (!fs.existsSync(cacheDir)) { fs.mkdirSync(cacheDir, { recursive: true }); } +// 初始化下载配置 +electronDown.initialize() + /** * 启动web服务 */ @@ -2471,7 +2475,7 @@ async function saveFile(fileObject, data, origStat, overwrite, defEnc) { async function doSaveFile(isNew) { if (enableStoreBkp && !isNew) { - //Copy file to backup file (after conflict and stat is checked) + //Copy file to back up file (after conflict and stat is checked) let bkpFh; try { diff --git a/electron/package.json b/electron/package.json index a955dad59..46216c362 100755 --- a/electron/package.json +++ b/electron/package.json @@ -26,11 +26,14 @@ "url": "https://github.com/kuaifan/dootask.git" }, "devDependencies": { + "@dootask/electron-dl": "^4.0.0-rc.1", "@electron-forge/cli": "^7.8.3", "@electron-forge/maker-deb": "^7.8.3", "@electron-forge/maker-rpm": "^7.8.3", "@electron-forge/maker-squirrel": "^7.8.3", "@electron-forge/maker-zip": "^7.8.3", + "@types/crc": "^3.8.3", + "@types/electron-config": "^0.2.1", "dotenv": "^16.4.5", "electron": "^37.2.6", "electron-builder": "^26.0.12", @@ -44,7 +47,6 @@ "crc": "^3.8.0", "dayjs": "^1.11.13", "electron-config": "^2.0.0", - "electron-dl": "^4.0.0", "electron-log": "^5.4.2", "electron-screenshots-tool": "^1.1.2", "electron-squirrel-startup": "^1.0.1",