From 4b95c52b6c8830501c53f33ae82947dedc624a88 Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Wed, 11 May 2022 21:18:07 -0700 Subject: [PATCH] emit download errors --- src-tauri/src/downloader.rs | 52 ++++++++++++++++++++--- src/ui/App.tsx | 2 +- src/ui/components/MiniDialog.tsx | 3 +- src/ui/components/common/DownloadList.css | 7 +++ src/ui/components/common/DownloadList.tsx | 13 +++--- src/ui/components/common/ProgressBar.tsx | 3 +- src/utils/download.ts | 14 ++++++ 7 files changed, 78 insertions(+), 16 deletions(-) diff --git a/src-tauri/src/downloader.rs b/src-tauri/src/downloader.rs index a43d975..09d89e7 100644 --- a/src-tauri/src/downloader.rs +++ b/src-tauri/src/downloader.rs @@ -10,15 +10,26 @@ use futures_util::StreamExt; #[tauri::command] pub async fn download_file(window: tauri::Window, url: &str, path: &str) -> Result<(), String> { // Reqwest setup - let res = reqwest::get(url) - .await - .or(Err(format!("Failed to get {}", url)))?; + let res = match reqwest::get(url) + .await { + Ok(r) => r, + Err(e) => { + emit_download_err(window, format!("Failed to request {}", url), url); + return Err(format!("Failed to request {}", url)); + }, + }; let total_size = res .content_length() .unwrap_or(0); // Create file path - let mut file = File::create(path).or(Err(format!("Failed to create file '{}'", path)))?; + let mut file = match File::create(path) { + Ok(f) => f, + Err(e) => { + emit_download_err(window, format!("Failed to create file '{}'", path), path); + return Err(format!("Failed to create file '{}'", path)); + }, + }; let mut downloaded: u64 = 0; // File stream @@ -26,12 +37,23 @@ pub async fn download_file(window: tauri::Window, url: &str, path: &str) -> Resu // Await chunks while let Some(item) = stream.next().await { - let chunk = item.or(Err(format!("Error while downloading file"))).unwrap(); + let chunk = match item { + Ok(itm) => itm, + Err(e) => { + emit_download_err(window, format!("Error while downloading file"), path); + return Err(format!("Error while downloading file: {}", e)); + }, + }; let vect = &chunk.to_vec()[..]; // Write bytes - file.write_all(&vect) - .or(Err(format!("Error while writing file")))?; + match file.write_all(&vect) { + Ok(x) => x, + Err(e) => { + emit_download_err(window, format!("Error while writing file"), path); + return Err(format!("Error while writing file: {}", e)); + }, + } // New progress let new = min(downloaded + (chunk.len() as u64), total_size); @@ -63,4 +85,20 @@ pub async fn download_file(window: tauri::Window, url: &str, path: &str) -> Resu // We are done return Ok(()); +} + +pub fn emit_download_err(window: tauri::Window, msg: std::string::String, path: &str) { + let mut res_hash = std::collections::HashMap::new(); + + res_hash.insert( + "error".to_string(), + msg.to_string() + ); + + res_hash.insert( + "path".to_string(), + path.to_string() + ); + + window.emit("download_error", &res_hash).unwrap(); } \ No newline at end of file diff --git a/src/ui/App.tsx b/src/ui/App.tsx index e0c9fed..c2b6064 100644 --- a/src/ui/App.tsx +++ b/src/ui/App.tsx @@ -64,7 +64,7 @@ class App extends React.Component { { // Mini downloads section this.state.miniDownloadsOpen ? - { + { this.setState({ miniDownloadsOpen: false }) }}> diff --git a/src/ui/components/MiniDialog.tsx b/src/ui/components/MiniDialog.tsx index fd83f23..c3ca912 100644 --- a/src/ui/components/MiniDialog.tsx +++ b/src/ui/components/MiniDialog.tsx @@ -5,6 +5,7 @@ import './MiniDialog.css' interface IProps { children: React.ReactNode[] | React.ReactNode; + title?: string; closeFn: () => void; } @@ -17,7 +18,7 @@ export default class MiniDialog extends React.Component { return (
-
+ {this.props?.title}
diff --git a/src/ui/components/common/DownloadList.css b/src/ui/components/common/DownloadList.css index e69de29..5be9bca 100644 --- a/src/ui/components/common/DownloadList.css +++ b/src/ui/components/common/DownloadList.css @@ -0,0 +1,7 @@ +.DownloadList { + display: flex; + flex-direction: column; + flex: 1; + overflow-y: auto; + padding: 10px; +} \ No newline at end of file diff --git a/src/ui/components/common/DownloadList.tsx b/src/ui/components/common/DownloadList.tsx index f57847b..8d8fe66 100644 --- a/src/ui/components/common/DownloadList.tsx +++ b/src/ui/components/common/DownloadList.tsx @@ -14,14 +14,17 @@ export default class DownloadList extends React.Component { } render() { + const list = this.props.downloadManager.getDownloads().map((download) => { + return ( + + ) + }) + + return (
{ - this.props.downloadManager.getDownloads().map((download) => { - return ( - - ) - }) + list.length > 0 ? list : 'No downloads present' }
) diff --git a/src/ui/components/common/ProgressBar.tsx b/src/ui/components/common/ProgressBar.tsx index 5aace8d..66b637b 100644 --- a/src/ui/components/common/ProgressBar.tsx +++ b/src/ui/components/common/ProgressBar.tsx @@ -33,9 +33,8 @@ export default class ProgressBar extends React.Component { total: prog?.total || 0, }) - if (this.state.status === 'finished' /* || this.state.status === 'error' */) { + if (this.state.status === 'finished' || this.state.status === 'error') { // Ensure progress is 100% - clearInterval(intv) } }, 500) diff --git a/src/utils/download.ts b/src/utils/download.ts index 6512e83..020d733 100644 --- a/src/utils/download.ts +++ b/src/utils/download.ts @@ -8,6 +8,7 @@ export default class DownloadHandler { progress: number, total: number, status: string, + error?: string, }[] // Pass tauri invoke function @@ -35,6 +36,19 @@ export default class DownloadHandler { const index = this.downloads.findIndex(download => download.path === filename) this.downloads[index].status = 'finished' }) + + listen('download_error', (...payload) => { + // @ts-expect-error shut up typescript + const errorData: { + path: string, + error: string, + } = payload[0]?.payload + + // Set download to error + const index = this.downloads.findIndex(download => download.path === errorData.path) + this.downloads[index].status = 'error' + this.downloads[index].error = errorData.error + }) } getDownloads() {