Rudimentary DB upload

This commit is contained in:
Bianca Fürstenau 2025-02-11 15:47:31 +01:00
parent 51828ef351
commit ce3a2850ce

View file

@ -1,6 +1,7 @@
use chrono::offset::Utc;
use curl::{easy, easy::Easy2};
use rand::prelude::*;
use rusqlite::{Connection, DatabaseName};
use ring_compat::signature::ed25519::SigningKey;
use tauri::{Manager, State};
use tokio::sync::Mutex;
@ -69,6 +70,17 @@ pub fn data_client(file: &str) -> Result<Easy2<Collector>, ()> {
Ok(client)
}
fn put_client(file: &str, payload: &[u8]) -> Result<Easy2<Collector>, ()> {
let mut client = data_client(&file)?;
client.put(true).map_err(|_| ())?;
client.get_mut().1.extend_from_slice(payload);
client.in_filesize(payload.len() as u64)
.map_err(|e| println!("{:?}", e))?;
client.upload(true)
.map_err(|e| println!("{:?}", e))?;
Ok(client)
}
#[tauri::command]
pub async fn pull_data(
state: State<'_, Mutex<AppState>>,
@ -80,19 +92,33 @@ pub async fn pull_data(
Ok(String::from_utf8_lossy(content).to_string())
}
async fn push_key(id: &u64, key: &SigningKey) -> Result<(), ()> {
let file = format!("{:016X}.key", id);
let v_key = key.verifying_key();
let mut client = put_client(&file, v_key.as_ref())?;
let perf = client.perform()
.map_err(|e| println!("{:?}", e))?;
Ok(())
}
fn push_db(id: &u64, db: &Connection) -> Result<(), ()> {
let file = format!("{:016X}.sqlite", id);
db.backup(DatabaseName::Main, "tmp.sqlite", None)
.map_err(|e| println!("{:?}", e))?;
let buf = std::fs::read("tmp.sqlite")
.map_err(|e| println!("{:?}", e))?;
let mut client = put_client(&file, buf.as_ref())?;
let perf = client.perform()
.map_err(|e| println!("{:?}", e))?;
Ok(())
}
#[tauri::command]
pub async fn push_data(
state: State<'_, Mutex<AppState>>,
) -> Result<String, ()> {
) -> Result<(), ()> {
let state = state.lock().await;
let key_file = format!("{:016X}.key", state.id);
let mut client = data_client(&key_file)?;
client.put(true).map_err(|_| ())?;
let v_key = state.key.verifying_key();
client.get_mut().1.extend_from_slice(v_key.as_ref());
client.in_filesize(v_key.as_ref().len() as u64);
client.upload(true);
let perf = client.perform().map_err(|_| ())?;
let content = &client.get_ref().0;
Ok(String::from_utf8_lossy(content).to_string())
push_key(&state.id, &state.key).await?;
push_db(&state.id, &state.db);
Ok(())
}