Skip to content
This repository was archived by the owner on Jul 17, 2026. It is now read-only.

refactor(download): 重构下载进度跟踪和多线程下载逻辑 - 提取 ProgressTracker 结构体统一管理进度更新,避免重复代码 - 简化多线程下载实现,使用 futures_util 替代手动任务管理 - 改进文件大小探测逻辑,增强 Range 请求处理 - 优化元数据写入函数,提取标签获取逻辑 - #823

Merged
kazukokawagawa merged 10 commits into
devfrom
dev-font
Feb 4, 2026

Conversation

@kazukokawagawa

Copy link
Copy Markdown
Collaborator

No description provided.

- 提取 ProgressTracker 结构体统一管理进度更新,避免重复代码
- 简化多线程下载实现,使用 futures_util 替代手动任务管理
- 改进文件大小探测逻辑,增强 Range 请求处理
- 优化元数据写入函数,提取标签获取逻辑
将下载进度回调从 JSON 字符串改为直接传递 NAPI 结构体,提升性能并简化代码
移除 serde 依赖,将进度字段类型从 u64 改为 f64 以兼容 NAPI
改进错误信息,使其更具描述性
统一进度回调参数处理,支持从Rust返回的snake_case对象和JSON字符串两种格式。移除重复的类型定义,将DownloadProgress接口移至类型声明文件。
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @kazukokawagawa, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求主要对下载模块进行了全面的重构和优化。核心目标是提升下载进度跟踪的准确性、多线程下载的效率和稳定性,以及文件元数据写入的健壮性。通过引入新的进度跟踪机制、简化并发下载模型和增强文件信息探测,整体下载体验和代码质量得到了显著改善。

Highlights

  • 下载进度统一管理: 引入 ProgressTracker 结构体,统一管理下载进度更新逻辑,避免重复代码,并确保进度回调的及时性和准确性(每1%或500ms更新一次)。
  • 多线程下载简化与增强: 重构多线程下载实现,利用 futures_util::stream::buffer_unordered 替代手动任务管理和写入通道,极大地简化了并发下载逻辑,并为每个分片下载增加了重试机制。
  • 文件大小探测增强: 改进了文件大小探测逻辑,在 HEAD 请求失败或未提供内容长度时,会尝试发送 Range: bytes=0-0 的 GET 请求来获取 Content-Range 头信息,从而更可靠地确定文件总大小。
  • 元数据写入优化: 优化了元数据写入函数,提取了获取或创建标签的逻辑到 get_or_create_tag 辅助函数中,提高了代码的清晰度和可维护性。
  • IPC回调兼容性: 更新了 Electron 主进程中的 onProgress 回调处理,使其能够直接接收 Rust 端发送的结构化 DownloadProgress 对象,同时保留对旧版 JSON 字符串格式的兼容性,并处理了 Rust 的 snake_case 到 JS 的 camelCase 映射。
