Skip to content

Commit

Permalink
perf: optimize webdav
Browse files Browse the repository at this point in the history
  • Loading branch information
Pylogmon committed Aug 31, 2023
1 parent 902466f commit 29b1940
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 168 deletions.
88 changes: 88 additions & 0 deletions src-tauri/src/backup.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
body: Option<String>,
) -> Result<String, 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 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));
}
}
}
9 changes: 3 additions & 6 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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::*;
Expand All @@ -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;

Expand Down Expand Up @@ -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!())
Expand Down
158 changes: 0 additions & 158 deletions src-tauri/src/webdav.rs

This file was deleted.

12 changes: 8 additions & 4 deletions src/window/Config/pages/Backup/utils/webdav.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 29b1940

Please sign in to comment.