From 29b1940bedf04c2f67fa5fd11100326f579fb415 Mon Sep 17 00:00:00 2001 From: Pylogmon Date: Thu, 31 Aug 2023 10:02:34 +0800 Subject: [PATCH] perf: optimize webdav --- src-tauri/src/backup.rs | 88 ++++++++++ src-tauri/src/main.rs | 9 +- src-tauri/src/webdav.rs | 158 ------------------ .../Config/pages/Backup/utils/webdav.jsx | 12 +- 4 files changed, 99 insertions(+), 168 deletions(-) create mode 100644 src-tauri/src/backup.rs delete mode 100644 src-tauri/src/webdav.rs diff --git a/src-tauri/src/backup.rs b/src-tauri/src/backup.rs new file mode 100644 index 0000000000..d4b5cde5ad --- /dev/null +++ b/src-tauri/src/backup.rs @@ -0,0 +1,88 @@ +use log::error; +use reqwest_dav::{Auth, ClientBuilder, Depth}; + +#[tauri::command(async)] +pub async fn webdav( + operate: &str, + url: String, + username: String, + password: String, + name: Option, + body: Option, +) -> Result { + // build a client + let client = match ClientBuilder::new() + .set_host(url) + .set_auth(Auth::Basic(username, password)) + .build() + { + Ok(v) => v, + Err(e) => { + error!("Build WebDav Client Error: {}", e); + return Err(format!("Build WebDav Client Error: {}", e)); + } + }; + match client.mkcol("/pot-app").await { + Ok(()) => {} + Err(e) => { + error!("WebDav Mkcol Error: {}", e); + return Err(format!("WebDav Mkcol Error: {}", e)); + } + }; + match operate { + "list" => { + let res = match client.list("pot-app", Depth::Number(1)).await { + Ok(v) => v, + Err(e) => { + error!("WebDav List Error: {}", e); + return Err(format!("WebDav List Error: {}", e)); + } + }; + match serde_json::to_string(&res) { + Ok(v) => Ok(v), + Err(e) => { + error!("WebDav List Json Error: {}", e); + Err(format!("WebDav List Json Error: {}", e)) + } + } + } + "get" => { + let res = match client.get(&format!("pot-app/{}", name.unwrap())).await { + Ok(v) => v, + Err(e) => { + error!("WebDav Get Error: {}", e); + return Err(format!("WebDav Get Error: {}", e)); + } + }; + match res.text().await { + Ok(v) => Ok(v), + Err(e) => { + error!("WebDav Get Text Error: {}", e); + Err(format!("WebDav Get Text Error: {}", e)) + } + } + } + "put" => match client + .put(&format!("pot-app/{}", name.unwrap()), body.unwrap()) + .await + { + Ok(()) => return Ok("".to_string()), + Err(e) => { + error!("WebDav Put Error: {}", e); + return Err(format!("WebDav Put Error: {}", e)); + } + }, + + "delete" => match client.delete(&format!("pot-app/{}", name.unwrap())).await { + Ok(()) => return Ok("".to_string()), + Err(e) => { + error!("WebDav Delete Error: {}", e); + return Err(format!("WebDav Delete Error: {}", e)); + } + }, + _ => { + error!("WebDav Operate Error: {}", operate); + return Err(format!("WebDav Operate Error: {}", operate)); + } + } +} diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index d1c18ee537..0ede5bb1c5 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -1,6 +1,7 @@ // Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] +mod backup; mod cmd; mod config; mod hotkey; @@ -10,9 +11,9 @@ mod server; mod system_ocr; mod tray; mod updater; -mod webdav; mod window; +use backup::*; use cmd::*; use config::*; use hotkey::*; @@ -28,7 +29,6 @@ use tauri::Manager; use tauri_plugin_log::LogTarget; use tray::*; use updater::check_update; -use webdav::*; use window::config_window; use window::updater_window; @@ -116,10 +116,7 @@ fn main() { updater_window, screenshot, lang_detect, - backup_list, - get_backup, - put_backup, - delete_backup + webdav ]) .on_system_tray_event(tray_event_handler) .build(tauri::generate_context!()) diff --git a/src-tauri/src/webdav.rs b/src-tauri/src/webdav.rs deleted file mode 100644 index 1e9c0f1451..0000000000 --- a/src-tauri/src/webdav.rs +++ /dev/null @@ -1,158 +0,0 @@ -use log::error; -use reqwest_dav::{Auth, ClientBuilder, Depth}; - -#[tauri::command(async)] -pub async fn backup_list( - url: String, - username: String, - password: String, -) -> Result { - // build a client - let client = match ClientBuilder::new() - .set_host(url) - .set_auth(Auth::Basic(username, password)) - .build() - { - Ok(v) => v, - Err(e) => { - error!("Build WebDav Client Error: {}", e); - return Err(format!("Build WebDav Client Error: {}", e)); - } - }; - match client.mkcol("/pot-app").await { - Ok(()) => {} - Err(e) => { - error!("WebDav Mkcol Error: {}", e); - return Err(format!("WebDav Mkcol Error: {}", e)); - } - }; - let res = match client.list("pot-app", Depth::Number(1)).await { - Ok(v) => v, - Err(e) => { - error!("WebDav List Error: {}", e); - return Err(format!("WebDav List Error: {}", e)); - } - }; - match serde_json::to_string(&res) { - Ok(v) => Ok(v), - Err(e) => { - error!("WebDav List Json Error: {}", e); - Err(format!("WebDav List Json Error: {}", e)) - } - } -} - -#[tauri::command(async)] -pub async fn get_backup( - url: String, - username: String, - password: String, - name: String, -) -> Result { - // build a client - let client = match ClientBuilder::new() - .set_host(url) - .set_auth(Auth::Basic(username, password)) - .build() - { - Ok(v) => v, - Err(e) => { - error!("Build WebDav Client Error: {}", e); - return Err(format!("Build WebDav Client Error: {}", e)); - } - }; - match client.mkcol("/pot-app").await { - Ok(()) => {} - Err(e) => { - error!("WebDav Mkcol Error: {}", e); - return Err(format!("WebDav Mkcol Error: {}", e)); - } - }; - let res = match client.get(&format!("pot-app/{name}")).await { - Ok(v) => v, - Err(e) => { - error!("WebDav Get Error: {}", e); - return Err(format!("WebDav Get Error: {}", e)); - } - }; - match res.text().await { - Ok(v) => Ok(v), - Err(e) => { - error!("WebDav Get Text Error: {}", e); - Err(format!("WebDav Get Text Error: {}", e)) - } - } -} - -#[tauri::command(async)] -pub async fn put_backup( - url: String, - username: String, - password: String, - name: String, - body: String, -) -> Result<(), String> { - // build a client - let client = match ClientBuilder::new() - .set_host(url) - .set_auth(Auth::Basic(username, password)) - .build() - { - Ok(v) => v, - Err(e) => { - error!("Build WebDav Client Error: {}", e); - return Err(format!("Build WebDav Client Error: {}", e)); - } - }; - match client.mkcol("/pot-app").await { - Ok(()) => {} - Err(e) => { - error!("WebDav Mkcol Error: {}", e); - return Err(format!("WebDav Mkcol Error: {}", e)); - } - }; - - match client.put(&format!("pot-app/{name}"), body).await { - Ok(()) => return Ok(()), - Err(e) => { - error!("WebDav Put Error: {}", e); - return Err(format!("WebDav Put Error: {}", e)); - } - } -} - -#[tauri::command(async)] -pub async fn delete_backup( - url: String, - username: String, - password: String, - name: String, -) -> Result<(), String> { - // build a client - let client = match ClientBuilder::new() - .set_host(url) - .set_auth(Auth::Basic(username, password)) - .build() - { - Ok(v) => v, - Err(e) => { - error!("Build WebDav Client Error: {}", e); - return Err(format!("Build WebDav Client Error: {}", e)); - } - }; - match client.mkcol("/pot-app").await { - Ok(()) => {} - Err(e) => { - error!("WebDav Mkcol Error: {}", e); - return Err(format!("WebDav Mkcol Error: {}", e)); - } - }; - - match client.delete(&format!("pot-app/{name}")).await { - Ok(()) => return Ok(()), - Err(e) => { - error!("WebDav Delete Error: {}", e); - return Err(format!("WebDav Delete Error: {}", e)); - } - } -} diff --git a/src/window/Config/pages/Backup/utils/webdav.jsx b/src/window/Config/pages/Backup/utils/webdav.jsx index 07f6bf4637..f5eea064ce 100644 --- a/src/window/Config/pages/Backup/utils/webdav.jsx +++ b/src/window/Config/pages/Backup/utils/webdav.jsx @@ -3,7 +3,8 @@ import { store } from '../../../../../utils/store'; import { invoke } from '@tauri-apps/api'; export async function backup(url, username, password, name, body) { - return await invoke('put_backup', { + return await invoke('webdav', { + operate: 'put', url, username, password, @@ -13,7 +14,8 @@ export async function backup(url, username, password, name, body) { } export async function list(url, username, password) { - const backup_list_text = await invoke('backup_list', { + const backup_list_text = await invoke('webdav', { + operate: 'list', url, username, password, @@ -28,7 +30,8 @@ export async function list(url, username, password) { } export async function get(url, username, password, name) { - const body = await invoke('get_backup', { + const body = await invoke('webdav', { + operate: 'get', url, username, password, @@ -39,7 +42,8 @@ export async function get(url, username, password, name) { } export async function remove(url, username, password, name) { - return await invoke('delete_backup', { + return await invoke('webdav', { + operate: 'delete', url, username, password,