mirror of
https://github.com/HugoAura/Seewo-HugoAura.git
synced 2026-06-21 15:44:26 +08:00
[🚧 Fix] <PLS & FS> Improve PLS download logic & UX
1. [+] 增加了 PLS 下载操作的取消功能 2. [/] 修复了 FS IPC 中 `downloadFile` 时, 过早地从 downloadTasks 中删除任务的逻辑错误。 3. [↑] 改进了 PLS IPC 中 `handlePLSDownload` 获取版本信息时的逻辑, 现在该函数会从全局 API 信息中逐个尝试 API 域名。减少了极端网络环境下, 版本信息获取失败的可能性。 4. [/] 修复了下载失败后, 下载按钮依然保持灰显的问题。 5. [+] 为 PLS 下载增加了进度条显示。 6. [/] 优化了 `plsConnectionManager` 中一些不必要的 IPC 状态同步 (有些时候还会导致逻辑错误)。
This commit is contained in:
@@ -38,10 +38,13 @@ const composableFunctions = {
|
||||
progressCallback(failedTemplate);
|
||||
return false;
|
||||
}
|
||||
if (!fs.existsSync(path.dirname(targetPath))) {
|
||||
failedTemplate.message = "Path not exists";
|
||||
progressCallback(failedTemplate);
|
||||
|
||||
const dirName = path.dirname(targetPath);
|
||||
|
||||
if (!fs.existsSync(dirName)) {
|
||||
fs.mkdirSync(dirName);
|
||||
}
|
||||
|
||||
const httpModuleIns = url.startsWith("https") ? nodeHttps : nodeHttp;
|
||||
|
||||
global.__HUGO_AURA__.fsTasks?.downloadTasks.set(taskId, {
|
||||
@@ -49,6 +52,14 @@ const composableFunctions = {
|
||||
cancelReq: null,
|
||||
});
|
||||
|
||||
progressCallback({
|
||||
id: taskId,
|
||||
progress: 0,
|
||||
status: "waiting",
|
||||
dlUrl: url,
|
||||
savePath: targetPath,
|
||||
});
|
||||
|
||||
const fsStream = fs.createWriteStream(targetPath);
|
||||
|
||||
const dlReq = httpModuleIns.get(url, (response) => {
|
||||
@@ -64,9 +75,12 @@ const composableFunctions = {
|
||||
const totalBytes = parseInt(contentLength, 10) || 0; // No error handling 😆
|
||||
let curRecvBytes = 0;
|
||||
|
||||
let hasCancelled = false;
|
||||
|
||||
global.__HUGO_AURA__.fsTasks?.downloadTasks.set(taskId, {
|
||||
status: "progressing",
|
||||
cancelReq: () => {
|
||||
hasCancelled = true;
|
||||
dlReq.destroy();
|
||||
fsStream.close();
|
||||
fs.unlink(targetPath, () => {});
|
||||
@@ -102,6 +116,9 @@ const composableFunctions = {
|
||||
|
||||
fsStream.on("finish", () => {
|
||||
fsStream.close();
|
||||
if (hasCancelled) {
|
||||
return;
|
||||
}
|
||||
progressCallback({
|
||||
id: taskId,
|
||||
progress: (100).toFixed(2),
|
||||
@@ -111,9 +128,9 @@ const composableFunctions = {
|
||||
dlUrl: url,
|
||||
savePath: targetPath,
|
||||
});
|
||||
global.__HUGO_AURA__.fsTasks?.downloadTasks.delete(taskId);
|
||||
});
|
||||
|
||||
global.__HUGO_AURA__.fsTasks?.downloadTasks.delete(taskId);
|
||||
return true;
|
||||
});
|
||||
|
||||
@@ -123,7 +140,10 @@ const composableFunctions = {
|
||||
failedTemplate.message =
|
||||
"Request error: Unexpected error while downloading file";
|
||||
failedTemplate.errorObj = e;
|
||||
console.error(`[HugoAura / IPC / FS / ERROR] Error downloading file from ${url}, errorObj:`, e);
|
||||
console.error(
|
||||
`[HugoAura / IPC / FS / ERROR] Error downloading file from ${url}, errorObj:`,
|
||||
e
|
||||
);
|
||||
progressCallback(failedTemplate);
|
||||
global.__HUGO_AURA__.fsTasks?.downloadTasks.delete(taskId);
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user