Data provider refactor #11
No files matched your search
@@ -5,7 +5,7 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|||||||
use zstd::{DEFAULT_COMPRESSION_LEVEL, Decoder, Encoder};
|
use zstd::{DEFAULT_COMPRESSION_LEVEL, Decoder, Encoder};
|
||||||
use crate::fs::*;
|
use crate::fs::*;
|
||||||
use crate::notification::NotificationData;
|
use crate::notification::NotificationData;
|
||||||
use crate::notification::NotificationData::{PermissionDenied, WebApiUnavailable};
|
use crate::notification::NotificationData::{CompressError, PermissionDenied, WebApiUnavailable};
|
||||||
|
|
||||||
// const API_URL: &str = "http://127.0.0.1:8089/";
|
// const API_URL: &str = "http://127.0.0.1:8089/";
|
||||||
const API_URL: &str = "https://learning.micialware.ru/";
|
const API_URL: &str = "https://learning.micialware.ru/";
|
||||||
@@ -13,7 +13,7 @@ const API_URL: &str = "https://learning.micialware.ru/";
|
|||||||
pub async fn send_data(id: String) -> Result<(), NotificationData> {
|
pub async fn send_data(id: String) -> Result<(), NotificationData> {
|
||||||
let db_file = db_path();
|
let db_file = db_path();
|
||||||
let data = tokio::fs::read(db_file).await.map_err(|_| WebApiUnavailable)?;
|
let data = tokio::fs::read(db_file).await.map_err(|_| WebApiUnavailable)?;
|
||||||
let data = compress(data);
|
let data = compress(data)?;
|
||||||
|
|
||||||
let api_url = API_URL.to_string();
|
let api_url = API_URL.to_string();
|
||||||
let id_url = format!("{api_url}upload/stream/{id}");
|
let id_url = format!("{api_url}upload/stream/{id}");
|
||||||
@@ -30,10 +30,11 @@ pub async fn send_data(id: String) -> Result<(), NotificationData> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compress(data: Vec<u8>) -> Vec<u8> {
|
fn compress(data: Vec<u8>) -> Result<Vec<u8>, NotificationData> {
|
||||||
let mut encoder = Encoder::new(Vec::new(), DEFAULT_COMPRESSION_LEVEL).unwrap();
|
let mut encoder = Encoder::new(Vec::new(), DEFAULT_COMPRESSION_LEVEL).map_err(|_| CompressError)?;
|
||||||
io::copy(&mut &data[..], &mut encoder).unwrap();
|
io::copy(&mut &data[..], &mut encoder).map_err(|_| PermissionDenied)?;
|
||||||
encoder.finish().unwrap()
|
encoder.finish().map_err(|_| CompressError)?;
|
||||||
|
Ok(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn load_data(id: String, temp: bool) -> Result<(), NotificationData> {
|
pub async fn load_data(id: String, temp: bool) -> Result<(), NotificationData> {
|
||||||
@@ -47,7 +48,7 @@ pub async fn load_data(id: String, temp: bool) -> Result<(), NotificationData> {
|
|||||||
.bytes()
|
.bytes()
|
||||||
.await.map_err(|_| WebApiUnavailable)?
|
.await.map_err(|_| WebApiUnavailable)?
|
||||||
.to_vec();
|
.to_vec();
|
||||||
let data = decompress(data);
|
let data = decompress(data)?;
|
||||||
|
|
||||||
let path = app_data_dir();
|
let path = app_data_dir();
|
||||||
let file_name = if temp { "data.db.tmp" } else { "data.db" };
|
let file_name = if temp { "data.db.tmp" } else { "data.db" };
|
||||||
@@ -66,14 +67,11 @@ pub async fn load_data(id: String, temp: bool) -> Result<(), NotificationData> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn decompress(data: Vec<u8>) -> Vec<u8> {
|
pub fn decompress(data: Vec<u8>) -> Result<Vec<u8>, NotificationData> {
|
||||||
let mut decoder = Decoder::new(&data[..]).unwrap();
|
let mut decoder = Decoder::new(&data[..]).map_err(|_| CompressError)?;
|
||||||
let mut decompressed = Vec::new();
|
let mut decompressed = Vec::new();
|
||||||
let res = io::copy(&mut decoder, &mut decompressed);
|
io::copy(&mut decoder, &mut decompressed).map_err(|_| PermissionDenied)?;
|
||||||
if let Err(e) = res {
|
Ok(decompressed)
|
||||||
info!("{}", e);
|
|
||||||
}
|
|
||||||
decompressed
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn first_sync() -> Result<String, NotificationData> {
|
pub async fn first_sync() -> Result<String, NotificationData> {
|
||||||
|
|||||||
@@ -163,6 +163,9 @@ impl NotificationManager {
|
|||||||
NotificationData::DbLoadError => {
|
NotificationData::DbLoadError => {
|
||||||
text!("Не получается загрузить данные")
|
text!("Не получается загрузить данные")
|
||||||
}
|
}
|
||||||
|
NotificationData::CompressError => {
|
||||||
|
text!("Ошибка сжатия или распаковки данных")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.width(Fill)
|
.width(Fill)
|
||||||
.into()
|
.into()
|
||||||
@@ -187,4 +190,5 @@ pub enum NotificationData {
|
|||||||
WebApiUnavailable,
|
WebApiUnavailable,
|
||||||
DbSaveError,
|
DbSaveError,
|
||||||
DbLoadError,
|
DbLoadError,
|
||||||
|
CompressError,
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user