mirror of
https://github.com/amtoaer/bili-sync.git
synced 2026-09-05 07:28:04 +08:00
refactor: 下载部分使用 tokio 的封装代替手动实现 (#245)
This commit is contained in:
Generated
+3
-2
@@ -414,6 +414,7 @@ dependencies = [
|
|||||||
"strum",
|
"strum",
|
||||||
"thiserror 2.0.11",
|
"thiserror 2.0.11",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
"tokio-util",
|
||||||
"toml",
|
"toml",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
@@ -3274,9 +3275,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio-util"
|
name = "tokio-util"
|
||||||
version = "0.7.11"
|
version = "0.7.13"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9cf6b47b3771c49ac75ad09a6162f53ad4b8088b76ac60e8ec1455b31a189fe1"
|
checksum = "d7fcaa8d55a2bdd6b83ace262b016eca0d79ee02818c5c1bcdf0305114081078"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bytes",
|
"bytes",
|
||||||
"futures-core",
|
"futures-core",
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ serde_urlencoded = "0.7.1"
|
|||||||
strum = { version = "0.26.3", features = ["derive"] }
|
strum = { version = "0.26.3", features = ["derive"] }
|
||||||
thiserror = "2.0.11"
|
thiserror = "2.0.11"
|
||||||
tokio = { version = "1.43.0", features = ["full"] }
|
tokio = { version = "1.43.0", features = ["full"] }
|
||||||
|
tokio-util = { version = "0.7.13", features = ["io"] }
|
||||||
toml = "0.8.19"
|
toml = "0.8.19"
|
||||||
tracing = "0.1.41"
|
tracing = "0.1.41"
|
||||||
tracing-subscriber = { version = "0.3.19", features = ["chrono"] }
|
tracing-subscriber = { version = "0.3.19", features = ["chrono"] }
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ serde_urlencoded = { workspace = true }
|
|||||||
strum = { workspace = true }
|
strum = { workspace = true }
|
||||||
thiserror = { workspace = true }
|
thiserror = { workspace = true }
|
||||||
tokio = { workspace = true }
|
tokio = { workspace = true }
|
||||||
|
tokio-util = { workspace = true }
|
||||||
toml = { workspace = true }
|
toml = { workspace = true }
|
||||||
tracing = { workspace = true }
|
tracing = { workspace = true }
|
||||||
tracing-subscriber = { workspace = true }
|
tracing-subscriber = { workspace = true }
|
||||||
|
|||||||
@@ -2,10 +2,11 @@ use core::str;
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use anyhow::{bail, ensure, Result};
|
use anyhow::{bail, ensure, Result};
|
||||||
use futures::StreamExt;
|
use futures::TryStreamExt;
|
||||||
use reqwest::Method;
|
use reqwest::Method;
|
||||||
use tokio::fs::{self, File};
|
use tokio::fs::{self, File};
|
||||||
use tokio::io::AsyncWriteExt;
|
use tokio::io::AsyncWriteExt;
|
||||||
|
use tokio_util::io::StreamReader;
|
||||||
|
|
||||||
use crate::bilibili::Client;
|
use crate::bilibili::Client;
|
||||||
pub struct Downloader {
|
pub struct Downloader {
|
||||||
@@ -27,13 +28,8 @@ impl Downloader {
|
|||||||
let mut file = File::create(path).await?;
|
let mut file = File::create(path).await?;
|
||||||
let resp = self.client.request(Method::GET, url, None).send().await?;
|
let resp = self.client.request(Method::GET, url, None).send().await?;
|
||||||
let expected = resp.content_length().unwrap_or_default();
|
let expected = resp.content_length().unwrap_or_default();
|
||||||
let mut received = 0u64;
|
let mut stream_reader = StreamReader::new(resp.bytes_stream().map_err(std::io::Error::other));
|
||||||
let mut stream = resp.bytes_stream();
|
let received = tokio::io::copy(&mut stream_reader, &mut file).await?;
|
||||||
while let Some(bytes) = stream.next().await {
|
|
||||||
let bytes = bytes?;
|
|
||||||
received += bytes.len() as u64;
|
|
||||||
file.write_all(&bytes).await?;
|
|
||||||
}
|
|
||||||
file.flush().await?;
|
file.flush().await?;
|
||||||
ensure!(
|
ensure!(
|
||||||
received >= expected,
|
received >= expected,
|
||||||
|
|||||||
Reference in New Issue
Block a user