From b888db6a61f4b29dc55b4c607e4fe6bc02f33610 Mon Sep 17 00:00:00 2001 From: amtoaer Date: Wed, 22 Jan 2025 01:52:32 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=95=B0=E6=8D=AE=E5=9D=97?= =?UTF-8?q?=E5=B7=B2=E7=BB=8F=E5=9C=A8=E5=86=85=E5=AD=98=E4=B8=AD=EF=BC=8C?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E4=BD=BF=E7=94=A8=20write=5Fall?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/bili_sync/src/downloader.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/crates/bili_sync/src/downloader.rs b/crates/bili_sync/src/downloader.rs index 34e721d..8909c43 100644 --- a/crates/bili_sync/src/downloader.rs +++ b/crates/bili_sync/src/downloader.rs @@ -5,7 +5,7 @@ use anyhow::{bail, ensure, Result}; use futures::StreamExt; use reqwest::Method; use tokio::fs::{self, File}; -use tokio::io::{self, AsyncWriteExt}; +use tokio::io::AsyncWriteExt; use crate::bilibili::Client; pub struct Downloader { @@ -26,16 +26,13 @@ impl Downloader { } let mut file = File::create(path).await?; let resp = self.client.request(Method::GET, url, None).send().await?; - let expected = resp.content_length().unwrap_or_else(|| { - warn!("content length is missing, fallback to 0"); - 0 - }); + let expected = resp.content_length().unwrap_or_default(); let mut received = 0u64; let mut stream = resp.bytes_stream(); while let Some(bytes) = stream.next().await { let bytes = bytes?; received += bytes.len() as u64; - io::copy(&mut bytes.as_ref(), &mut file).await?; + file.write_all(&bytes).await?; } file.flush().await?; ensure!(