From 046ba643e2d28831fa4efd3ebbb4b3d2cf03122e Mon Sep 17 00:00:00 2001 From: quexeky Date: Wed, 6 Nov 2024 16:39:30 +1100 Subject: [PATCH] refactor(downloads): Scoping changes and removing qualifications Signed-off-by: quexeky --- src-tauri/src/downloads/download_commands.rs | 25 ++++++++++++++++---- src-tauri/src/downloads/download_logic.rs | 14 +++++------ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src-tauri/src/downloads/download_commands.rs b/src-tauri/src/downloads/download_commands.rs index 064bc55..d39da69 100644 --- a/src-tauri/src/downloads/download_commands.rs +++ b/src-tauri/src/downloads/download_commands.rs @@ -95,11 +95,12 @@ pub async fn stop_specific_game_download( game_id: String, ) -> Result<(), String> { info!("called stop_specific_game_download"); - let lock = state.lock().unwrap(); - let download_agent = lock.game_downloads.get(&game_id).unwrap(); + let callback = { + let lock = state.lock().unwrap(); + let download_agent = lock.game_downloads.get(&game_id).unwrap(); + download_agent.callback.clone() + }; - let callback = download_agent.callback.clone(); - drop(lock); info!("Stopping callback"); callback.store(true, Ordering::Release); @@ -118,3 +119,19 @@ pub async fn get_game_download_progress( info!("{}", progress); Ok(progress) } + +/* +#[tauri::command] +async fn resume_game_download( + state: tauri::State<'_, Mutex>, + game_id: String, +) -> Result<(), String> { + + let download = { + let lock = state.lock().unwrap(); + lock.game_downloads.get(&game_id).unwrap().clone() + }; + + Ok(()) +} +*/ \ No newline at end of file diff --git a/src-tauri/src/downloads/download_logic.rs b/src-tauri/src/downloads/download_logic.rs index 121a844..fa99b92 100644 --- a/src-tauri/src/downloads/download_logic.rs +++ b/src-tauri/src/downloads/download_logic.rs @@ -38,7 +38,7 @@ impl DropFileWriter { } // TODO: Implement error handling impl Write for DropFileWriter { - fn write(&mut self, buf: &[u8]) -> std::io::Result { + fn write(&mut self, buf: &[u8]) -> io::Result { if self.callback.load(Ordering::Acquire) { return Err(Error::new( ErrorKind::ConnectionAborted, @@ -53,13 +53,13 @@ impl Write for DropFileWriter { self.file.write(buf) } - fn flush(&mut self) -> std::io::Result<()> { + fn flush(&mut self) -> io::Result<()> { self.hasher.flush()?; self.file.flush() } } impl Seek for DropFileWriter { - fn seek(&mut self, pos: SeekFrom) -> std::io::Result { + fn seek(&mut self, pos: SeekFrom) -> io::Result { self.file.seek(pos) } } @@ -101,18 +101,19 @@ pub fn download_game_chunk( .expect("Failed to seek to file offset"); } - // Writing everything to disk directly is probably slightly faster because it balances out the writes, - // but this is better than the performance loss from constantly reading the callbacks + // Writing everything to disk directly is probably slightly faster in terms of disk + // speed because it balances out the writes, but this is better than the performance + // loss from constantly reading the callbacks let mut writer = BufWriter::with_capacity(1024 * 1024, file); - //copy_to_drop_file_writer(&mut response, &mut file); match io::copy(&mut response, &mut writer) { Ok(_) => {} Err(e) => { info!("Copy errored with error {}", e) } } + writer.flush().unwrap(); let file = match writer.into_inner() { Ok(file) => file, Err(_) => { @@ -129,5 +130,4 @@ pub fn download_game_chunk( ); } - // stream.flush().unwrap(); }