Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
rbtcollins committed Aug 15, 2023
1 parent 49ebf74 commit 3c4ced6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
9 changes: 5 additions & 4 deletions src/dist/download.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fs;
use std::ops;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use anyhow::{anyhow, Context, Result};
use sha2::{Digest, Sha256};
Expand All @@ -13,12 +14,12 @@ use crate::utils::utils;

const UPDATE_HASH_LEN: usize = 20;

#[derive(Copy, Clone)]
#[derive(Clone)]
pub struct DownloadCfg<'a> {
pub dist_root: &'a str,
pub temp_cfg: &'a temp::Cfg,
pub download_dir: &'a PathBuf,
pub notify_handler: &'a dyn Fn(Notification<'_>),
pub notify_handler: Arc<dyn Fn(Notification<'_>)>,
}

pub(crate) struct File {
Expand Down Expand Up @@ -76,7 +77,7 @@ impl<'a> DownloadCfg<'a> {
&partial_file_path,
Some(&mut hasher),
true,
&|n| (self.notify_handler)(n.into()),
Some(Arc::new(|n| (self.notify_handler)(n.into()))),
)
.await
{
Expand Down Expand Up @@ -170,7 +171,7 @@ impl<'a> DownloadCfg<'a> {
let file = self.temp_cfg.new_file_with_ext("", ext)?;

let mut hasher = Sha256::new();
utils::run_future(utils::download_file(&url, &file, Some(&mut hasher), &|n| {
utils::run_future(utils::download_file(&url, &file, Some(&mut hasher), |n| {
(self.notify_handler)(n.into())
}))?;
let actual_hash = format!("{:x}", hasher.finalize());
Expand Down
2 changes: 1 addition & 1 deletion src/dist/manifestation/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ fn update_from_dist(
&manifest_url,
&manifest_file,
None,
&|_| {},
None,
))?;
let manifest_str = utils::read_file("manifest", &manifest_file)?;
let manifest = Manifest::parse(&manifest_str)?;
Expand Down
23 changes: 14 additions & 9 deletions src/utils/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fs::{self, File};
use std::future::Future;
use std::io::{self, BufReader, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;

use anyhow::{anyhow, bail, Context, Result};
use home::env as home;
Expand Down Expand Up @@ -138,17 +139,17 @@ pub async fn download_file(
url: &Url,
path: &Path,
hasher: Option<&mut Sha256>,
notify_handler: &dyn Fn(Notification<'_>),
notify_handler: Option<Arc<dyn Fn(Notification<'_>)>>,
) -> Result<()> {
download_file_with_resume(url, path, hasher, false, &notify_handler).await
download_file_with_resume(url, path, hasher, false, notify_handler).await
}

pub(crate) async fn download_file_with_resume(
url: &Url,
path: &Path,
hasher: Option<&mut Sha256>,
resume_from_partial: bool,
notify_handler: &dyn Fn(Notification<'_>),
notify_handler: Option<Arc<dyn Fn(Notification<'_>)>>,
) -> Result<()> {
use download::DownloadError as DEK;
match download_file_(url, path, hasher, resume_from_partial, notify_handler).await {
Expand Down Expand Up @@ -183,14 +184,16 @@ async fn download_file_(
path: &Path,
hasher: Option<&mut Sha256>,
resume_from_partial: bool,
notify_handler: &dyn Fn(Notification<'_>),
notify_handler: Option<Arc<dyn Fn(Notification<'_>)>>,
) -> Result<()> {
use download::download_to_path_with_backend;
use download::{Backend, Event, TlsBackend};
use sha2::Digest;
use std::cell::RefCell;

notify_handler(Notification::DownloadingFile(url, path));
notify_handler
.as_ref()
.map(|notify_handler| notify_handler(Notification::DownloadingFile(url, path)));

let hasher = RefCell::new(hasher);

Expand All @@ -203,7 +206,7 @@ async fn download_file_(
}
}

match msg {
notify_handler.as_ref().map(|notify_handler| match msg {
Event::DownloadContentLengthReceived(len) => {
notify_handler(Notification::DownloadContentLengthReceived(len));
}
Expand All @@ -213,7 +216,7 @@ async fn download_file_(
Event::ResumingPartialDownload => {
notify_handler(Notification::ResumingPartialDownload);
}
}
});

Ok(())
};
Expand All @@ -240,12 +243,14 @@ async fn download_file_(
};
(Backend::Reqwest(tls_backend), Notification::UsingReqwest)
};
notify_handler(notification);
notify_handler
.as_ref()
.map(|notify_handler| notify_handler(notification));
let res =
download_to_path_with_backend(backend, url, path, resume_from_partial, Some(callback))
.await;

notify_handler(Notification::DownloadFinished);
notify_handler.map(|notify_handler| notify_handler(Notification::DownloadFinished));

res
}
Expand Down

0 comments on commit 3c4ced6

Please sign in to comment.