From 045b3d390257bcd5fa016532f687f4d49f3bde6a Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Mon, 9 May 2022 19:09:02 -0700 Subject: [PATCH] begin download handler --- src-tauri/src/downloader.rs | 21 ++++++++++++++++++++- src-tauri/src/main.rs | 1 + src/utils/download.ts | 25 ++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/downloader.rs b/src-tauri/src/downloader.rs index ba32813..5bb1005 100644 --- a/src-tauri/src/downloader.rs +++ b/src-tauri/src/downloader.rs @@ -13,7 +13,7 @@ use tauri::{ // and docs ofc #[tauri::command] -pub async fn download_file(url: &str, path: &str) -> Result<(), String> { +pub async fn download_file(window: tauri::Window, url: &str, path: &str) -> Result<(), String> { // Reqwest setup let res = reqwest::get(url) .await @@ -42,10 +42,29 @@ pub async fn download_file(url: &str, path: &str) -> Result<(), String> { let new = min(downloaded + (chunk.len() as u64), total_size); downloaded = new; + let mut resHash = std::collections::HashMap::new(); + + resHash.insert( + "downloaded".to_string(), + downloaded.to_string() + ); + + resHash.insert( + "total".to_string(), + total_size.to_string() + ); + + resHash.insert( + "path".to_string(), + path.to_string() + ); + // Create event to send to frontend + window.emit("download_progress", &resHash); } // One more "finish" event + window.emit("download_finished", &path); // We are done return Ok(()); diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index d9d2ba7..2f1afd1 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -14,6 +14,7 @@ use opener; fn main() { tauri::Builder::default() .invoke_handler(tauri::generate_handler![run_program]) + .invoke_handler(tauri::generate_handler![downloader::download_file]) .run(tauri::generate_context!()) .expect("error while running tauri application"); } diff --git a/src/utils/download.ts b/src/utils/download.ts index e535c8b..861570c 100644 --- a/src/utils/download.ts +++ b/src/utils/download.ts @@ -1,5 +1,28 @@ + +import { invoke } from '@tauri-apps/api/tauri' +import { listen } from '@tauri-apps/api/event' + export default class DownloadHandler { + downloads: Array + + // Pass tauri invoke function constructor() { - console.log('AAAAAAAAAAAAAAAAAAAAA') + this.downloads = [] + + listen('download_progress', (...payload) => { + console.log(payload) + }) + + listen('download_finished', (...payload) => { + console.log(payload) + }) + } + + addDownload(url: string, path: string) { + // Begin download from rust backend + invoke('download_file', { url, path }) + + // Register event handler + this.downloads.push(path) } } \ No newline at end of file