diff --git a/.gitignore b/.gitignore index 10bf5e1..422de9e 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,7 @@ *.abbj # Ignore json data files -*.json \ No newline at end of file +*.json + +# Ignore default download directory +tmp/ \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index 9e89bed..ad43e90 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,7 +1,7 @@ use std::error::Error; use std::ffi::OsString; use std::fs::File; -use std::io::{BufReader, BufWriter}; +use std::io::BufReader; use std::path::Path; use std::process::exit; @@ -51,7 +51,7 @@ impl ConfigManager { fn load(&mut self) { match read_config(&self.config_path) { Err(e) => { - println!("Failed to read config file {:#?}", &self.config_path); + println!("Failed to read config file {:#?}: {}", &self.config_path, e); exit(1); } Ok(config) => self.config = config, diff --git a/src/main.rs b/src/main.rs index b36db43..bc90b80 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,7 @@ fn read_animeboxes_backup>( Ok(result) } -fn write_to_file(data: &[u8], path: &Path) -> std::io::Result<()> { +fn write_to_file(data: &[u8], path: &Path) -> std::io::Result { if !path.exists() { let mut file = File::create(path)?; file.write_all(data)?; @@ -39,6 +39,33 @@ fn write_to_file(data: &[u8], path: &Path) -> std::io::Result<()> { let mut file = OpenOptions::new().append(true).open(path)?; file.write_all(data)?; } + Ok(data.len()) +} + +fn curl_write_to_file_function(data: &[u8], path: &Path) -> Result { + match write_to_file(data, &path) { + Err(e) => { + println!("Error writing to {:#?}: {}", &path, e); + Err(WriteError::Pause) + } + Ok(d) => Ok(d), + } +} + +fn calculate_percentage(numerator: f64, denominator: f64) -> f64 { + if numerator <= 0.0 { + 0.0 + } else { + (numerator / denominator) * 100.0 + } +} + +fn print_progress(data: &[u8]) -> Result<(), Box> { + let mut stdout = stdout(); + stdout.queue(cursor::SavePosition)?; + stdout.write(data)?; + stdout.queue(cursor::RestorePosition)?; + stdout.flush()?; Ok(()) } @@ -50,7 +77,10 @@ fn download_command(backup: model::anime_boxes::Backup, mut config: config::Conf .as_ref() .unwrap_or(&String::from("tmp")), ); - create_dir_all(&temp_directory); + match create_dir_all(&temp_directory) { + Err(e) => println!("Failed to create directory {:#?}: {}", &temp_directory, e), + Ok(_) => (), + }; let favorites: Vec = backup .favorites .iter() @@ -71,7 +101,7 @@ fn download_command(backup: model::anime_boxes::Backup, mut config: config::Conf let favorite_url = Url::parse(&favorite); match favorite_url { Err(e) => { - println!("Error downloading {}. Not a valid URL", favorite); + println!("Error downloading {}. Not a valid URL. {}", favorite, e); continue; } Ok(url) => { @@ -82,42 +112,41 @@ fn download_command(backup: model::anime_boxes::Backup, mut config: config::Conf let progress_count = format!("{:}/{:}", count, to_download_count); let progress_filename = String::from(filename); - easy.url(url.as_str()); - easy.progress(true); - easy.progress_function( + match easy.url(url.as_str()) { + Err(e) => println!("Error setting download URL {}: {}", &filename, e), + Ok(_) => (), + }; + match easy.progress(true) { + Err(e) => println!("Error setting progress setting: {}", e), + Ok(_) => (), + }; + match easy.progress_function( move |total_bytes_to_download, bytes_downloaded, - total_bytes_to_upload, - bytes_uploaded| { - let percentage = if bytes_downloaded <= 0.0 { - 0.0 - } else { - (bytes_downloaded / total_bytes_to_download) * 100.0 - }; - let mut stdout = stdout(); - stdout.queue(cursor::SavePosition); - stdout.write( + _total_bytes_to_upload, + _bytes_uploaded| { + let percentage = + calculate_percentage(bytes_downloaded, total_bytes_to_download); + match print_progress( format!( "{}: {:.2}% {}", progress_filename, percentage, &progress_count ) .as_bytes(), - ); - stdout.queue(cursor::RestorePosition); - stdout.flush(); + ) { + Err(e) => println!("Error showing downloading progress: {}", e), + Ok(_) => (), + }; true }, - ); - match easy.write_function(move |data| match write_to_file(data, &target_path) { - Err(e) => { - println!("Error writing to {:#?}: {}", target_path, e); - Err(WriteError::Pause) - } - Ok(d) => Ok(data.len()), - }) { - Err(e) => { - println!("Error downloading {}", &favorite); - } + ) { + Err(e) => println!("Error showing downloading progress: {}", e), + Ok(_) => (), + }; + match easy + .write_function(move |data| curl_write_to_file_function(data, &target_path)) + { + Err(e) => println!("Error downloading {}: {}", &favorite, e), Ok(_) => favorites_downloaded.push(String::from(&favorite)), } easy.perform().unwrap(); @@ -197,7 +226,7 @@ fn main() { let config = matches.value_of("config").unwrap_or("config.json"); let config_path = env::current_dir().unwrap().join(config); - let mut config_manager = ConfigManager::new(config_path.as_path()); + let config_manager = ConfigManager::new(config_path.as_path()); let path = matches.value_of("INPUT").unwrap(); let result = read_animeboxes_backup(path).unwrap();