Autosync first edition
This commit is contained in:
@@ -5,3 +5,4 @@ pub(crate) mod settings;
|
|||||||
pub(crate) mod sqlite;
|
pub(crate) mod sqlite;
|
||||||
pub(crate) mod voice;
|
pub(crate) mod voice;
|
||||||
pub(crate) mod words;
|
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, ())?;
|
conn.execute(query, ())?;
|
||||||
Ok(())
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
+10
-12
@@ -1,8 +1,4 @@
|
|||||||
//#![windows_subsystem = "windows"]
|
#![windows_subsystem = "windows"]
|
||||||
use mimalloc::MiMalloc;
|
|
||||||
|
|
||||||
#[global_allocator]
|
|
||||||
static GLOBAL: MiMalloc = MiMalloc;
|
|
||||||
mod data_provider;
|
mod data_provider;
|
||||||
mod dictionary;
|
mod dictionary;
|
||||||
mod dictionary_test;
|
mod dictionary_test;
|
||||||
@@ -36,7 +32,11 @@ use iced::{keyboard, Subscription, Theme};
|
|||||||
use iced_core::Size;
|
use iced_core::Size;
|
||||||
use iced_core::window::Position;
|
use iced_core::window::Position;
|
||||||
use rusqlite::Connection;
|
use rusqlite::Connection;
|
||||||
|
use mimalloc::MiMalloc;
|
||||||
|
use crate::data_provider::sqlite::default_connection;
|
||||||
|
|
||||||
|
#[global_allocator]
|
||||||
|
static GLOBAL: MiMalloc = MiMalloc;
|
||||||
const USER_FONT: Font = Font::with_name("Noto Sans JP");
|
const USER_FONT: Font = Font::with_name("Noto Sans JP");
|
||||||
|
|
||||||
fn main() -> iced::Result {
|
fn main() -> iced::Result {
|
||||||
@@ -72,17 +72,14 @@ pub struct AppState {
|
|||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
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();
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
dictionary: vec![],
|
dictionary: vec![],
|
||||||
card_sets: vec![],
|
card_sets: vec![],
|
||||||
word_groups: vec![],
|
word_groups: vec![],
|
||||||
connection,
|
connection: default_connection(),
|
||||||
sync_data: AppSettings { key: None },
|
sync_data: AppSettings { key: None, auto_web_fetch: false },
|
||||||
activity: Default::default(),
|
activity: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -102,5 +99,6 @@ fn fill_state(state: &mut AppState) {
|
|||||||
|
|
||||||
fn load_settings(connection: &Connection) -> AppSettings {
|
fn load_settings(connection: &Connection) -> AppSettings {
|
||||||
let key = get_setting("SYNC_KEY".to_string(), connection);
|
let key = get_setting("SYNC_KEY".to_string(), connection);
|
||||||
AppSettings { key }
|
let fetch = get_setting("AUTO_WEB_FETCH".to_string(), connection).unwrap_or("false".to_string()).parse::<bool>().unwrap();
|
||||||
|
AppSettings { key, auto_web_fetch: fetch }
|
||||||
}
|
}
|
||||||
|
|||||||
+58
-9
@@ -1,27 +1,29 @@
|
|||||||
use crate::state_navigate_handle;
|
|
||||||
use crate::data_provider::history::{get_history_of_set, history_dir};
|
use crate::data_provider::history::{get_history_of_set, history_dir};
|
||||||
use crate::data_provider::sqlite::create_db;
|
use crate::data_provider::sqlite::{create_db, default_connection};
|
||||||
use crate::dictionary::{DictionaryMessage, DictionaryState};
|
use crate::data_provider::web_api::{get_local_version, get_web_version, load_data, set_local_version};
|
||||||
|
use crate::dictionary::{app_data_dir, DictionaryMessage, DictionaryState};
|
||||||
use crate::dictionary_test::{DictionaryQuizMessage, DictionaryQuizState};
|
use crate::dictionary_test::{DictionaryQuizMessage, DictionaryQuizState};
|
||||||
use crate::history::{HistoryMessage, HistoryState};
|
use crate::history::{HistoryMessage, HistoryState};
|
||||||
use crate::message_navigation;
|
use crate::message_navigation;
|
||||||
use crate::navigation::Page::*;
|
use crate::navigation::Page::*;
|
||||||
use crate::navigation::RootMessage::{DataLoaded, Keyboard};
|
use crate::navigation::RootMessage::{DataLoaded, Keyboard, UpdateData};
|
||||||
use crate::quiz::{QuizMessage, QuizState};
|
use crate::quiz::{QuizMessage, QuizState};
|
||||||
use crate::randomizer::{RandomizerMessage, RandomizerState};
|
use crate::randomizer::{RandomizerMessage, RandomizerState};
|
||||||
use crate::repetition::{RepetitionMessage, RepetitionState};
|
use crate::repetition::{RepetitionMessage, RepetitionState};
|
||||||
use crate::repetition_settings::{RepetitionSettingsMessage, RepetitionSettingsState};
|
use crate::repetition_settings::{RepetitionSettingsMessage, RepetitionSettingsState};
|
||||||
use crate::repetitions::{RepetitionsMessage, RepetitionsState};
|
use crate::repetitions::{RepetitionsMessage, RepetitionsState};
|
||||||
use crate::selector::{SelectorMessage, SelectorState};
|
use crate::selector::{SelectorMessage, SelectorState};
|
||||||
|
use crate::state_navigate_handle;
|
||||||
use crate::state_update;
|
use crate::state_update;
|
||||||
use crate::sync::{SyncMessage, SyncState};
|
use crate::sync::{SyncMessage, SyncState};
|
||||||
use crate::view_navigation;
|
use crate::view_navigation;
|
||||||
use crate::word::{WordMessage, WordState};
|
use crate::word::{WordMessage, WordState};
|
||||||
use crate::writing::{WritingMessage, WritingState};
|
use crate::writing::{WritingMessage, WritingState};
|
||||||
use crate::{fill_state, AppState};
|
use crate::{AppState, fill_state};
|
||||||
use chrono::NaiveDate;
|
use chrono::NaiveDate;
|
||||||
use iced::keyboard::Event;
|
use iced::keyboard::Event;
|
||||||
use iced::{Element, Task};
|
use iced::{Element, Task};
|
||||||
|
use reqwest::Error;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
@@ -54,6 +56,8 @@ pub enum RootMessage {
|
|||||||
RepetitionSettings(RepetitionSettingsMessage),
|
RepetitionSettings(RepetitionSettingsMessage),
|
||||||
Keyboard(Event),
|
Keyboard(Event),
|
||||||
DataLoaded(HashMap<u32, Vec<(NaiveDate, u32)>>),
|
DataLoaded(HashMap<u32, Vec<(NaiveDate, u32)>>),
|
||||||
|
None,
|
||||||
|
UpdateData,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Page {
|
pub enum Page {
|
||||||
@@ -81,10 +85,29 @@ pub struct ScreenState {
|
|||||||
impl ScreenState {
|
impl ScreenState {
|
||||||
pub fn boot() -> (ScreenState, Task<RootMessage>) {
|
pub fn boot() -> (ScreenState, Task<RootMessage>) {
|
||||||
create_db();
|
create_db();
|
||||||
(
|
let state = ScreenState::default();
|
||||||
ScreenState::default(),
|
let reading_additional_task = Task::perform(Self::load_additional_data(), |data| data);
|
||||||
Task::perform(Self::load_additional_data(), |data| data),
|
|
||||||
)
|
let final_task;
|
||||||
|
let state_guard = state.app_state.lock().unwrap();
|
||||||
|
if state_guard.sync_data.auto_web_fetch {
|
||||||
|
let key = state_guard.sync_data.key.clone().unwrap();
|
||||||
|
final_task = Task::batch([
|
||||||
|
reading_additional_task,
|
||||||
|
Task::perform(Self::load_web_backup(key), |result| {
|
||||||
|
if let Ok(update) = result && update {
|
||||||
|
UpdateData
|
||||||
|
} else {
|
||||||
|
RootMessage::None
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
final_task = reading_additional_task;
|
||||||
|
}
|
||||||
|
|
||||||
|
drop(state_guard);
|
||||||
|
(state, final_task)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn load_additional_data() -> RootMessage {
|
async fn load_additional_data() -> RootMessage {
|
||||||
@@ -111,6 +134,19 @@ impl ScreenState {
|
|||||||
DataLoaded(map)
|
DataLoaded(map)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn load_web_backup(string: String) -> Result<bool, Error> {
|
||||||
|
let local = get_local_version().await;
|
||||||
|
let web = get_web_version(string.as_str()).await?;
|
||||||
|
if web > local {
|
||||||
|
println!("Web version is newer than local version");
|
||||||
|
load_data(string, true).await;
|
||||||
|
set_local_version(web).await;
|
||||||
|
}else {
|
||||||
|
return Ok(false);
|
||||||
|
}
|
||||||
|
Ok(true)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn update(&mut self, message: RootMessage) -> Task<RootMessage> {
|
pub fn update(&mut self, message: RootMessage) -> Task<RootMessage> {
|
||||||
let time = Instant::now();
|
let time = Instant::now();
|
||||||
if let Keyboard(e) = message {
|
if let Keyboard(e) = message {
|
||||||
@@ -122,6 +158,18 @@ impl ScreenState {
|
|||||||
return Task::none();
|
return Task::none();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let UpdateData = message{
|
||||||
|
println!("Loading data");
|
||||||
|
let mut state = self.app_state.lock().unwrap();
|
||||||
|
let path = app_data_dir();
|
||||||
|
let db_file = path.join("data.db");
|
||||||
|
let temp_db_file = path.join("data.db.tmp");
|
||||||
|
std::fs::rename(temp_db_file, &db_file).unwrap();
|
||||||
|
|
||||||
|
state.connection = default_connection();
|
||||||
|
fill_state(&mut state);
|
||||||
|
return Task::none();
|
||||||
|
}
|
||||||
let task: (Task<RootMessage>, bool) = self.navigate_or_update(message);
|
let task: (Task<RootMessage>, bool) = self.navigate_or_update(message);
|
||||||
|
|
||||||
if task.1 {
|
if task.1 {
|
||||||
@@ -198,6 +246,7 @@ impl ScreenState {
|
|||||||
|
|
||||||
pub struct AppSettings {
|
pub struct AppSettings {
|
||||||
pub(crate) key: Option<String>,
|
pub(crate) key: Option<String>,
|
||||||
|
pub(crate) auto_web_fetch: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
|
|||||||
+18
-75
@@ -1,5 +1,5 @@
|
|||||||
use crate::data_provider::settings::{delete_settings, set_setting};
|
use crate::data_provider::settings::{delete_settings, set_setting};
|
||||||
use crate::dictionary::app_data_dir;
|
use crate::data_provider::web_api::*;
|
||||||
use crate::navigation::Page::PreviousPage;
|
use crate::navigation::Page::PreviousPage;
|
||||||
use crate::navigation::{NavigatedPage, Page};
|
use crate::navigation::{NavigatedPage, Page};
|
||||||
use crate::styling::*;
|
use crate::styling::*;
|
||||||
@@ -7,14 +7,11 @@ use crate::sync::SyncMessage::*;
|
|||||||
use crate::{fill_state, AppState, RootMessage};
|
use crate::{fill_state, AppState, RootMessage};
|
||||||
use iced::widget::button::danger;
|
use iced::widget::button::danger;
|
||||||
use iced::widget::container::rounded_box;
|
use iced::widget::container::rounded_box;
|
||||||
use iced::widget::{button, column, container, progress_bar, row, space, text};
|
use iced::widget::{button, column, container, progress_bar, row, space, text, toggler};
|
||||||
use iced::{Center, Element, Fill, Font, Length, Task};
|
use iced::{Center, Element, Fill, Font, Length, Task};
|
||||||
use std::io;
|
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use zstd::{Decoder, Encoder, DEFAULT_COMPRESSION_LEVEL};
|
|
||||||
|
|
||||||
const API_URL: &str = "https://learning.micialware.ru/";
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub enum SyncMessage {
|
pub enum SyncMessage {
|
||||||
@@ -30,10 +27,12 @@ pub enum SyncMessage {
|
|||||||
NetworkFinished,
|
NetworkFinished,
|
||||||
Disable,
|
Disable,
|
||||||
DisableSync,
|
DisableSync,
|
||||||
|
SwitchAutoSync,
|
||||||
}
|
}
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct SyncState {
|
pub struct SyncState {
|
||||||
state: Arc<Mutex<AppState>>,
|
state: Arc<Mutex<AppState>>,
|
||||||
|
auto_fetch: bool,
|
||||||
frozen: bool,
|
frozen: bool,
|
||||||
progress: f32,
|
progress: f32,
|
||||||
}
|
}
|
||||||
@@ -49,13 +48,17 @@ impl NavigatedPage<SyncMessage> for SyncState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn navigated(&mut self) {
|
fn navigated(&mut self) {}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SyncState {
|
impl SyncState {
|
||||||
pub fn new(state: Arc<Mutex<AppState>>) -> SyncState {
|
pub fn new(state: Arc<Mutex<AppState>>) -> SyncState {
|
||||||
|
let auto_fetch;
|
||||||
|
{
|
||||||
|
auto_fetch = state.lock().unwrap().sync_data.auto_web_fetch;
|
||||||
|
}
|
||||||
Self {
|
Self {
|
||||||
|
auto_fetch,
|
||||||
state,
|
state,
|
||||||
frozen: false,
|
frozen: false,
|
||||||
progress: 0.0,
|
progress: 0.0,
|
||||||
@@ -108,7 +111,7 @@ impl SyncState {
|
|||||||
async { tokio::time::sleep(Duration::from_millis(200)).await },
|
async { tokio::time::sleep(Duration::from_millis(200)).await },
|
||||||
|_| RootMessage::Sync(NextAnimation),
|
|_| RootMessage::Sync(NextAnimation),
|
||||||
),
|
),
|
||||||
Task::perform(load_data(id), |_| RootMessage::Sync(NetworkFinished)),
|
Task::perform(load_data(id, false), |_| RootMessage::Sync(NetworkFinished)),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return tasks;
|
return tasks;
|
||||||
@@ -144,6 +147,12 @@ impl SyncState {
|
|||||||
|
|
||||||
delete_settings("SYNC_KEY".to_string(), &state.connection);
|
delete_settings("SYNC_KEY".to_string(), &state.connection);
|
||||||
}
|
}
|
||||||
|
SwitchAutoSync => {
|
||||||
|
self.auto_fetch = !self.auto_fetch;
|
||||||
|
let mut state = self.state.lock().unwrap();
|
||||||
|
state.sync_data.auto_web_fetch = self.auto_fetch;
|
||||||
|
set_setting("AUTO_WEB_FETCH".to_string(), self.auto_fetch.to_string(), &state.connection);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Task::none()
|
Task::none()
|
||||||
}
|
}
|
||||||
@@ -187,6 +196,7 @@ impl SyncState {
|
|||||||
button("↓ Скачать").style(jl_button).on_press(GetLast)
|
button("↓ Скачать").style(jl_button).on_press(GetLast)
|
||||||
]
|
]
|
||||||
.spacing(DEFAULT_SPACING),
|
.spacing(DEFAULT_SPACING),
|
||||||
|
toggler(self.auto_fetch).label("Автоматическая синхронизация").on_toggle(|_| SwitchAutoSync),
|
||||||
container(network_view).width(Fill),
|
container(network_view).width(Fill),
|
||||||
button("Отключить синхронизацию")
|
button("Отключить синхронизацию")
|
||||||
.style(danger)
|
.style(danger)
|
||||||
@@ -229,73 +239,6 @@ impl SyncState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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/{id}");
|
|
||||||
|
|
||||||
let form = reqwest::multipart::Form::new().part("db", reqwest::multipart::Part::bytes(data));
|
|
||||||
let client = reqwest::Client::new();
|
|
||||||
client.post(&id_url).multipart(form).send().await.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn load_data(id: String) {
|
|
||||||
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 db_file = path.join("data.db");
|
|
||||||
tokio::fs::write(db_file, data).await.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn validate_id(id: &str) -> bool {
|
fn validate_id(id: &str) -> bool {
|
||||||
id.len() == 24 && id.chars().all(|c| c.is_ascii_alphanumeric())
|
id.len() == 24 && id.chars().all(|c| c.is_ascii_alphanumeric())
|
||||||
|
|||||||
Reference in New Issue
Block a user