refactor: 升级 tauri 到 2.x

This commit is contained in:
jeasonnow
2024-07-30 10:53:06 +08:00
parent 1d0628ed19
commit 8c07aaae3c
17 changed files with 6475 additions and 1673 deletions

View File

@@ -1,8 +1,11 @@
use crate::util::{check_file_or_append, get_download_message, show_toast, MessageType};
use std::fs::{self, File};
use std::io::Write;
use tauri::api::http::{ClientBuilder, HttpRequestBuilder, ResponseType};
use tauri::{api, command, AppHandle, Manager, Window};
use std::str::FromStr;
use tauri::http::Method;
use tauri_plugin_http::reqwest::{ClientBuilder, Request};
// use tauri::http::{ClientBuilder, HttpRequestBuilder, ResponseType};
use tauri::{command, AppHandle, Manager, Url, WebviewWindow};
#[derive(serde::Deserialize)]
pub struct DownloadFileParams {
@@ -18,24 +21,22 @@ pub struct BinaryDownloadParams {
#[command]
pub async fn download_file(app: AppHandle, params: DownloadFileParams) -> Result<(), String> {
let window: Window = app.get_window("pake").unwrap();
let window: WebviewWindow = app.get_webview_window("pake").unwrap();
show_toast(&window, &get_download_message(MessageType::Start));
let output_path = api::path::download_dir().unwrap().join(params.filename);
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
.send(
HttpRequestBuilder::new("GET", &params.url)
.unwrap()
.response_type(ResponseType::Binary),
.execute(
Request::new(Method::GET, Url::from_str(&params.url).unwrap())
)
.await;
match response {
Ok(res) => {
let bytes = res.bytes().await.unwrap().data;
let bytes = res.bytes().await.unwrap();
let mut file = File::create(file_path).unwrap();
file.write_all(&bytes).unwrap();
@@ -54,9 +55,9 @@ pub async fn download_file_by_binary(
app: AppHandle,
params: BinaryDownloadParams,
) -> Result<(), String> {
let window: Window = app.get_window("pake").unwrap();
let window: WebviewWindow = app.get_webview_window("pake").unwrap();
show_toast(&window, &get_download_message(MessageType::Start));
let output_path = api::path::download_dir().unwrap().join(params.filename);
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, &params.binary);
match download_file_result {