Reduced panics

This commit is contained in:
Daniel M 2021-03-31 17:30:16 +02:00
parent 182442ba3b
commit 36ad1263b4
3 changed files with 40 additions and 36 deletions

View File

@ -28,9 +28,7 @@ pub enum DlStatus {
DoneErr { DoneErr {
filename: String filename: String
}, },
Message(String), Message(String)
/// Like Message but triggers a display refresh
MessageNow(String)
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -53,13 +51,14 @@ impl DlReporter {
} }
} }
pub fn send(& self, status: DlStatus) -> ResBE<()> { pub fn send(& self, status: DlStatus) {
// This should not fail, so unwrap it here instead propagating the error
self.transmitter.send( self.transmitter.send(
DlReport { DlReport {
id: self.id, id: self.id,
status: status status: status
} }
).map_err(|e| e.into()) ).unwrap();
} }
} }
@ -215,9 +214,6 @@ pub async fn watch_and_print_reports(mut receiver: mpsc::UnboundedReceiver<DlRep
}, },
DlStatus::Message(msg) => { DlStatus::Message(msg) => {
msg_queue.push_back(msg); msg_queue.push_back(msg);
},
DlStatus::MessageNow(msg) => {
msg_queue.push_back(msg);
moved_lines = print_accumulated_report(&statuses, &mut msg_queue, moved_lines)?; moved_lines = print_accumulated_report(&statuses, &mut msg_queue, moved_lines)?;
t_last = SystemTime::now(); t_last = SystemTime::now();
} }

View File

@ -120,7 +120,7 @@ pub async fn download_feedback_chunks(url: &str, into_file: &str, rep: DlReporte
bytes_total: content_length, bytes_total: content_length,
filename: filename.to_string() filename: filename.to_string()
} }
)?; );
let mut curr_progress = 0; let mut curr_progress = 0;
@ -184,7 +184,7 @@ pub async fn download_feedback_chunks(url: &str, into_file: &str, rep: DlReporte
speed_mbps: speed_mbps, speed_mbps: speed_mbps,
bytes_curr: curr_progress bytes_curr: curr_progress
} }
)?; );
} }
if buff.len() > 0 { if buff.len() > 0 {
@ -201,7 +201,7 @@ pub async fn download_feedback_chunks(url: &str, into_file: &str, rep: DlReporte
DlStatus::Done { DlStatus::Done {
duration_ms: duration_ms duration_ms: duration_ms
} }
)?; );
Ok(()) Ok(())
} }
@ -255,7 +255,7 @@ pub async fn download_feedback_multi(url: &str, into_file: &str, rep: DlReporter
rep.send(DlStatus::Init { rep.send(DlStatus::Init {
bytes_total: content_length, bytes_total: content_length,
filename: into_file.to_string() filename: into_file.to_string()
})?; });
let rep_task = rep.clone(); let rep_task = rep.clone();
@ -292,7 +292,7 @@ pub async fn download_feedback_multi(url: &str, into_file: &str, rep: DlReporter
rep.send(DlStatus::Update { rep.send(DlStatus::Update {
speed_mbps: speed, speed_mbps: speed,
bytes_curr: curr bytes_curr: curr
}).unwrap(); });
} else { } else {
update_counter += 1; update_counter += 1;
@ -308,7 +308,7 @@ pub async fn download_feedback_multi(url: &str, into_file: &str, rep: DlReporter
}, },
// Just forwared everything else to the calling receiver // Just forwared everything else to the calling receiver
_ => rep.send(update.status).unwrap() _ => rep.send(update.status)
} }
} }
@ -348,7 +348,7 @@ pub async fn download_feedback_multi(url: &str, into_file: &str, rep: DlReporter
rep.send(DlStatus::Done { rep.send(DlStatus::Done {
duration_ms: t_start.elapsed()?.as_millis() as u64 duration_ms: t_start.elapsed()?.as_millis() as u64
})?; });
Ok(()) Ok(())
} }

View File

