下载/主界面优化
This commit is contained in:
@@ -158,9 +158,10 @@ impl DownloadEngine {
|
||||
|
||||
let id = self.inner.storage.next_task_id();
|
||||
|
||||
// 创建分段
|
||||
// 创建分段(受 continue_download 设置控制:关闭时强制单线程、不支持续传)
|
||||
let resume_enabled = settings.continue_download;
|
||||
let segments = match &probe {
|
||||
Ok(p) if p.supports_resume && p.total_size.map(|s| s > 0).unwrap_or(false) => {
|
||||
Ok(p) if resume_enabled && p.supports_resume && p.total_size.map(|s| s > 0).unwrap_or(false) => {
|
||||
split_segments(p.total_size.unwrap(), settings.max_connections)
|
||||
}
|
||||
_ => vec![Segment {
|
||||
@@ -172,7 +173,8 @@ impl DownloadEngine {
|
||||
};
|
||||
|
||||
let total_size = probe.as_ref().ok().and_then(|p| p.total_size).unwrap_or(0);
|
||||
let supports_resume = probe.as_ref().ok().map(|p| p.supports_resume).unwrap_or(false);
|
||||
let supports_resume = resume_enabled
|
||||
&& probe.as_ref().ok().map(|p| p.supports_resume).unwrap_or(false);
|
||||
|
||||
let task = DownloadTask {
|
||||
id: id.clone(),
|
||||
|
||||
@@ -318,13 +318,14 @@ async fn download_segment_with_client(
|
||||
match stream.next().await {
|
||||
Some(Ok(chunk)) => {
|
||||
buf.extend_from_slice(&chunk);
|
||||
// 接收到数据立即更新进度(避免监控周期内进度无变化导致速度显示为 0)
|
||||
local_completed += chunk.len() as u64;
|
||||
progress.store(local_completed, Ordering::Relaxed);
|
||||
// 积累到 64KB 再写入(减少 I/O 次数)
|
||||
if buf.len() >= 64 * 1024 {
|
||||
file.write_all(&buf)
|
||||
.await
|
||||
.map_err(|e| format!("写入文件失败: {}", e))?;
|
||||
local_completed += buf.len() as u64;
|
||||
progress.store(local_completed, Ordering::Relaxed);
|
||||
// 限速
|
||||
limiter.consume(buf.len() as u64).await;
|
||||
buf.clear();
|
||||
@@ -339,8 +340,7 @@ async fn download_segment_with_client(
|
||||
file.write_all(&buf)
|
||||
.await
|
||||
.map_err(|e| format!("写入文件失败: {}", e))?;
|
||||
local_completed += buf.len() as u64;
|
||||
progress.store(local_completed, Ordering::Relaxed);
|
||||
// local_completed 和 progress 已在接收 chunk 时更新
|
||||
limiter.consume(buf.len() as u64).await;
|
||||
buf.clear();
|
||||
}
|
||||
|
||||
@@ -38,6 +38,10 @@ pub struct Storage {
|
||||
|
||||
impl Storage {
|
||||
pub fn new(data_dir: PathBuf) -> Self {
|
||||
// 确保数据目录存在(首次启动或目录被删除时自动创建)
|
||||
if let Err(e) = fs::create_dir_all(&data_dir) {
|
||||
eprintln!("[download_engine] 创建数据目录失败: {} ({})", data_dir.display(), e);
|
||||
}
|
||||
let state_path = data_dir.join("engine_state.json");
|
||||
let existing = Self::load_raw(&state_path);
|
||||
let next_id = existing.as_ref().map(|s| s.next_id).unwrap_or(1);
|
||||
@@ -70,6 +74,10 @@ impl Storage {
|
||||
state.next_id = self.id_counter.load(Ordering::SeqCst);
|
||||
match serde_json::to_string_pretty(&state) {
|
||||
Ok(json) => {
|
||||
// 兜底:若父目录被外部删除则在写入前重建
|
||||
if let Some(parent) = self.state_path.parent() {
|
||||
let _ = fs::create_dir_all(parent);
|
||||
}
|
||||
if let Err(e) = fs::write(&self.state_path, json) {
|
||||
eprintln!("[download_engine] 保存状态失败: {}", e);
|
||||
}
|
||||
|
||||
@@ -54,6 +54,11 @@ pub fn run() {
|
||||
.plugin(tauri_plugin_autostart::Builder::new().build())
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.plugin(
|
||||
tauri_plugin_snap_layout::init()
|
||||
.button_id("titlebar-maximize")
|
||||
.build()
|
||||
)
|
||||
.manage(ProcessManager::new())
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
greet,
|
||||
|
||||
Reference in New Issue
Block a user