summaryrefslogtreecommitdiffstats
path: root/src/launcher/download.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/launcher/download.rs')
-rw-r--r--src/launcher/download.rs37
1 files changed, 18 insertions, 19 deletions
diff --git a/src/launcher/download.rs b/src/launcher/download.rs
index 846bed1..ec4a59c 100644
--- a/src/launcher/download.rs
+++ b/src/launcher/download.rs
@@ -1,10 +1,9 @@
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
-use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use futures::{stream, StreamExt, TryStream, TryStreamExt};
-use log::{debug, warn};
-use reqwest::{Client, IntoUrl, Method, RequestBuilder};
+use log::debug;
+use reqwest::{Client, Method, RequestBuilder};
use sha1_smol::{Digest, Sha1};
use tokio::fs;
use tokio::fs::File;
@@ -15,13 +14,15 @@ use crate::util::{FileVerifyError, IntegrityError};
pub trait Download: Debug + Display {
// return Ok(None) to skip downloading this file
- fn get_url(&self) -> impl IntoUrl;
-
- async fn prepare(&mut self, req: RequestBuilder) -> Result<Option<RequestBuilder>, Box<dyn Error>>;
+ async fn prepare(&mut self, client: &Client) -> Result<Option<RequestBuilder>, Box<dyn Error>>;
async fn handle_chunk(&mut self, chunk: &[u8]) -> Result<(), Box<dyn Error>>;
async fn finish(&mut self) -> Result<(), Box<dyn Error>>;
}
+pub trait FileDownload: Download {
+ fn get_path(&self) -> &Path;
+}
+
pub struct MultiDownloader<'j, T: Download + 'j, I: Iterator<Item = &'j mut T>> {
jobs: I,
nconcurrent: usize
@@ -110,12 +111,12 @@ impl<'j, T: Download + 'j, I: Iterator<Item = &'j mut T>> MultiDownloader<'j, T,
}
}
- let Some(rq) = map_err!(
- job.prepare(client.request(Method::GET, job.get_url())
- .header(reqwest::header::USER_AGENT, USER_AGENT)).await, Phase::Prepare, job) else {
+ let Some(rq) = map_err!(job.prepare(client).await, Phase::Prepare, job) else {
return Ok(())
};
+ let rq = rq.header(reqwest::header::USER_AGENT, USER_AGENT);
+
let mut data = map_err!(map_err!(rq.send().await, Phase::Send, job).error_for_status(), Phase::Send, job).bytes_stream();
while let Some(bytes) = data.next().await {
@@ -187,10 +188,6 @@ impl VerifiedDownload {
&self.url
}
- pub fn get_path(&self) -> &Path {
- &self.path
- }
-
pub fn get_expect_size(&self) -> Option<usize> {
self.expect_size
}
@@ -210,11 +207,7 @@ impl VerifiedDownload {
}
impl Download for VerifiedDownload {
- fn get_url(&self) -> impl IntoUrl {
- &self.url
- }
-
- async fn prepare(&mut self, req: RequestBuilder) -> Result<Option<RequestBuilder>, Box<dyn Error>> {
+ async fn prepare(&mut self, client: &Client) -> Result<Option<RequestBuilder>, Box<dyn Error>> {
if !util::should_download(&self.path, self.expect_size, self.expect_sha1).await? {
return Ok(None)
}
@@ -222,7 +215,7 @@ impl Download for VerifiedDownload {
// potentially racy to close the file and reopen it... :/
self.open_output().await?;
- Ok(Some(req))
+ Ok(Some(client.request(Method::GET, &self.url)))
}
async fn handle_chunk(&mut self, chunk: &[u8]) -> Result<(), Box<dyn Error>> {
@@ -257,6 +250,12 @@ impl Download for VerifiedDownload {
}
}
+impl FileDownload for VerifiedDownload {
+ fn get_path(&self) -> &Path {
+ &self.path
+ }
+}
+
pub async fn verify_files(files: impl Iterator<Item = &mut VerifiedDownload>) -> Result<(), FileVerifyError> {
stream::iter(files)
.map(|dl| Ok(async move {