@ -88,8 +88,6 @@ async fn main() -> ResBE<()> {
.get_matches(); .get_matches();
let outdir = match arguments.value_of("outdir") { let outdir = match arguments.value_of("outdir") {
Some(it) => it, Some(it) => it,
None => "./" None => "./"
@ -133,9 +131,11 @@ async fn main() -> ResBE<()> {
let is_zippy = arguments.is_present("zippyshare"); let is_zippy = arguments.is_present("zippyshare");
if arguments.is_present("listfile") {
let s_listfile = arguments.value_of("listfile").unwrap(); // Evaluate and execute the requested action. The 3 different actions are
// mutally exclusive, so only one of them will be executed
if let Some(s_listfile) = arguments.value_of("listfile") {
let listfile = Path::new(s_listfile); let listfile = Path::new(s_listfile);
@ -154,29 +154,28 @@ async fn main() -> ResBE<()> {
download_multiple(urls, outdir, numparal, boost, is_zippy).await?; download_multiple(urls, outdir, numparal, boost, is_zippy).await?;
} else if arguments.is_present("download") { }
let url = arguments.value_of("download").unwrap(); if let Some(url) = arguments.value_of("download") {
let numparal = if boost != 1 { let boost = if boost != 1 {
boost boost
} else { } else {
numparal numparal
}; };
download_multiple(vec![url.to_string()], outdir, 1, numparal, is_zippy).await?; download_multiple(vec![url.to_string()], outdir, 1, boost, is_zippy).await?;
} else if arguments.is_present("resolve") { }
let url = arguments.value_of("resolve").unwrap(); if let Some(url) = arguments.value_of("resolve") {
match zippy::resolve_link(&url).await { match zippy::resolve_link(&url).await {
Ok(resolved_url) => { Ok(resolved_url) => {
println!("{}", resolved_url); println!("{}", resolved_url);
}, },
Err(e) => { Err(_e) => {
println!("Zippyshare link could not be resolved"); println!("Zippyshare link could not be resolved");
eprintln!("{}", e);
exit(1); exit(1);
} }
@ -233,8 +232,8 @@ async fn download_multiple(urls: Vec<String>, outdir: &str, numparal: i32, boost
Ok(url) => url, Ok(url) => url,
Err(_e) => { Err(_e) => {
rep.send( rep.send(
DlStatus::MessageNow(format!("Zippyshare link could not be resolved: {}", url)) DlStatus::Message(format!("Zippyshare link could not be resolved: {}", url))
).unwrap(); );
continue; continue;
} }
@ -250,14 +249,23 @@ async fn download_multiple(urls: Vec<String>, outdir: &str, numparal: i32, boost
// If file with same name is present locally, check filesize // If file with same name is present locally, check filesize
if path_into_file.exists() { if path_into_file.exists() {
let (filesize, _) = download::http_get_filesize_and_range_support(&url).await.unwrap(); let filesize = match download::http_get_filesize_and_range_support(&url).await {
Ok((filesize, _)) => filesize,
Err(_e) => {
rep.send(
DlStatus::Message(format!("Error while querying metadata: {}", url))
);
continue;
}
};
let local_filesize = std::fs::metadata(path_into_file).unwrap().len(); let local_filesize = std::fs::metadata(path_into_file).unwrap().len();
if filesize == local_filesize { if filesize == local_filesize {
rep.send(DlStatus::MessageNow(format!("Skipping file '{}': already present", &file_name))).unwrap(); rep.send(DlStatus::Message(format!("Skipping file '{}': already present", &file_name)));
continue; continue;
} else { } else {
rep.send(DlStatus::MessageNow(format!("Replacing file '{}': present but not completed", &file_name))).unwrap(); rep.send(DlStatus::Message(format!("Replacing file '{}': present but not completed", &file_name)));
} }
} }
@ -265,13 +273,13 @@ async fn download_multiple(urls: Vec<String>, outdir: &str, numparal: i32, boost
if let Err(_e) = download::download_feedback(&url, &into_file, rep.clone()).await { if let Err(_e) = download::download_feedback(&url, &into_file, rep.clone()).await {
rep.send(DlStatus::DoneErr { rep.send(DlStatus::DoneErr {
filename: into_file.to_string() filename: into_file.to_string()
}).unwrap(); });
} }
} else { } else {
if let Err(_e) = download::download_feedback_multi(&url, &into_file, rep.clone(), boost).await { if let Err(_e) = download::download_feedback_multi(&url, &into_file, rep.clone(), boost).await {
rep.send(DlStatus::DoneErr { rep.send(DlStatus::DoneErr {
filename: into_file.to_string() filename: into_file.to_string()
}).unwrap(); });
} }
}; };