Significantly improve the download experience.

This commit is contained in:
Tw93
2023-07-20 15:48:46 +08:00
parent dbd7f9a106
commit f1e05e6b02
7 changed files with 478 additions and 840 deletions

View File

@@ -1,6 +1,8 @@
use crate::util::{check_file_or_append, get_download_message, show_toast};
use download_rs::sync_download::Download;
use crate::util::{check_file_or_append, get_download_message, MessageType, show_toast};
use tauri::{api, command, AppHandle, Manager, Window};
use tauri::api::http::{ClientBuilder, HttpRequestBuilder, ResponseType};
use std::fs::File;
use std::io::Write;
#[derive(serde::Deserialize)]
pub struct DownloadFileParams {
@@ -11,16 +13,29 @@ pub struct DownloadFileParams {
#[command]
pub async fn download_file(app: AppHandle, params: DownloadFileParams) -> Result<(), String> {
let window: Window = app.get_window("pake").unwrap();
show_toast(&window, &get_download_message(MessageType::Start));
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 = Download::new(&params.url, Some(&file_path), None);
match download.download() {
Ok(_) => {
show_toast(&window, &get_download_message());
let client = ClientBuilder::new().build().unwrap();
let response = client.send(
HttpRequestBuilder::new("GET", &params.url)
.unwrap()
.response_type(ResponseType::Binary)
).await;
match response {
Ok(res) => {
let bytes = res.bytes().await.unwrap().data;
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, &e.to_string());
show_toast(&window, &get_download_message(MessageType::Failure));
Err(e.to_string())
}
}

View File

@@ -38,7 +38,6 @@ pub fn get_menu() -> Menu {
pub fn menu_event_handle(event: WindowMenuEvent) {
if event.menu_item_id() == "close" {
event.window().minimize().expect("can't minimize window");
// event.window().eval("toggleVideoPlayback(true);").unwrap();
}
if event.menu_item_id() == "goto_url" {