-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge #630: installer: Download and install bitcoind from GUI installer
bf67f94 installer: download and install bitcoind (jp1ac4) d507d57 gui: add subscription to Step trait (jp1ac4) ae23fbc gui: add download module (jp1ac4) b3bc943 gui: add dependencies to download and install bitcoind (jp1ac4) Pull request description: This is a follow-up PR to #592 as part of #570 to download and install bitcoind. I'm creating this draft PR now to facilitate discussion. Once #592 has been merged, I'll rebase on master. ACKs for top commit: darosior: ACK bf67f94 -- modulo a few changes we'll address in follow-ups. I've not reviewed the code but significantly tested it on both Windows and Linux. edouardparis: ACK bf67f94 Tree-SHA512: bda4b8bfbb3a59917d9ea60c074c2b0021229213240bebc4bd176f9909c62ab323d0a8d4becffbac192f29fad92adb81b2da0bc9b37c8eea1654c26ec6699077
- Loading branch information
Showing
11 changed files
with
1,067 additions
and
108 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// This is based on https://github.com/iced-rs/iced/blob/master/examples/download_progress/src/download.rs | ||
// with some modifications to store the downloaded bytes in `Progress::Finished` and `State::Downloading` | ||
// and to keep track of any download errors. | ||
use iced::subscription; | ||
|
||
use std::hash::Hash; | ||
|
||
// Just a little utility function | ||
pub fn file<I: 'static + Hash + Copy + Send + Sync, T: ToString>( | ||
id: I, | ||
url: T, | ||
) -> iced::Subscription<(I, Progress)> { | ||
subscription::unfold(id, State::Ready(url.to_string()), move |state| { | ||
download(id, state) | ||
}) | ||
} | ||
|
||
#[derive(Debug, Hash, Clone)] | ||
pub struct Download<I> { | ||
id: I, | ||
url: String, | ||
} | ||
|
||
/// Possible errors with download. | ||
#[derive(PartialEq, Eq, Debug, Clone)] | ||
pub enum DownloadError { | ||
UnknownContentLength, | ||
RequestError(String), | ||
} | ||
|
||
impl std::fmt::Display for DownloadError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
match self { | ||
Self::UnknownContentLength => { | ||
write!(f, "Response has unknown content length.") | ||
} | ||
Self::RequestError(e) => { | ||
write!(f, "Request error: '{}'.", e) | ||
} | ||
} | ||
} | ||
} | ||
|
||
async fn download<I: Copy>(id: I, state: State) -> ((I, Progress), State) { | ||
match state { | ||
State::Ready(url) => { | ||
let response = reqwest::get(&url).await; | ||
|
||
match response { | ||
Ok(response) => { | ||
if let Some(total) = response.content_length() { | ||
( | ||
(id, Progress::Started), | ||
State::Downloading { | ||
response, | ||
total, | ||
downloaded: 0, | ||
bytes: Vec::new(), | ||
}, | ||
) | ||
} else { | ||
( | ||
(id, Progress::Errored(DownloadError::UnknownContentLength)), | ||
State::Finished, | ||
) | ||
} | ||
} | ||
Err(e) => ( | ||
( | ||
id, | ||
Progress::Errored(DownloadError::RequestError(e.to_string())), | ||
), | ||
State::Finished, | ||
), | ||
} | ||
} | ||
State::Downloading { | ||
mut response, | ||
total, | ||
downloaded, | ||
mut bytes, | ||
} => match response.chunk().await { | ||
Ok(Some(chunk)) => { | ||
let downloaded = downloaded + chunk.len() as u64; | ||
|
||
let percentage = (downloaded as f32 / total as f32) * 100.0; | ||
|
||
bytes.append(&mut chunk.to_vec()); | ||
|
||
( | ||
(id, Progress::Advanced(percentage)), | ||
State::Downloading { | ||
response, | ||
total, | ||
downloaded, | ||
bytes, | ||
}, | ||
) | ||
} | ||
Ok(None) => ((id, Progress::Finished(bytes)), State::Finished), | ||
Err(e) => ( | ||
( | ||
id, | ||
Progress::Errored(DownloadError::RequestError(e.to_string())), | ||
), | ||
State::Finished, | ||
), | ||
}, | ||
State::Finished => { | ||
// We do not let the stream die, as it would start a | ||
// new download repeatedly if the user is not careful | ||
// in case of errors. | ||
iced::futures::future::pending().await | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum Progress { | ||
Started, | ||
Advanced(f32), | ||
Finished(Vec<u8>), | ||
Errored(DownloadError), | ||
} | ||
|
||
pub enum State { | ||
Ready(String), | ||
Downloading { | ||
response: reqwest::Response, | ||
total: u64, | ||
downloaded: u64, | ||
bytes: Vec<u8>, | ||
}, | ||
Finished, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.