This commit is contained in:
Bianca Fürstenau 2025-02-11 14:57:40 +01:00
parent bd40e5c8d8
commit 51828ef351
3 changed files with 48 additions and 27 deletions

View file

@ -1,7 +1,7 @@
use rand::prelude::*;
use tokio::sync::Mutex;
use rusqlite::Connection;
use ring_compat::signature::ed25519::SigningKey;
use rusqlite::Connection;
use tokio::sync::Mutex;
pub struct AppState {
pub db: Connection,
@ -30,6 +30,11 @@ impl AppState {
let last_sync = i64::MIN;
let id = rng.gen();
let key = SigningKey::generate(&mut rng);
AppState { db, last_sync, id, key }
AppState {
db,
last_sync,
id,
key,
}
}
}

View file

@ -19,55 +19,60 @@ impl easy::Handler for Collector {
fn read(&mut self, data: &mut [u8]) -> Result<usize, easy::ReadError> {
let p = self.2;
let src: &[u8] = self.1.as_ref();
let n = usize::min(src.len()-p, data.len());
data[..n].copy_from_slice(&src[p..(p+n)]);
self.2 = n+p;
let n = usize::min(src.len() - p, data.len());
data[..n].copy_from_slice(&src[p..(p + n)]);
self.2 = n + p;
Ok(n)
}
fn seek(&mut self, whence: std::io::SeekFrom) -> easy::SeekResult {
use std::io::SeekFrom::{Start, End, Current};
use std::io::SeekFrom::{Current, End, Start};
match whence {
Start(p) => {
self.2 = p as usize;
easy::SeekResult::Ok
},
}
End(d) => {
let p = self.1.len() as i64;
if p+d < 0 {
if p + d < 0 {
easy::SeekResult::Fail
} else {
self.2 = (p+d) as usize;
self.2 = (p + d) as usize;
easy::SeekResult::Ok
}
},
}
Current(d) => {
let p = self.2 as i64;
if p+d < 0 {
if p + d < 0 {
easy::SeekResult::Fail
} else {
self.2 = (p+d) as usize;
self.2 = (p + d) as usize;
easy::SeekResult::Ok
}
},
}
}
}
}
pub fn data_client(file: &str) -> Result<Easy2<Collector>, ()> {
let mut client = Easy2::new(Collector(Vec::new(), Vec::new(), 0));
let url = format!("https://cloud.seebruecke.org/public.php/webdav/data/{}", file);
client.url(&url)
.map_err(|_| ())?;
let url = format!(
"https://cloud.seebruecke.org/public.php/webdav/data/{}",
file
);
client.url(&url).map_err(|_| ())?;
client.username(include_str!("cloud_user.txt")).map_err(|_| ())?;
client.http_auth(easy::Auth::new().auto(true)).map_err(|_| ())?;
client.http_auth(easy::Auth::new().auto(true))
.map_err(|_| ())?;
client.ssl_cainfo_blob(include_bytes!("isrg-root-x1.pem"))
.map_err(|_| ())?;
Ok(client)
}
#[tauri::command]
pub async fn pull_data(state: State<'_, Mutex<AppState>>) -> Result<String, ()> {
pub async fn pull_data(
state: State<'_, Mutex<AppState>>,
) -> Result<String, ()> {
let mut client = data_client("")?;
client.custom_request("PROPFIND").map_err(|_| ())?;
client.perform().map_err(|_| ())?;
@ -76,7 +81,9 @@ pub async fn pull_data(state: State<'_, Mutex<AppState>>) -> Result<String, ()>
}
#[tauri::command]
pub async fn push_data(state: State<'_, Mutex<AppState>>) -> Result<String, ()> {
pub async fn push_data(
state: State<'_, Mutex<AppState>>,
) -> Result<String, ()> {
let state = state.lock().await;
let key_file = format!("{:016X}.key", state.id);
let mut client = data_client(&key_file)?;

View file

@ -4,8 +4,8 @@ use rusqlite::{types::ToSqlOutput, Connection, ToSql};
use tauri::{Manager, State};
use tokio::sync::Mutex;
mod data_door;
mod app_state;
mod data_door;
use app_state::AppState;
@ -63,22 +63,31 @@ async fn swap(
rng.gen::<i64>(),
store,
acc,
state.id,
i64::from_ne_bytes(state.id.to_ne_bytes()),
Utc::now().timestamp(),
false,
),
)
.map_err(|_| ())?;
.map_err(|e| println!("{:?}", e))?;
Ok(())
}
#[tauri::command]
async fn count(state: State<'_, Mutex<AppState>>) -> Result<String, ()> {
let state = state.lock().await;
let mut stmt = state.db.prepare("SELECT COUNT(*) FROM swap").unwrap();
let mut rows = stmt.query([]).unwrap();
let row = rows.next().unwrap().unwrap();
let cnt: u64 = row.get_unwrap(0);
let mut stmt =
state.db.prepare("SELECT COUNT(*) FROM swap")
.map_err(|e| println!("{:?}", e))?;
let mut rows = stmt.query([]).map_err(|e| println!("{:?}", e))?;
let row = rows.next().map_err(|e| println!("{:?}", e))?;
let row = match row {
Some(r) => Ok(r),
None => {
println!("No rows");
Err(())
}
}?;
let cnt: u64 = row.get(0).map_err(|e| println!("{:?}", e))?;
Ok(cnt.to_string())
}