|
|
@ -31,7 +31,7 @@ fn read_animeboxes_backup<P: AsRef<Path>>( |
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_to_file(data: &[u8], path: &Path) -> std::io::Result<()> {
|
|
|
|
fn write_to_file(data: &[u8], path: &Path) -> std::io::Result<usize> {
|
|
|
|
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<usize, WriteError> {
|
|
|
|
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<dyn Error>> {
|
|
|
|
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<String> = 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();
|
|
|
|
|
|
|
|