This commit is contained in:
Bianca Fürstenau 2025-02-09 18:57:46 +01:00
parent 9609a80f7f
commit 7c4ca73f68
7 changed files with 151 additions and 12 deletions

View file

@ -1,8 +1,18 @@
use rusqlite::{Connection, ToSql, types::ToSqlOutput};
use rand::prelude::*;
use tokio::sync::Mutex;
use tauri::{Manager, State};
use chrono::offset::Utc;
use curl::{easy, easy::Easy2};
use rand::prelude::*;
use rusqlite::{types::ToSqlOutput, Connection, ToSql};
use tauri::{Manager, State};
use tokio::sync::Mutex;
struct Collector(Vec<u8>);
impl easy::Handler for Collector {
fn write(&mut self, data: &[u8]) -> Result<usize, easy::WriteError> {
self.0.extend_from_slice(data);
Ok(data.len())
}
}
struct AppState {
db: Connection,
@ -48,9 +58,31 @@ impl ToSql for Store {
}
}
#[tauri::command]
async fn pull_data() -> Result<String, ()> {
let mut client = Easy2::new(Collector(Vec::new()));
client.custom_request("PROPFIND").map_err(|_| ())?;
client.url("https://cloud.seebruecke.org/public.php/webdav/data/")
.map_err(|_| ())?;
client.username(include_str!("cloud_user.txt"));
client.http_auth(easy::Auth::new().auto(true));
client.perform().map_err(|_| ())?;
let content = client.get_ref();
Ok(String::from_utf8_lossy(&content.0).to_string())
}
#[tauri::command]
async fn push_data() -> Result<(), ()> {
let mut client = Easy2::new(Collector(Vec::new()));
Ok(())
}
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
#[tauri::command]
async fn swap(store: &str, state: State<'_, Mutex<AppState>>) -> Result<(), ()> {
async fn swap(
store: &str,
state: State<'_, Mutex<AppState>>,
) -> Result<(), ()> {
let state = state.lock().await;
let mut rng = rand::rng();
let store: Store = store.try_into()?;
@ -63,7 +95,8 @@ async fn swap(store: &str, state: State<'_, Mutex<AppState>>) -> Result<(), ()>
Utc::now().timestamp(),
false,
),
).map_err(|_| ())?;
)
.map_err(|_| ())?;
Ok(())
}
@ -91,12 +124,19 @@ pub fn run() {
cancellation BOOL
)",
(),
).unwrap();
)
.unwrap();
let state = AppState { db, acc };
tauri::Builder::default()
.setup(|app| {app.manage(Mutex::new(state)); Ok(())})
.plugin(tauri_plugin_fs::init())
.setup(|app| {
app.manage(Mutex::new(state));
Ok(())
})
.plugin(tauri_plugin_opener::init())
.invoke_handler(tauri::generate_handler![swap, count])
.invoke_handler(tauri::generate_handler![
swap, count, pull_data
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}