mirror of
https://github.com/kuaifan/dootask.git
synced 2025-12-11 18:42:54 +00:00
perf: 优化下载工具
This commit is contained in:
parent
c0b4674568
commit
f6850fc795
174
electron/electron-down.js
vendored
Normal file
174
electron/electron-down.js
vendored
Normal file
@ -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
|
||||
}
|
||||
6
electron/electron.js
vendored
6
electron/electron.js
vendored
@ -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 {
|
||||
|
||||
@ -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",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user