This commit is contained in:
Daniel M 2022-03-31 18:19:58 +02:00
parent c9ac3dd683
commit 16d0edbbb6
3 changed files with 63 additions and 38 deletions

View File

@ -47,6 +47,46 @@ impl DlReporter {
}) })
.unwrap(); .unwrap();
} }
pub fn init(&self, bytes_total: u64, filename: String) {
self.send(DlStatus::Init {
bytes_total,
filename,
})
}
pub fn update(&self, speed_mbps: f32, bytes_curr: u64) {
self.send(DlStatus::Update {
speed_mbps,
bytes_curr,
})
}
pub fn done(&self, duration_ms: u64) {
self.send(DlStatus::Done { duration_ms })
}
pub fn done_err(&self, filename: String) {
self.send(DlStatus::DoneErr { filename })
}
pub fn skipped(&self) {
self.send(DlStatus::Skipped);
}
pub fn msg(&self, msg: String) {
self.send(DlStatus::Message(msg));
}
}
#[macro_export]
macro_rules! report_msg {
($rep:ident, $fmt:expr) => {
DlReporter::msg(&$rep, $fmt.to_string());
};
($rep:ident, $fmt:expr, $($fmt2:expr),+) => {
DlReporter::msg(&$rep, format!($fmt, $($fmt2,)+));
};
} }
struct InfoHolder { struct InfoHolder {

View File

@ -131,10 +131,7 @@ pub async fn download_feedback_chunks(
let filename = into_file.file_name().unwrap().to_str().unwrap(); let filename = into_file.file_name().unwrap().to_str().unwrap();
// Report the download start // Report the download start
rep.send(DlStatus::Init { rep.init(content_length, filename.to_string());
bytes_total: content_length,
filename: filename.to_string(),
});
let mut curr_progress = 0; let mut curr_progress = 0;
let mut speed_mbps = 0.0; let mut speed_mbps = 0.0;
@ -187,10 +184,7 @@ pub async fn download_feedback_chunks(
} }
// Send status update report // Send status update report
rep.send(DlStatus::Update { rep.update(speed_mbps, curr_progress);
speed_mbps,
bytes_curr: curr_progress,
});
} }
if !buff.is_empty() { if !buff.is_empty() {
@ -207,7 +201,7 @@ pub async fn download_feedback_chunks(
let duration_ms = t_start.elapsed()?.as_millis() as u64; let duration_ms = t_start.elapsed()?.as_millis() as u64;
// Send report that the download is finished // Send report that the download is finished
rep.send(DlStatus::Done { duration_ms }); rep.done(duration_ms);
Ok(()) Ok(())
} }
@ -274,10 +268,7 @@ pub async fn download_feedback_multi(
let filename = Path::new(into_file).file_name().unwrap().to_str().unwrap(); let filename = Path::new(into_file).file_name().unwrap().to_str().unwrap();
rep.send(DlStatus::Init { rep.init(content_length, filename.to_string());
bytes_total: content_length,
filename: filename.to_string(),
});
let rep_task = rep.clone(); let rep_task = rep.clone();
@ -319,10 +310,7 @@ pub async fn download_feedback_multi(
t_last = SystemTime::now(); t_last = SystemTime::now();
} }
rep.send(DlStatus::Update { rep.update(speed_mbps, progress_curr);
speed_mbps,
bytes_curr: progress_curr,
});
} }
DlStatus::Done { duration_ms: _ } => { DlStatus::Done { duration_ms: _ } => {
@ -366,9 +354,7 @@ pub async fn download_feedback_multi(
ofile.set_len(content_length).await?; ofile.set_len(content_length).await?;
rep.send(DlStatus::Done { rep.done(t_start.elapsed()?.as_millis() as u64);
duration_ms: t_start.elapsed()?.as_millis() as u64,
});
Ok(()) Ok(())
} }

View File

@ -18,7 +18,7 @@ use tokio::{
use crate::{ use crate::{
args::CLIArgs, args::CLIArgs,
dlreport::{watch_and_print_reports, DlReport, DlReporter, DlStatus}, dlreport::{watch_and_print_reports, DlReport, DlReporter},
download::{download_feedback, download_feedback_multi, http_get_filesize_and_range_support}, download::{download_feedback, download_feedback_multi, http_get_filesize_and_range_support},
zippy::is_zippyshare_url, zippy::is_zippyshare_url,
}; };
@ -123,10 +123,11 @@ async fn download_job(urls: SyncQueue, reporter: UnboundedSender<DlReport>, cli_
match zippy::resolve_link(&dlreq.url).await { match zippy::resolve_link(&dlreq.url).await {
Ok(url) => url, Ok(url) => url,
Err(_e) => { Err(_e) => {
reporter.send(DlStatus::Message(format!( report_msg!(
reporter,
"Zippyshare link could not be resolved, skipping: {}", "Zippyshare link could not be resolved, skipping: {}",
dlreq.url dlreq.url
))); );
continue; continue;
} }
} }
@ -137,9 +138,7 @@ async fn download_job(urls: SyncQueue, reporter: UnboundedSender<DlReport>, cli_
let info = match http_get_filesize_and_range_support(&url).await { let info = match http_get_filesize_and_range_support(&url).await {
Ok(it) => it, Ok(it) => it,
Err(_e) => { Err(_e) => {
reporter.send(DlStatus::Message(format!( report_msg!(reporter, "Error while querying metadata: {url}");
"Error while querying metadata: {url}"
)));
continue; continue;
} }
}; };
@ -157,27 +156,29 @@ async fn download_job(urls: SyncQueue, reporter: UnboundedSender<DlReport>, cli_
let local_filesize = std::fs::metadata(&into_file).unwrap().len(); let local_filesize = std::fs::metadata(&into_file).unwrap().len();
if info.filesize == local_filesize { if info.filesize == local_filesize {
reporter.send(DlStatus::Message(format!( report_msg!(
reporter,
"Skipping file '{}': already present", "Skipping file '{}': already present",
info.filename info.filename
))); );
reporter.send(DlStatus::Skipped); reporter.skipped();
continue; continue;
} else { } else {
reporter.send(DlStatus::Message(format!( report_msg!(
reporter,
"Replacing file '{}': present but not completed", "Replacing file '{}': present but not completed",
&info.filename &info.filename
))); );
} }
} }
let dl_status = if cli_args.conn_count.get() == 1 { let dl_status = if cli_args.conn_count.get() == 1 {
download_feedback(&url, &into_file, reporter.clone(), Some(info.filesize)).await download_feedback(&url, &into_file, reporter.clone(), Some(info.filesize)).await
} else if !info.range_support { } else if !info.range_support {
reporter.send(DlStatus::Message(format!( report_msg!(
"Server does not support range headers. Downloading with single connection: {}", reporter,
url "Server does not support range headers. Downloading with single connection: {url}"
))); );
download_feedback(&url, &into_file, reporter.clone(), Some(info.filesize)).await download_feedback(&url, &into_file, reporter.clone(), Some(info.filesize)).await
} else { } else {
download_feedback_multi( download_feedback_multi(
@ -191,9 +192,7 @@ async fn download_job(urls: SyncQueue, reporter: UnboundedSender<DlReport>, cli_
}; };
if dl_status.is_err() { if dl_status.is_err() {
reporter.send(DlStatus::DoneErr { reporter.done_err(info.filename);
filename: info.filename,
});
} }
} }
} }