Refactor
This commit is contained in:
parent
c9ac3dd683
commit
16d0edbbb6
@ -47,6 +47,46 @@ impl DlReporter {
|
||||
})
|
||||
.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 {
|
||||
|
||||
@ -131,10 +131,7 @@ pub async fn download_feedback_chunks(
|
||||
let filename = into_file.file_name().unwrap().to_str().unwrap();
|
||||
|
||||
// Report the download start
|
||||
rep.send(DlStatus::Init {
|
||||
bytes_total: content_length,
|
||||
filename: filename.to_string(),
|
||||
});
|
||||
rep.init(content_length, filename.to_string());
|
||||
|
||||
let mut curr_progress = 0;
|
||||
let mut speed_mbps = 0.0;
|
||||
@ -187,10 +184,7 @@ pub async fn download_feedback_chunks(
|
||||
}
|
||||
|
||||
// Send status update report
|
||||
rep.send(DlStatus::Update {
|
||||
speed_mbps,
|
||||
bytes_curr: curr_progress,
|
||||
});
|
||||
rep.update(speed_mbps, curr_progress);
|
||||
}
|
||||
|
||||
if !buff.is_empty() {
|
||||
@ -207,7 +201,7 @@ pub async fn download_feedback_chunks(
|
||||
let duration_ms = t_start.elapsed()?.as_millis() as u64;
|
||||
|
||||
// Send report that the download is finished
|
||||
rep.send(DlStatus::Done { duration_ms });
|
||||
rep.done(duration_ms);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -274,10 +268,7 @@ pub async fn download_feedback_multi(
|
||||
|
||||
let filename = Path::new(into_file).file_name().unwrap().to_str().unwrap();
|
||||
|
||||
rep.send(DlStatus::Init {
|
||||
bytes_total: content_length,
|
||||
filename: filename.to_string(),
|
||||
});
|
||||
rep.init(content_length, filename.to_string());
|
||||
|
||||
let rep_task = rep.clone();
|
||||
|
||||
@ -319,10 +310,7 @@ pub async fn download_feedback_multi(
|
||||
t_last = SystemTime::now();
|
||||
}
|
||||
|
||||
rep.send(DlStatus::Update {
|
||||
speed_mbps,
|
||||
bytes_curr: progress_curr,
|
||||
});
|
||||
rep.update(speed_mbps, progress_curr);
|
||||
}
|
||||
DlStatus::Done { duration_ms: _ } => {
|
||||
|
||||
@ -366,9 +354,7 @@ pub async fn download_feedback_multi(
|
||||
|
||||
ofile.set_len(content_length).await?;
|
||||
|
||||
rep.send(DlStatus::Done {
|
||||
duration_ms: t_start.elapsed()?.as_millis() as u64,
|
||||
});
|
||||
rep.done(t_start.elapsed()?.as_millis() as u64);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
35
src/main.rs
35
src/main.rs
@ -18,7 +18,7 @@ use tokio::{
|
||||
|
||||
use crate::{
|
||||
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},
|
||||
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 {
|
||||
Ok(url) => url,
|
||||
Err(_e) => {
|
||||
reporter.send(DlStatus::Message(format!(
|
||||
report_msg!(
|
||||
reporter,
|
||||
"Zippyshare link could not be resolved, skipping: {}",
|
||||
dlreq.url
|
||||
)));
|
||||
);
|
||||
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 {
|
||||
Ok(it) => it,
|
||||
Err(_e) => {
|
||||
reporter.send(DlStatus::Message(format!(
|
||||
"Error while querying metadata: {url}"
|
||||
)));
|
||||
report_msg!(reporter, "Error while querying metadata: {url}");
|
||||
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();
|
||||
|
||||
if info.filesize == local_filesize {
|
||||
reporter.send(DlStatus::Message(format!(
|
||||
report_msg!(
|
||||
reporter,
|
||||
"Skipping file '{}': already present",
|
||||
info.filename
|
||||
)));
|
||||
reporter.send(DlStatus::Skipped);
|
||||
);
|
||||
reporter.skipped();
|
||||
continue;
|
||||
} else {
|
||||
reporter.send(DlStatus::Message(format!(
|
||||
report_msg!(
|
||||
reporter,
|
||||
"Replacing file '{}': present but not completed",
|
||||
&info.filename
|
||||
)));
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let dl_status = if cli_args.conn_count.get() == 1 {
|
||||
download_feedback(&url, &into_file, reporter.clone(), Some(info.filesize)).await
|
||||
} else if !info.range_support {
|
||||
reporter.send(DlStatus::Message(format!(
|
||||
"Server does not support range headers. Downloading with single connection: {}",
|
||||
url
|
||||
)));
|
||||
report_msg!(
|
||||
reporter,
|
||||
"Server does not support range headers. Downloading with single connection: {url}"
|
||||
);
|
||||
download_feedback(&url, &into_file, reporter.clone(), Some(info.filesize)).await
|
||||
} else {
|
||||
download_feedback_multi(
|
||||
@ -191,9 +192,7 @@ async fn download_job(urls: SyncQueue, reporter: UnboundedSender<DlReport>, cli_
|
||||
};
|
||||
|
||||
if dl_status.is_err() {
|
||||
reporter.send(DlStatus::DoneErr {
|
||||
filename: info.filename,
|
||||
});
|
||||
reporter.done_err(info.filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user