Fix download_progress and make it work on Wasm

Co-authored-by: Skygrango <skygrango@gmail.com>
This commit is contained in:
Héctor Ramón Jiménez 2024-09-10 19:24:30 +02:00
parent 44235f0c0b
commit 1a0bcdb2f6
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
3 changed files with 48 additions and 77 deletions

View file

@ -12,4 +12,4 @@ iced.features = ["tokio"]
[dependencies.reqwest] [dependencies.reqwest]
version = "0.12" version = "0.12"
default-features = false default-features = false
features = ["rustls-tls"] features = ["stream", "rustls-tls"]

View file

@ -1,91 +1,62 @@
use iced::futures; use iced::futures::{SinkExt, Stream, StreamExt};
use iced::stream::try_channel;
use iced::Subscription; use iced::Subscription;
use std::hash::Hash; use std::hash::Hash;
use std::sync::Arc;
// Just a little utility function // Just a little utility function
pub fn file<I: 'static + Hash + Copy + Send + Sync, T: ToString>( pub fn file<I: 'static + Hash + Copy + Send + Sync, T: ToString>(
id: I, id: I,
url: T, url: T,
) -> iced::Subscription<(I, Progress)> { ) -> iced::Subscription<(I, Result<Progress, Error>)> {
Subscription::run_with_id( Subscription::run_with_id(
id, id,
futures::stream::unfold(State::Ready(url.to_string()), move |state| { download(url.to_string()).map(move |progress| (id, progress)),
use iced::futures::FutureExt;
download(id, state).map(Some)
}),
) )
} }
async fn download<I: Copy>(id: I, state: State) -> ((I, Progress), State) { fn download(url: String) -> impl Stream<Item = Result<Progress, Error>> {
match state { try_channel(1, move |mut output| async move {
State::Ready(url) => { let response = reqwest::get(&url).await?;
let response = reqwest::get(&url).await; let total = response.content_length().ok_or(Error::NoContentLength)?;
match response { let _ = output.send(Progress::Downloading { percent: 0.0 }).await;
Ok(response) => {
if let Some(total) = response.content_length() { let mut byte_stream = response.bytes_stream();
( let mut downloaded = 0;
(id, Progress::Started),
State::Downloading { while let Some(next_bytes) = byte_stream.next().await {
response, let bytes = next_bytes?;
total, downloaded += bytes.len();
downloaded: 0,
}, let _ = output
) .send(Progress::Downloading {
} else { percent: 100.0 * downloaded as f32 / total as f32,
((id, Progress::Errored), State::Finished) })
} .await;
}
Err(_) => ((id, Progress::Errored), State::Finished),
}
} }
State::Downloading {
mut response,
total,
downloaded,
} => match response.chunk().await {
Ok(Some(chunk)) => {
let downloaded = downloaded + chunk.len() as u64;
let percentage = (downloaded as f32 / total as f32) * 100.0; let _ = output.send(Progress::Finished).await;
( Ok(())
(id, Progress::Advanced(percentage)), })
State::Downloading {
response,
total,
downloaded,
},
)
}
Ok(None) => ((id, Progress::Finished), State::Finished),
Err(_) => ((id, Progress::Errored), 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)] #[derive(Debug, Clone)]
pub enum Progress { pub enum Progress {
Started, Downloading { percent: f32 },
Advanced(f32),
Finished, Finished,
Errored,
} }
pub enum State { #[derive(Debug, Clone)]
Ready(String), pub enum Error {
Downloading { RequestFailed(Arc<reqwest::Error>),
response: reqwest::Response, NoContentLength,
total: u64, }
downloaded: u64,
}, impl From<reqwest::Error> for Error {
Finished, fn from(error: reqwest::Error) -> Self {
Error::RequestFailed(Arc::new(error))
}
} }

View file

@ -23,7 +23,7 @@ struct Example {
pub enum Message { pub enum Message {
Add, Add,
Download(usize), Download(usize),
DownloadProgressed((usize, download::Progress)), DownloadProgressed((usize, Result<download::Progress, download::Error>)),
} }
impl Example { impl Example {
@ -114,19 +114,19 @@ impl Download {
} }
} }
pub fn progress(&mut self, new_progress: download::Progress) { pub fn progress(
&mut self,
new_progress: Result<download::Progress, download::Error>,
) {
if let State::Downloading { progress } = &mut self.state { if let State::Downloading { progress } = &mut self.state {
match new_progress { match new_progress {
download::Progress::Started => { Ok(download::Progress::Downloading { percent }) => {
*progress = 0.0; *progress = percent;
} }
download::Progress::Advanced(percentage) => { Ok(download::Progress::Finished) => {
*progress = percentage;
}
download::Progress::Finished => {
self.state = State::Finished; self.state = State::Finished;
} }
download::Progress::Errored => { Err(_error) => {
self.state = State::Errored; self.state = State::Errored;
} }
} }
@ -136,7 +136,7 @@ impl Download {
pub fn subscription(&self) -> Subscription<Message> { pub fn subscription(&self) -> Subscription<Message> {
match self.state { match self.state {
State::Downloading { .. } => { State::Downloading { .. } => {
download::file(self.id, "https://speed.hetzner.de/100MB.bin?") download::file(self.id, "https://huggingface.co/mattshumer/Reflection-Llama-3.1-70B/resolve/main/model-00001-of-00162.safetensors")
.map(Message::DownloadProgressed) .map(Message::DownloadProgressed)
} }
_ => Subscription::none(), _ => Subscription::none(),