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 crate::fs::*;
|
||||
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 = "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> {
|
||||
let db_file = db_path();
|
||||
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 id_url = format!("{api_url}upload/stream/{id}");
|
||||
@@ -30,10 +30,11 @@ pub async fn send_data(id: String) -> Result<(), NotificationData> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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();
|
||||
encoder.finish().unwrap()
|
||||
fn compress(data: Vec<u8>) -> Result<Vec<u8>, NotificationData> {
|
||||
let mut encoder = Encoder::new(Vec::new(), DEFAULT_COMPRESSION_LEVEL).map_err(|_| CompressError)?;
|
||||
io::copy(&mut &data[..], &mut encoder).map_err(|_| PermissionDenied)?;
|
||||
encoder.finish().map_err(|_| CompressError)?;
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
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()
|
||||
.await.map_err(|_| WebApiUnavailable)?
|
||||
.to_vec();
|
||||
let data = decompress(data);
|
||||
let data = decompress(data)?;
|
||||
|
||||
let path = app_data_dir();
|
||||
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(())
|
||||
}
|
||||
|
||||
pub fn decompress(data: Vec<u8>) -> Vec<u8> {
|
||||
let mut decoder = Decoder::new(&data[..]).unwrap();
|
||||
pub fn decompress(data: Vec<u8>) -> Result<Vec<u8>, NotificationData> {
|
||||
let mut decoder = Decoder::new(&data[..]).map_err(|_| CompressError)?;
|
||||
let mut decompressed = Vec::new();
|
||||
let res = io::copy(&mut decoder, &mut decompressed);
|
||||
if let Err(e) = res {
|
||||
info!("{}", e);
|
||||
}
|
||||
decompressed
|
||||
io::copy(&mut decoder, &mut decompressed).map_err(|_| PermissionDenied)?;
|
||||
Ok(decompressed)
|
||||
}
|
||||
|
||||
pub async fn first_sync() -> Result<String, NotificationData> {
|
||||
|
||||
@@ -163,6 +163,9 @@ impl NotificationManager {
|
||||
NotificationData::DbLoadError => {
|
||||
text!("Не получается загрузить данные")
|
||||
}
|
||||
NotificationData::CompressError => {
|
||||
text!("Ошибка сжатия или распаковки данных")
|
||||
}
|
||||
}
|
||||
.width(Fill)
|
||||
.into()
|
||||
@@ -187,4 +190,5 @@ pub enum NotificationData {
|
||||
WebApiUnavailable,
|
||||
DbSaveError,
|
||||
DbLoadError,
|
||||
CompressError,
|
||||
}
|
||||
Reference in new issue
Block a user