Changelog
  • electron/main/ipc/ipc-file.ts
    • 移除了旧的 DownloadProgress 接口定义。
    • 更新了 onProgress 回调处理逻辑,以支持新的结构化进度数据,并兼容旧的 JSON 字符串格式。
    • 实现了 Rust snake_case 字段到 JS camelCase 字段的映射。
  • native/external-media-integration/src/discord.rs
    • 格式化导入语句,提高代码可读性。
  • native/external-media-integration/src/lib.rs
    • 格式化导入语句,提高代码可读性。
  • native/external-media-integration/src/logger.rs
    • 格式化导入语句,提高代码可读性。
  • native/external-media-integration/src/sys_media/linux.rs
    • 格式化导入语句,提高代码可读性。
  • native/external-media-integration/src/sys_media/macos.rs
    • 格式化导入语句,提高代码可读性。
  • native/external-media-integration/src/sys_media/mod.rs
    • 格式化导入语句,提高代码可读性。
  • native/external-media-integration/src/sys_media/windows.rs
    • 格式化导入语句,提高代码可读性。
  • native/ferrous-opencc-wasm/src/lib.rs
    • 格式化导入语句,提高代码可读性。
  • native/taskbar-lyric/src/lib.rs
    • 格式化导入语句,提高代码可读性。
  • native/taskbar-lyric/src/logger.rs
    • 格式化导入语句,提高代码可读性。
  • native/taskbar-lyric/src/strategy/win10.rs
    • 格式化导入语句,提高代码可读性。
  • native/taskbar-lyric/src/strategy/win11.rs
    • 格式化导入语句,提高代码可读性。
  • native/taskbar-lyric/src/tray_watcher.rs
    • 格式化导入语句,提高代码可读性。
  • native/taskbar-lyric/src/uia.rs
    • 格式化导入语句,提高代码可读性。
  • native/taskbar-lyric/src/uia_watcher.rs
    • 格式化导入语句,提高代码可读性。
  • native/taskbar-lyric/src/utils.rs
    • 格式化导入语句,提高代码可读性。
  • native/tools/index.d.ts
    • 更新了 downloadFile 函数的 onProgress 回调类型,现在直接传递 DownloadProgress 接口对象。
    • 新增了 DownloadProgress 接口的 TypeScript 定义。
  • native/tools/src/download.rs
    • 引入了 ProgressTracker 结构体,用于集中管理下载进度更新。
    • 重构了单线程和多线程下载的进度更新逻辑,统一使用 ProgressTracker。
    • 增强了文件总大小的探测逻辑,支持通过 HEAD 请求和 Range GET 请求获取。
    • 多线程下载现在使用 futures_util::stream::buffer_unordered 来并行处理分片下载,并为每个分片下载增加了重试机制。
    • 优化了 write_metadata 函数,通过 get_or_create_tag 辅助函数简化了标签获取逻辑。
    • 更新了下载过程中的日志和错误信息为中文。
  • native/tools/src/lib.rs
    • 格式化导入语句,提高代码可读性。
  • native/tools/src/scanner.rs
    • 格式化导入语句,提高代码可读性。
  • rustfmt.toml
    • 移除了该文件,可能意味着项目将采用默认的 Rust 格式化配置。
Activity
  • 此拉取请求由 kazukokawagawa 创建。
  • 目前没有其他评审活动或评论。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

这次的重构非常出色,极大地改善了下载模块的代码质量。ProgressTracker 的提取、使用 futures_util 简化多线程下载、以及对元数据写入逻辑的优化,都让代码变得更清晰、更健壮。这是一个高质量的 PR,我只有一个小小的改进建议。

Comment on lines +505 to +511
if tagged_file.primary_tag_mut().is_some() {
return Ok(tagged_file.primary_tag_mut().unwrap());
}

if tagged_file.first_tag_mut().is_some() {
return Ok(tagged_file.first_tag_mut().unwrap());
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

这个函数可以通过使用 if let 来变得更高效和符合 Rust 的惯用写法。当前实现中,primary_tag_mut() 和 first_tag_mut() 都被调用了两次(一次用于 is_some() 检查,一次用于 unwrap()),这有些冗余。使用 if let 可以将两次调用合并为一次,使代码更简洁。

Suggested change
if tagged_file.primary_tag_mut().is_some() {
return Ok(tagged_file.primary_tag_mut().unwrap());
}
if tagged_file.first_tag_mut().is_some() {
return Ok(tagged_file.first_tag_mut().unwrap());
}
if let Some(tag) = tagged_file.primary_tag_mut() {
return Ok(tag);
}
if let Some(tag) = tagged_file.first_tag_mut() {
return Ok(tag);
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

傻逼这玩意改了就炸了你知道吗

@kazukokawagawa kazukokawagawa self-assigned this Feb 4, 2026
- 新增下载设置选项:启用HTTPS升级和HTTP/2下载
- 升级reqwest到0.13.1并使用rustls-native-certs特性
- 自动将HTTP链接升级为HTTPS以支持HTTP/2协议
- 在下载配置中新增enableDownloadHttps和enableDownloadHttp2参数
- 更新所有相关调用链以传递新的下载参数
- 将 Rust 原生下载器从函数式 API 重构为面向对象的 DownloadTask 类
- 移除独立的 enableDownloadHttps 设置,HTTP/2 启用时自动升级 HTTP 到 HTTPS
- 改进下载进度报告机制,优化性能并减少不必要的回调
- 统一下载任务管理,使用 Map 跟踪活动下载以便取消操作
- 清理相关类型定义和配置界面中已移除的选项
@kazukokawagawa
kazukokawagawa marked this pull request as ready for review February 4, 2026 10:18
@kazukokawagawa
kazukokawagawa merged commit 2a6d186 into dev Feb 4, 2026
4 checks passed
@kazukokawagawa
kazukokawagawa deleted the dev-font branch February 4, 2026 10:25
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants