feat: 调整下载全部通过 rs 能力实现

This commit is contained in:
jeasonnow
2023-06-25 15:24:10 +08:00
parent 3c97825b54
commit f70920dbbe
4 changed files with 40 additions and 13 deletions

View File

@@ -1,3 +1,5 @@
use std::{fs};
use crate::util::{check_file_or_append, get_download_message, show_toast};
use download_rs::sync_download::Download;
use tauri::{api, command, AppHandle, Manager, Window};
@@ -8,6 +10,12 @@ pub struct DownloadFileParams {
filename: String,
}
#[derive(serde::Deserialize)]
pub struct BinaryDownloadParams {
filename: String,
binary: Vec<u8>,
}
#[command]
pub async fn download_file(app: AppHandle, params: DownloadFileParams) -> Result<(), String> {
let window: Window = app.get_window("pake").unwrap();
@@ -25,3 +33,22 @@ pub async fn download_file(app: AppHandle, params: DownloadFileParams) -> Result
}
}
}
#[command]
pub async fn download_file_by_binary(app: AppHandle, params: BinaryDownloadParams) -> Result<(), String> {
let window: Window = app.get_window("pake").unwrap();
let output_path = api::path::download_dir().unwrap().join(params.filename);
let file_path = check_file_or_append(output_path.to_str().unwrap());
let download_file_result = fs::write(&file_path, &params.binary);
match download_file_result {
Ok(_) => {
show_toast(&window, &get_download_message());
Ok(())
},
Err(e) => {
show_toast(&window, &e.to_string());
Err(e.to_string())
}
}
}