✨ Support image download and right-click menu
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use crate::util::{check_file_or_append, get_download_message, show_toast, MessageType};
|
||||
use crate::util::{check_file_or_append, get_download_message_with_lang, show_toast, MessageType};
|
||||
use std::fs::{self, File};
|
||||
use std::io::Write;
|
||||
use std::str::FromStr;
|
||||
@@ -10,12 +10,14 @@ use tauri_plugin_http::reqwest::{ClientBuilder, Request};
|
||||
pub struct DownloadFileParams {
|
||||
url: String,
|
||||
filename: String,
|
||||
language: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct BinaryDownloadParams {
|
||||
filename: String,
|
||||
binary: Vec<u8>,
|
||||
language: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
@@ -28,7 +30,7 @@ pub struct NotificationParams {
|
||||
#[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));
|
||||
show_toast(&window, &get_download_message_with_lang(MessageType::Start, params.language.clone()));
|
||||
|
||||
let output_path = app.path().download_dir().unwrap().join(params.filename);
|
||||
let file_path = check_file_or_append(output_path.to_str().unwrap());
|
||||
@@ -47,11 +49,11 @@ pub async fn download_file(app: AppHandle, params: DownloadFileParams) -> Result
|
||||
|
||||
let mut file = File::create(file_path).unwrap();
|
||||
file.write_all(&bytes).unwrap();
|
||||
show_toast(&window, &get_download_message(MessageType::Success));
|
||||
show_toast(&window, &get_download_message_with_lang(MessageType::Success, params.language.clone()));
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
show_toast(&window, &get_download_message(MessageType::Failure));
|
||||
show_toast(&window, &get_download_message_with_lang(MessageType::Failure, params.language));
|
||||
Err(e.to_string())
|
||||
}
|
||||
}
|
||||
@@ -63,17 +65,17 @@ pub async fn download_file_by_binary(
|
||||
params: BinaryDownloadParams,
|
||||
) -> Result<(), String> {
|
||||
let window: WebviewWindow = app.get_webview_window("pake").unwrap();
|
||||
show_toast(&window, &get_download_message(MessageType::Start));
|
||||
show_toast(&window, &get_download_message_with_lang(MessageType::Start, params.language.clone()));
|
||||
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));
|
||||
show_toast(&window, &get_download_message_with_lang(MessageType::Success, params.language.clone()));
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
show_toast(&window, &get_download_message(MessageType::Failure));
|
||||
show_toast(&window, &get_download_message_with_lang(MessageType::Failure, params.language));
|
||||
Err(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user