tenho seguinte código tauri v2 use serde::{Deserialize, Serialize}; use…
- User
tenho seguinte código tauri v2
use serde::{Deserialize, Serialize}; use std::path::{Path, PathBuf};
use tauri::{ipc::Channel}; use reqwest::Client; use tokio::fs as tokio_fs; use tokio::io::AsyncWriteExt; use std::time::{Duration, Instant}; use std::error::Error;
#[derive(Debug, Deserialize)] struct PatchEntry { file_name: String, }
#[derive(Debug, Serialize, Clone)] pub struct DownloadMessage { message: String, }
#[derive(Debug, Serialize, Clone)] pub struct DownloadInfo { current_file: usize, total_files: usize, file_name: String, downloaded_bytes: u64, total_bytes: u64, }
#[derive(Debug, Serialize, Clone)] #[serde(rename_all = "camelCase", tag = "event", content = "data")] pub enum DownloadEvent { Message(DownloadMessage), Info(DownloadInfo), Speed(f64), }
// Função para obter o diretório do executável fn get_exe_dir() -> PathBuf { std::env::current_exe() .unwrap() .parent() .unwrap() .to_path_buf() }
// Função assíncrona para ler o arquivo de patches async fn read_patch_file(path: &Path) -> Result<Vec, Box> { let content = tokio_fs::read_to_string(path).await?; let patches: Vec = serde_json::from_str(&content)?; Ok(patches) }
// Função para baixar um arquivo async fn download_file( client: &Client, entry: PatchEntry, download_dir: &Path, current_file: usize, total_files: usize, on_event: &Channel, ) -> Result<(), Box> { let url = format!("https://hil-speed.hetzner.com/{}", entry.file_name); let file_path = download_dir.join(&entry.file_name);
codelet mut response = client.get(&url).send().await?; let total_bytes = response.content_length().unwrap_or(0); let mut file = tokio_fs::OpenOptions::new() .write(true) .create(true) .open(&file_path) .await?; let mut downloaded_bytes = 0; let start_time = Instant::now(); on_event.send(DownloadEvent::Message(DownloadMessage { message: "Baixando".to_string(), }))?; let mut last_update_time = Instant::now(); let update_interval = Duration::from_millis(500); while let Some(chunk) = response.chunk().await? { file.write_all(&chunk).await?; downloaded_bytes += chunk.len() as u64; let elapsed_time = Instant::now().duration_since(start_time).as_secs_f64(); let speed = if elapsed_time > 0.0 { downloaded_bytes as f64 / elapsed_time } else { 0.0 }; if last_update_time.elapsed() >= update_interval { last_update_time = Instant::now(); on_event.send(DownloadEvent::Info(DownloadInfo { current_file, total_files, file_name: entry.file_name.clone(), downloaded_bytes, total_bytes, }))?; on_event.send(DownloadEvent::Speed(speed))?; } } on_event.send(DownloadEvent::Message(DownloadMessage { message: format!("Download concluído."),}))?; Ok(()) - Assistant