Autosync first edition
This commit is contained in:
@@ -5,3 +5,4 @@ pub(crate) mod settings;
|
||||
pub(crate) mod sqlite;
|
||||
pub(crate) mod voice;
|
||||
pub(crate) mod words;
|
||||
pub(crate) mod web_api;
|
||||
|
||||
@@ -55,3 +55,11 @@ fn make_card_stats(conn: &Connection) -> Result<(), rusqlite::Error> {
|
||||
conn.execute(query, ())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn default_connection() -> Connection {
|
||||
let path = app_data_dir();
|
||||
let db_file = path.join("data.db");
|
||||
let connection = Connection::open(db_file).unwrap();
|
||||
connection.execute("PRAGMA foreign_keys = ON;", []).unwrap();
|
||||
connection
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
use std::io;
|
||||
use tokio::fs::{File, OpenOptions};
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader};
|
||||
use zstd::{Decoder, Encoder, DEFAULT_COMPRESSION_LEVEL};
|
||||
use crate::dictionary::app_data_dir;
|
||||
|
||||
const API_URL: &str = "http://127.0.0.1:8089/";
|
||||
// const API_URL: &str = "https://learning.micialware.ru/";
|
||||
|
||||
pub async fn send_data(id: String) {
|
||||
let path = app_data_dir();
|
||||
let db_file = path.join("data.db");
|
||||
let data = tokio::fs::read(db_file).await.unwrap();
|
||||
let data = compress(data);
|
||||
|
||||
let api_url = API_URL.to_string();
|
||||
let id_url = format!("{api_url}upload/stream/{id}");
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let new_version = client.post(&id_url).body(data).send().await.unwrap().text().await.unwrap();
|
||||
set_local_version(new_version.parse::<u32>().unwrap()).await;
|
||||
}
|
||||
|
||||
fn compress(data: Vec<u8>) -> Vec<u8> {
|
||||
let mut encoder = Encoder::new(Vec::new(), DEFAULT_COMPRESSION_LEVEL).unwrap();
|
||||
io::copy(&mut &data[..], &mut encoder).unwrap();
|
||||
let compressed = encoder.finish().unwrap();
|
||||
compressed
|
||||
}
|
||||
|
||||
pub async fn load_data(id: String, temp: bool) {
|
||||
let api_url = API_URL.to_string();
|
||||
let id_url = format!("{api_url}download/{id}");
|
||||
let client = reqwest::Client::new();
|
||||
let data = client
|
||||
.get(&id_url)
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.bytes()
|
||||
.await
|
||||
.unwrap()
|
||||
.to_vec();
|
||||
let data = decompress(data);
|
||||
|
||||
let path = app_data_dir();
|
||||
let file_name = if temp { "data.db.tmp" } else { "data.db" };
|
||||
let db_file = path.join(file_name);
|
||||
|
||||
tokio::fs::write(db_file, data).await.unwrap();
|
||||
|
||||
let version_url = format!("{API_URL}{id}/version");
|
||||
let version = client.get(&version_url).send().await.unwrap().text().await.unwrap();
|
||||
set_local_version(version.parse::<u32>().unwrap()).await;
|
||||
}
|
||||
|
||||
pub fn decompress(data: Vec<u8>) -> Vec<u8> {
|
||||
let mut decoder = Decoder::new(&data[..]).unwrap();
|
||||
let mut decompressed = Vec::new();
|
||||
let res = io::copy(&mut decoder, &mut decompressed);
|
||||
if let Err(e) = res {
|
||||
println!("{}", e);
|
||||
}
|
||||
decompressed
|
||||
}
|
||||
|
||||
pub async fn first_sync() -> String {
|
||||
let id_url = API_URL.to_string() + "generate";
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let id = client
|
||||
.get(&id_url)
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.text()
|
||||
.await
|
||||
.unwrap();
|
||||
println!("id: {}", id);
|
||||
id
|
||||
}
|
||||
|
||||
pub async fn get_web_version(key: &str) -> Result<u32, reqwest::Error> {
|
||||
let id_url = format!("{API_URL}{key}/version");
|
||||
let client = reqwest::Client::new();
|
||||
let version = client.get(&id_url).send().await?.text().await?;
|
||||
return Ok(version.parse::<u32>().unwrap());
|
||||
}
|
||||
|
||||
pub async fn get_local_version() -> u32 {
|
||||
let file = app_data_dir().join("data.meta");
|
||||
if file.exists() {
|
||||
let mut data = String::new();
|
||||
File::open(file.clone()).await.unwrap().read_to_string(&mut data).await.unwrap();
|
||||
return data.parse::<u32>().unwrap();
|
||||
}
|
||||
|
||||
let mut file = OpenOptions::new().write(true).create(true).open(file).await.unwrap();
|
||||
file.write_all("0".as_bytes()).await.unwrap();
|
||||
0
|
||||
}
|
||||
|
||||
pub async fn set_local_version(version: u32) {
|
||||
let file_path = app_data_dir().join("data.meta");
|
||||
let mut file = OpenOptions::new().write(true).create(true).truncate(true).open(file_path).await.unwrap();
|
||||
file.write_all(version.to_string().as_bytes()).await.unwrap();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user