Auto remove file in AsyncFileWriter

unless done is called.

Also moves creation of the dir/file into the blocking thread to avoid
blocking.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-06-08 22:27:19 +10:00
parent 5d70f61317
commit 911c52d8e1
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
2 changed files with 70 additions and 21 deletions

View file

@ -9,7 +9,6 @@ use flate2::read::GzDecoder;
use futures_util::stream::StreamExt;
use log::{debug, info};
use reqwest::Method;
use scopeguard::ScopeGuard;
use serde::Serialize;
use tar::Archive;
use tinytemplate::TinyTemplate;
@ -68,17 +67,11 @@ pub async fn download<P: AsRef<Path>>(url: &str, path: P) -> Result<(), Binstall
let mut bytes_stream = resp.bytes_stream();
let mut writer = AsyncFileWriter::new(path)?;
let guard = scopeguard::guard(path, |path| {
fs::remove_file(path).ok();
});
while let Some(res) = bytes_stream.next().await {
writer.write(res?).await?;
}
writer.done().await?;
// Disarm as it is successfully downloaded and written to file.
ScopeGuard::into_inner(guard);
debug!("Download OK, written to file: '{}'", path.display());