Impl new fn helpers::download_and_extract

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-06-09 01:32:38 +10:00
parent c3b5cb11c2
commit 784d1f0bf6
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -53,6 +53,15 @@ pub async fn remote_exists(url: Url, method: Method) -> Result<bool, BinstallErr
/// Download a file from the provided URL to the provided path /// Download a file from the provided URL to the provided path
pub async fn download<P: AsRef<Path>>(url: &str, path: P) -> Result<(), BinstallError> { pub async fn download<P: AsRef<Path>>(url: &str, path: P) -> Result<(), BinstallError> {
let url = Url::parse(url)?; let url = Url::parse(url)?;
download_and_extract(url, PkgFmt::Bin, path.as_ref()).await
}
/// Download a file from the provided URL and extract it to the provided path
pub async fn download_and_extract<P: AsRef<Path>>(
url: Url,
fmt: PkgFmt,
path: P,
) -> Result<(), BinstallError> {
debug!("Downloading from: '{url}'"); debug!("Downloading from: '{url}'");
let resp = reqwest::get(url.clone()) let resp = reqwest::get(url.clone())
@ -68,7 +77,7 @@ pub async fn download<P: AsRef<Path>>(url: &str, path: P) -> Result<(), Binstall
debug!("Downloading to file: '{}'", path.display()); debug!("Downloading to file: '{}'", path.display());
let mut bytes_stream = resp.bytes_stream(); let mut bytes_stream = resp.bytes_stream();
let mut writer = AsyncFileWriter::new(path, PkgFmt::Bin); let mut writer = AsyncFileWriter::new(path, fmt);
while let Some(res) = bytes_stream.next().await { while let Some(res) = bytes_stream.next().await {
writer.write(res?).await?; writer.write(res?).await?;