use std::path::Path;
use futures_util::StreamExt;
use tokio::fs::File;
use tokio::io::AsyncWriteExt;
use crate::crypto::EncrawDecryptor;
use crate::error::{Error, Result};
use crate::transport::Transport;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum Strategy {
#[default]
Failover,
Race,
}
pub struct Downloader<'a> {
transport: &'a Transport,
urls: Vec<String>,
key: Option<String>,
strategy: Strategy,
}
impl<'a> Downloader<'a> {
pub fn new(transport: &'a Transport, urls: impl IntoIterator<Item = String>) -> Self {
Self {
transport,
urls: urls.into_iter().collect(),
key: None,
strategy: Strategy::default(),
}
}
#[must_use]
pub fn encraw_key(mut self, key: impl Into<String>) -> Self {
self.key = Some(key.into());
self
}
#[must_use]
pub fn maybe_encraw_key(mut self, key: Option<impl Into<String>>) -> Self {
self.key = key.map(Into::into);
self
}
#[must_use]
pub fn strategy(mut self, strategy: Strategy) -> Self {
self.strategy = strategy;
self
}
pub async fn to_file(&self, path: impl AsRef<Path>) -> Result<u64> {
let path = path.as_ref();
self.ensure_urls()?;
if self.strategy == Strategy::Race && self.urls.len() > 1 {
if let Some(written) = self.race_to_file(path).await? {
return Ok(written);
}
}
let mut last = None;
for url in &self.urls {
match self.stream_to_file(url, path).await {
Ok(written) => return Ok(written),
Err(err) => {
remove_partial(path).await;
last = Some(err);
},
}
}
Err(last.unwrap_or(Error::IdMissing("download url")))
}
pub async fn to_bytes(&self) -> Result<Vec<u8>> {
self.ensure_urls()?;
let mut last = None;
for url in &self.urls {
match self.transport.retrieve(url).await {
Ok(bytes) => {
return match &self.key {
Some(key) => crate::crypto::decrypt_encraw(&bytes, key),
None => Ok(bytes),
};
},
Err(err) => last = Some(err),
}
}
Err(last.unwrap_or(Error::IdMissing("download url")))
}
fn ensure_urls(&self) -> Result<()> {
if self.urls.is_empty() {
return Err(Error::IdMissing("download url"));
}
Ok(())
}
async fn race_to_file(&self, path: &Path) -> Result<Option<u64>> {
let mut opens = self
.urls
.iter()
.map(|url| {
let transport = self.transport;
Box::pin(async move { transport.open_stream(url).await })
})
.collect::<Vec<_>>();
let winner = loop {
if opens.is_empty() {
break None;
}
let (result, _index, rest) = futures_util::future::select_all(opens).await;
match result {
Ok(response) => break Some(response),
Err(_) => opens = rest,
}
};
let Some(response) = winner else {
return Ok(None);
};
if let Ok(written) = self.pipe(response, path).await {
return Ok(Some(written));
}
remove_partial(path).await;
Ok(None)
}
async fn stream_to_file(&self, url: &str, path: &Path) -> Result<u64> {
let response = self.transport.open_stream(url).await?;
self.pipe(response, path).await
}
async fn pipe(&self, response: reqwest::Response, path: &Path) -> Result<u64> {
let mut decryptor = match &self.key {
Some(key) => Some(EncrawDecryptor::from_hex_key(key)?),
None => None,
};
let mut file = File::create(path).await?;
let mut stream = response.bytes_stream();
let mut written = 0u64;
while let Some(chunk) = stream.next().await {
let mut chunk = chunk?.to_vec();
if let Some(decryptor) = &mut decryptor {
decryptor.apply(&mut chunk);
}
file.write_all(&chunk).await?;
written += chunk.len() as u64;
}
file.flush().await?;
Ok(written)
}
}
async fn remove_partial(path: &Path) {
let _ = tokio::fs::remove_file(path).await;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn failover_is_the_default_strategy() {
assert_eq!(Strategy::default(), Strategy::Failover);
}
}