下载非内核
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use tauri::{AppHandle, State};
|
||||
use tauri_plugin_opener::OpenerExt;
|
||||
|
||||
use super::engine::DownloadEngine;
|
||||
use super::task::{DownloadTask, DownloaderSettings};
|
||||
|
||||
/// 获取所有任务
|
||||
#[tauri::command]
|
||||
pub fn downloader_get_tasks(engine: State<'_, DownloadEngine>) -> Vec<DownloadTask> {
|
||||
engine.get_tasks()
|
||||
}
|
||||
|
||||
/// 添加下载任务
|
||||
#[tauri::command]
|
||||
pub async fn downloader_add_task(
|
||||
engine: State<'_, DownloadEngine>,
|
||||
url: String,
|
||||
filename: Option<String>,
|
||||
dir: Option<String>,
|
||||
headers: Option<HashMap<String, String>>,
|
||||
) -> Result<String, String> {
|
||||
engine.add_task(url, filename, dir, headers.unwrap_or_default()).await
|
||||
}
|
||||
|
||||
/// 暂停任务
|
||||
#[tauri::command]
|
||||
pub fn downloader_pause_task(engine: State<'_, DownloadEngine>, id: String) -> Result<(), String> {
|
||||
engine.pause_task(&id)
|
||||
}
|
||||
|
||||
/// 恢复任务
|
||||
#[tauri::command]
|
||||
pub fn downloader_resume_task(engine: State<'_, DownloadEngine>, id: String) -> Result<(), String> {
|
||||
engine.resume_task(&id)
|
||||
}
|
||||
|
||||
/// 移除任务
|
||||
#[tauri::command]
|
||||
pub fn downloader_remove_task(
|
||||
engine: State<'_, DownloadEngine>,
|
||||
id: String,
|
||||
delete_files: Option<bool>,
|
||||
) -> Result<(), String> {
|
||||
engine.remove_task(&id, delete_files.unwrap_or(false))
|
||||
}
|
||||
|
||||
/// 获取设置
|
||||
#[tauri::command]
|
||||
pub fn downloader_get_settings(engine: State<'_, DownloadEngine>) -> DownloaderSettings {
|
||||
engine.get_settings()
|
||||
}
|
||||
|
||||
/// 保存设置
|
||||
#[tauri::command]
|
||||
pub fn downloader_save_settings(
|
||||
engine: State<'_, DownloadEngine>,
|
||||
settings: DownloaderSettings,
|
||||
) -> Result<(), String> {
|
||||
engine.save_settings(settings);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 引擎状态(始终运行中)
|
||||
#[tauri::command]
|
||||
pub fn downloader_status(engine: State<'_, DownloadEngine>) -> serde_json::Value {
|
||||
serde_json::json!({
|
||||
"running": engine.is_started(),
|
||||
})
|
||||
}
|
||||
|
||||
/// 获取扩展服务信息
|
||||
#[tauri::command]
|
||||
pub fn downloader_get_extension_info(engine: State<'_, DownloadEngine>) -> serde_json::Value {
|
||||
let settings = engine.get_settings();
|
||||
serde_json::json!({
|
||||
"url": format!("http://127.0.0.1:{}/", settings.extension_port),
|
||||
"port": settings.extension_port,
|
||||
"secret": settings.extension_secret,
|
||||
"hasSecret": !settings.extension_secret.is_empty(),
|
||||
})
|
||||
}
|
||||
|
||||
/// 用系统资源管理器打开目录
|
||||
#[tauri::command]
|
||||
pub fn downloader_open_dir(app: AppHandle, path: String) -> Result<(), String> {
|
||||
app.opener()
|
||||
.open_path(path, None::<&str>)
|
||||
.map_err(|e| format!("打开目录失败: {}", e))
|
||||
}
|
||||
|
||||
/// 用系统默认浏览器打开 URL
|
||||
#[tauri::command]
|
||||
pub fn downloader_open_url(app: AppHandle, url: String) -> Result<(), String> {
|
||||
app.opener()
|
||||
.open_url(url, None::<&str>)
|
||||
.map_err(|e| format!("打开链接失败: {}", e))
|
||||
}
|
||||
Reference in New Issue
Block a user