@@ -234,7 +234,7 @@ async fn start_integrated_update<R: Runtime>(
234234 window : & WebviewWindow < R > ,
235235 update : & Update ,
236236) -> Result < UpdateResponseAction > {
237- let download_path = ensure_download_path ( window, update ) ? ;
237+ let download_path = ensure_download_dir ( window) ? . join ( download_file_name ( update ) ) ;
238238 debug ! ( "Download path: {}" , download_path. display( ) ) ;
239239 let downloaded = download_path. exists ( ) ;
240240 let ack_wait = Duration :: from_secs ( 3 ) ;
@@ -345,7 +345,7 @@ pub async fn download_update_idempotent<R: Runtime>(
345345 window : & WebviewWindow < R > ,
346346 update : & Update ,
347347) -> Result < PathBuf > {
348- let dl_path = ensure_download_path ( window, update ) ? ;
348+ let dl_path = ensure_download_dir ( window) ? . join ( download_file_name ( update ) ) ;
349349
350350 if dl_path. exists ( ) {
351351 info ! ( "{} already downloaded to {}" , update. version, dl_path. display( ) ) ;
@@ -385,21 +385,36 @@ pub async fn install_update_maybe_download<R: Runtime>(
385385 let dl_path = download_update_idempotent ( window, update) . await ?;
386386 let update_bytes = std:: fs:: read ( & dl_path) ?;
387387 update. install ( update_bytes. as_slice ( ) ) ?;
388+ delete_download_dir ( window) ;
388389 Ok ( ( ) )
389390}
390391
391- pub fn ensure_download_path < R : Runtime > (
392- window : & WebviewWindow < R > ,
393- update : & Update ,
394- ) -> Result < PathBuf > {
395- // Ensure dir exists
396- let base_dir = window . path ( ) . app_cache_dir ( ) ? . join ( "updates" ) ;
392+ pub fn download_dir < R : Runtime > ( window : & WebviewWindow < R > ) -> Result < PathBuf > {
393+ Ok ( window. path ( ) . app_cache_dir ( ) ? . join ( "updates" ) )
394+ }
395+
396+ pub fn ensure_download_dir < R : Runtime > ( window : & WebviewWindow < R > ) -> Result < PathBuf > {
397+ let base_dir = download_dir ( window ) ? ;
397398 std:: fs:: create_dir_all ( & base_dir) ?;
399+ Ok ( base_dir)
400+ }
398401
399- // Generate name based on signature
402+ pub fn download_file_name ( update : & Update ) -> String {
400403 let sig_digest = md5:: compute ( & update. signature ) ;
401- let name = format ! ( "yaak-{}-{:x}" , update. version, sig_digest) ;
402- let dl_path = base_dir . join ( name ) ;
404+ format ! ( "yaak-{}-{:x}" , update. version, sig_digest)
405+ }
403406
404- Ok ( dl_path)
407+ pub fn delete_download_dir < R : Runtime > ( window : & WebviewWindow < R > ) {
408+ let base_dir = match download_dir ( window) {
409+ Ok ( dir) => dir,
410+ Err ( e) => {
411+ warn ! ( "Failed to locate update downloads dir: {}" , e) ;
412+ return ;
413+ }
414+ } ;
415+ match std:: fs:: remove_dir_all ( & base_dir) {
416+ Ok ( ( ) ) => info ! ( "Removed update downloads dir {}" , base_dir. display( ) ) ,
417+ Err ( e) if e. kind ( ) == std:: io:: ErrorKind :: NotFound => { }
418+ Err ( e) => warn ! ( "Failed to remove update downloads dir {}: {}" , base_dir. display( ) , e) ,
419+ }
405420}
0 commit comments