74 lines
2.3 KiB
Rust
74 lines
2.3 KiB
Rust
use crate::util::{check_file_or_append, get_download_message, show_toast, MessageType};
|
|
use std::fs::{self, File};
|
|
use std::io::Write;
|
|
use std::str::FromStr;
|
|
use tauri::http::Method;
|
|
use tauri::{command, AppHandle, Manager, Url, WebviewWindow};
|
|
use tauri_plugin_http::reqwest::{ClientBuilder, Request};
|
|
|
|
#[derive(serde::Deserialize)]
|
|
pub struct DownloadFileParams {
|
|
url: String,
|
|
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: WebviewWindow = app.get_webview_window("pake").unwrap();
|
|
show_toast(&window, &get_download_message(MessageType::Start));
|
|
|
|
let output_path = app.path().download_dir().unwrap().join(params.filename);
|
|
let file_path = check_file_or_append(output_path.to_str().unwrap());
|
|
let client = ClientBuilder::new().build().unwrap();
|
|
|
|
let response = client
|
|
.execute(Request::new(
|
|
Method::GET,
|
|
Url::from_str(¶ms.url).unwrap(),
|
|
))
|
|
.await;
|
|
|
|
match response {
|
|
Ok(res) => {
|
|
let bytes = res.bytes().await.unwrap();
|
|
|
|
let mut file = File::create(file_path).unwrap();
|
|
file.write_all(&bytes).unwrap();
|
|
show_toast(&window, &get_download_message(MessageType::Success));
|
|
Ok(())
|
|
}
|
|
Err(e) => {
|
|
show_toast(&window, &get_download_message(MessageType::Failure));
|
|
Err(e.to_string())
|
|
}
|
|
}
|
|
}
|
|
|
|
#[command]
|
|
pub async fn download_file_by_binary(
|
|
app: AppHandle,
|
|
params: BinaryDownloadParams,
|
|
) -> Result<(), String> {
|
|
let window: WebviewWindow = app.get_webview_window("pake").unwrap();
|
|
show_toast(&window, &get_download_message(MessageType::Start));
|
|
let output_path = app.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, ¶ms.binary);
|
|
match download_file_result {
|
|
Ok(_) => {
|
|
show_toast(&window, &get_download_message(MessageType::Success));
|
|
Ok(())
|
|
}
|
|
Err(e) => {
|
|
show_toast(&window, &get_download_message(MessageType::Failure));
|
|
Err(e.to_string())
|
|
}
|
|
}
|
|
}
|