mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-21 04:58:42 +00:00
Use AsyncFileWriter
in helpers::download
so that writing to file will not block the download. Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
5fdeea86ad
commit
191fd6e981
1 changed files with 12 additions and 5 deletions
|
@ -7,6 +7,7 @@ use std::{
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use cargo_toml::Manifest;
|
use cargo_toml::Manifest;
|
||||||
use flate2::read::GzDecoder;
|
use flate2::read::GzDecoder;
|
||||||
|
use futures_util::stream::StreamExt;
|
||||||
use log::{debug, info};
|
use log::{debug, info};
|
||||||
use reqwest::Method;
|
use reqwest::Method;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
@ -56,13 +57,19 @@ pub async fn download<P: AsRef<Path>>(url: &str, path: P) -> Result<(), Binstall
|
||||||
err,
|
err,
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let bytes = resp.bytes().await?;
|
|
||||||
|
|
||||||
let path = path.as_ref();
|
let path = path.as_ref();
|
||||||
debug!("Download OK, writing to file: '{}'", path.display());
|
debug!("Downloading to file: '{}'", path.display());
|
||||||
|
|
||||||
fs::create_dir_all(path.parent().unwrap())?;
|
let mut bytes_stream = resp.bytes_stream();
|
||||||
fs::write(&path, bytes)?;
|
let writer = AsyncFileWriter::new(path)?;
|
||||||
|
|
||||||
|
while let Some(res) = bytes_stream.next().await {
|
||||||
|
writer.write(res?).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.done().await?;
|
||||||
|
|
||||||
|
debug!("Download OK, written to file: '{}'", path.display());
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue