优化调整
This commit is contained in:
@@ -761,7 +761,7 @@ impl DownloadEngine {
|
||||
last_time = now;
|
||||
|
||||
// 更新任务状态 + 发送进度事件
|
||||
let total_size = {
|
||||
let (total_size, live_status) = {
|
||||
let mut tasks = engine.inner.tasks.lock().unwrap_or_else(|e| e.into_inner());
|
||||
if let Some(task) = tasks.get_mut(&id_monitor) {
|
||||
task.completed_size = completed;
|
||||
@@ -771,7 +771,10 @@ impl DownloadEngine {
|
||||
seg.completed = prog.load(Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
task.total_size
|
||||
// 读取任务实时状态而非硬编码 Active:
|
||||
// 暂停后监控循环 break 前可能发出的最后一次事件
|
||||
// 必须携带 Paused,否则前端会把任务状态覆盖回"下载中"
|
||||
(task.total_size, task.status.clone())
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
@@ -784,7 +787,7 @@ impl DownloadEngine {
|
||||
completed_size: completed,
|
||||
total_size,
|
||||
speed,
|
||||
status: TaskStatus::Active,
|
||||
status: live_status,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ use axum::{
|
||||
Router,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tauri::{AppHandle, Emitter, Manager};
|
||||
|
||||
use super::engine::DownloadEngine;
|
||||
use super::task::DownloadTask;
|
||||
@@ -45,14 +46,18 @@ struct ErrorResponse {
|
||||
|
||||
impl ExtensionServer {
|
||||
/// 启动 HTTP API 服务器(绑定到 127.0.0.1:port)
|
||||
pub async fn start(engine: DownloadEngine, port: u16, secret: String) {
|
||||
pub async fn start(engine: DownloadEngine, port: u16, secret: String, app_handle: AppHandle) {
|
||||
let addr: SocketAddr = format!("127.0.0.1:{}", port).parse().expect("无效端口");
|
||||
|
||||
let app = Router::new()
|
||||
.route("/health", get(health))
|
||||
.route("/api/downloads", post(create_download).get(list_downloads))
|
||||
.route("/api/downloads/:id", axum::routing::delete(remove_download))
|
||||
.with_state(AppState { engine, secret });
|
||||
.with_state(AppState {
|
||||
engine,
|
||||
secret,
|
||||
app_handle,
|
||||
});
|
||||
|
||||
let listener = match tokio::net::TcpListener::bind(&addr).await {
|
||||
Ok(l) => l,
|
||||
@@ -74,6 +79,7 @@ impl ExtensionServer {
|
||||
struct AppState {
|
||||
engine: DownloadEngine,
|
||||
secret: String,
|
||||
app_handle: AppHandle,
|
||||
}
|
||||
|
||||
/// 鉴权检查:如果配置了 secret,校验 Bearer token
|
||||
@@ -109,7 +115,14 @@ async fn create_download(
|
||||
}
|
||||
|
||||
match state.engine.add_task(req.url, req.filename, req.dir, req.headers, true).await {
|
||||
Ok(id) => Ok(Json(CreateDownloadResponse { id })),
|
||||
Ok(id) => {
|
||||
// 浏览器扩展发起下载:置前主窗口并通知前端跳到下载画面(替代原桌面通知)
|
||||
crate::tray_menu::focus_main_window(&state.app_handle);
|
||||
let _ = state
|
||||
.app_handle
|
||||
.emit(crate::constants::events::DOWNLOAD_EXTENSION_ADDED, ());
|
||||
Ok(Json(CreateDownloadResponse { id }))
|
||||
}
|
||||
Err(e) => Err((StatusCode::BAD_REQUEST, Json(ErrorResponse { error: e }))),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user