Skip to content

Commit fc8fbff

Browse files
committed
fix
1 parent 801a9cb commit fc8fbff

File tree

4 files changed

+21
-126
lines changed

4 files changed

+21
-126
lines changed

Cargo.lock

Lines changed: 1 addition & 78 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "qiniu-uploader"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
edition = "2021"
55
description = "Qiniu upload lib with progress bar"
66
authors = ["linghaihui <[email protected]"]
@@ -19,5 +19,5 @@ mime = "0.3.17"
1919
reqwest = {version = "0.12.5", default-features = false, features = ["rustls-tls", "multipart", "stream", "json"] }
2020
serde = { version = "1.0.204", features = ["derive"] }
2121
serde_json = "1.0.122"
22-
tokio = { version = "1.39.2", features = ["full"] }
22+
tokio = { version = "1.39.2", default-features = false}
2323
tokio-util = "0.7.11"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ async fn main() -> Result<(), anyhow::Error> {
3030
.part_upload_file_with_progress(
3131
"test/Cargo.lock",
3232
f,
33-
file_size as i64,
33+
file_size as usize,
3434
Some(1024 * 1024 * 50),
3535
Some(10),
3636
None,

src/lib.rs

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub struct QiniuUploader {
2828
debug: bool,
2929
}
3030

31-
/// 七牛区域enum,见 <https://developer.qiniu.com/kodo/1671/region-endpoint-fq>
31+
/// 七牛区域Enum,见 <https://developer.qiniu.com/kodo/1671/region-endpoint-fq>
3232
#[derive(Debug, Clone, Copy)]
3333
pub enum QiniuRegionEnum {
3434
Z0,
@@ -174,17 +174,23 @@ impl QiniuUploader {
174174
/// - key: 上传文件的key,如test/Cargo.lock
175175
/// - data: R: AsyncReadExt + Unpin + Send + Sync + 'static
176176
/// - mime: 文件类型
177-
/// - 文件大小,单位 bytes
177+
/// - file_size 文件大小,单位 bytes
178+
/// - progress_style: 进度条样式
178179
pub async fn upload_file_with_progress<R: AsyncReadExt + Unpin + Send + Sync + 'static>(
179180
&self,
180181
key: &str,
181182
data: R,
182183
mime: Mime,
183184
file_size: usize,
185+
progress_style: Option<ProgressStyle>,
184186
) -> Result<(), anyhow::Error> {
185187
let reader = ReaderStream::new(data);
186188
let pb = ProgressBar::new(file_size as u64);
187-
pb.set_style(ProgressStyle::default_bar().template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})").unwrap().progress_chars("#>-"));
189+
let sty = match progress_style {
190+
Some(sty)=>sty,
191+
None=> ProgressStyle::default_bar().template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})").unwrap().progress_chars("#>-")
192+
};
193+
pb.set_style(sty);
188194
let pb1 = pb.clone();
189195
let stream = reader.inspect_ok(move |chunk| {
190196
pb1.inc(chunk.len() as u64);
@@ -400,8 +406,8 @@ impl QiniuUploader {
400406
self,
401407
key: &str,
402408
mut data: R,
403-
file_size: i64,
404-
part_size: Option<i64>,
409+
file_size: usize,
410+
part_size: Option<usize>,
405411
threads: Option<u8>,
406412
progress_style: Option<ProgressStyle>,
407413
) -> Result<(), anyhow::Error> {
@@ -418,27 +424,27 @@ impl QiniuUploader {
418424
// 单个 Part大小范围 1 MB - 1 GB,如果未指定part_size,默认5个线程
419425
let mut part_size = match part_size {
420426
Some(size) => size,
421-
None => file_size / threads.unwrap_or(5) as i64,
427+
None => file_size / threads.unwrap_or(5) as usize,
422428
};
423429
if part_size < 1024 * 1024 {
424430
part_size = 1024 * 1024;
425431
} else if part_size > 1024 * 1024 * 1024 {
426432
part_size = 1024 * 1024 * 1024;
427433
}
428434
loop {
429-
let last_bytes = file_size - upload_bytes;
430-
if last_bytes <= 0 {
435+
if upload_bytes >= file_size {
431436
break;
432437
}
433-
let mut part_size1 = part_size as usize;
438+
let last_bytes = file_size - upload_bytes;
439+
let mut part_size1 = part_size;
434440
// 倒数第二次上传后剩余小于1M,附加到倒数第二次上传
435441
if last_bytes - part_size < 1024 * 1024 && last_bytes < 1024 * 1024 * 1024 {
436-
part_size1 = last_bytes as usize;
442+
part_size1 = last_bytes;
437443
}
438444
let mut buf = vec![0; part_size1];
439445
data.read_exact(&mut buf).await?;
440446
part_number += 1;
441-
upload_bytes += buf.len() as i64;
447+
upload_bytes += buf.len() as usize;
442448
let this = self.clone();
443449
let key = key.to_string();
444450
let upload_id = upload_id.clone();
@@ -466,37 +472,3 @@ impl QiniuUploader {
466472
Ok(())
467473
}
468474
}
469-
470-
#[cfg(test)]
471-
mod tests {
472-
use std::os::unix::fs::MetadataExt;
473-
474-
use mime::APPLICATION_OCTET_STREAM;
475-
use tokio::fs;
476-
477-
use super::*;
478-
479-
#[tokio::test]
480-
async fn it_works() {
481-
let qiniu = QiniuUploader::new(
482-
String::from("access_key"),
483-
String::from("access_secret"),
484-
String::from("bucket"),
485-
Some(QiniuRegionEnum::Z0),
486-
true,
487-
);
488-
let f = fs::File::open("./Cargo.lock").await.unwrap();
489-
let file_size = f.metadata().await.unwrap().size();
490-
qiniu
491-
.part_upload_file_with_progress(
492-
"test/Cargo.lock",
493-
f,
494-
file_size as i64,
495-
Some(1024 * 1024 * 50),
496-
Some(10),
497-
None,
498-
)
499-
.await
500-
.unwrap();
501-
}
502-
}

0 commit comments

Comments
 (0)