From 9d8e398cbe97c4f28a515321a40145708d462f24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=80=E1=B4=8D=E1=B4=9B=E1=B4=8F=E1=B4=80=E1=B4=87?= =?UTF-8?q?=CA=80?= Date: Wed, 5 Feb 2025 02:33:15 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=B8=8B=E8=BD=BD=E9=83=A8?= =?UTF-8?q?=E5=88=86=E4=BD=BF=E7=94=A8=20tokio=20=E7=9A=84=E5=B0=81?= =?UTF-8?q?=E8=A3=85=E4=BB=A3=E6=9B=BF=E6=89=8B=E5=8A=A8=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=20(#245)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 5 +++-- Cargo.toml | 1 + crates/bili_sync/Cargo.toml | 1 + crates/bili_sync/src/downloader.rs | 12 ++++-------- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5ee0acc..e4449a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -414,6 +414,7 @@ dependencies = [ "strum", "thiserror 2.0.11", "tokio", + "tokio-util", "toml", "tracing", "tracing-subscriber", @@ -3274,9 +3275,9 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.7.11" +version = "0.7.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cf6b47b3771c49ac75ad09a6162f53ad4b8088b76ac60e8ec1455b31a189fe1" +checksum = "d7fcaa8d55a2bdd6b83ace262b016eca0d79ee02818c5c1bcdf0305114081078" dependencies = [ "bytes", "futures-core", diff --git a/Cargo.toml b/Cargo.toml index 7f01bf1..c2b86f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,6 +61,7 @@ serde_urlencoded = "0.7.1" strum = { version = "0.26.3", features = ["derive"] } thiserror = "2.0.11" tokio = { version = "1.43.0", features = ["full"] } +tokio-util = { version = "0.7.13", features = ["io"] } toml = "0.8.19" tracing = "0.1.41" tracing-subscriber = { version = "0.3.19", features = ["chrono"] } diff --git a/crates/bili_sync/Cargo.toml b/crates/bili_sync/Cargo.toml index bc92d98..7ebcf12 100644 --- a/crates/bili_sync/Cargo.toml +++ b/crates/bili_sync/Cargo.toml @@ -41,6 +41,7 @@ serde_urlencoded = { workspace = true } strum = { workspace = true } thiserror = { workspace = true } tokio = { workspace = true } +tokio-util = { workspace = true } toml = { workspace = true } tracing = { workspace = true } tracing-subscriber = { workspace = true } diff --git a/crates/bili_sync/src/downloader.rs b/crates/bili_sync/src/downloader.rs index 8909c43..e3bb747 100644 --- a/crates/bili_sync/src/downloader.rs +++ b/crates/bili_sync/src/downloader.rs @@ -2,10 +2,11 @@ use core::str; use std::path::Path; use anyhow::{bail, ensure, Result}; -use futures::StreamExt; +use futures::TryStreamExt; use reqwest::Method; use tokio::fs::{self, File}; use tokio::io::AsyncWriteExt; +use tokio_util::io::StreamReader; use crate::bilibili::Client; pub struct Downloader { @@ -27,13 +28,8 @@ 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_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; - file.write_all(&bytes).await?; - } + let mut stream_reader = StreamReader::new(resp.bytes_stream().map_err(std::io::Error::other)); + let received = tokio::io::copy(&mut stream_reader, &mut file).await?; file.flush().await?; ensure!( received >= expected,