Skip to content

Commit 9a8264a

Browse files
committed
Some fixes
1 parent 0bafd6a commit 9a8264a

File tree

6 files changed

+38
-16
lines changed

6 files changed

+38
-16
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dwrs"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
authors = ["vadim rofeds12345@gmail.com"]
55
edition = "2024"
66
description = "CLI tool for parallel file downloads with progress bar and i18n support"

src/config.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ use serde::{Deserialize, Serialize};
77
struct ConfigFile {
88
pub template: Option<String>,
99
pub bar_chars: Option<String>,
10-
pub workers: Option<u8>,
10+
pub workers: Option<usize>,
1111
}
1212

1313
#[derive(Debug)]
1414
pub struct Config {
1515
pub template: String,
1616
pub bar_chars: String,
17-
pub workers: u8,
17+
pub workers: usize,
1818
}
1919

2020
impl Config {
@@ -27,10 +27,11 @@ impl Config {
2727
bar_chars: None,
2828
workers: None,
2929
});
30+
let default = Self::default();
3031
Self {
31-
template: config_file.template.unwrap_or(Self::default().template),
32-
bar_chars: config_file.bar_chars.unwrap_or(Self::default().bar_chars),
33-
workers: config_file.workers.unwrap_or(Self::default().workers),
32+
template: config_file.template.unwrap_or(default.template),
33+
bar_chars: config_file.bar_chars.unwrap_or(default.bar_chars),
34+
workers: config_file.workers.unwrap_or(default.workers),
3435
}
3536
}
3637

src/download.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ pub async fn download_file(
1212
resume: bool,
1313
workers: usize,
1414
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
15-
let head_resp = client.head(url).send().await?;
15+
let head_resp = client.head(url).send().await.ok();
1616
let total_size = head_resp
17-
.headers()
18-
.get(reqwest::header::CONTENT_LENGTH)
17+
.as_ref()
18+
.and_then(|r| r.headers().get(reqwest::header::CONTENT_LENGTH))
1919
.and_then(|v| v.to_str().ok()?.parse::<u64>().ok())
2020
.unwrap_or(0);
2121

2222
let accept_ranges = head_resp
23-
.headers()
24-
.get(reqwest::header::ACCEPT_RANGES)
23+
.as_ref()
24+
.and_then(|r| r.headers().get(reqwest::header::ACCEPT_RANGES))
2525
.and_then(|v| v.to_str().ok())
2626
.unwrap_or("");
2727

@@ -88,7 +88,12 @@ async fn download_range(
8888

8989
let mut offset = start;
9090
if resume && output.exists() {
91-
offset += fs::metadata(output).await?.len();
91+
let existing_len = fs::metadata(output).await?.len();
92+
if existing_len > end - start + 1 {
93+
fs::remove_file(output).await.ok();
94+
} else {
95+
offset += existing_len;
96+
}
9297
}
9398

9499
let mut request = client.get(url);

src/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub mod progress;
2727

2828
use futures::stream::{FuturesUnordered, StreamExt};
2929
use indicatif::MultiProgress;
30+
use notify_rust::Notification;
3031
use reqwest::Client;
3132
use rust_i18n::t;
3233
use tokio::sync::Semaphore;
@@ -114,7 +115,15 @@ impl Downloader {
114115
self.config.continue_download,
115116
self.config.workers,
116117
)
117-
.await
118+
.await?;
119+
if self.config.notify {
120+
Notification::new()
121+
.summary("Download end")
122+
.body("Download end")
123+
.show()
124+
.ok();
125+
}
126+
Ok(())
118127
}
119128

120129
/// Download multiple files in parallel
@@ -174,6 +183,13 @@ impl Downloader {
174183
while let Some(result) = tasks.next().await {
175184
let _ = result??;
176185
}
186+
if self.config.notify {
187+
Notification::new()
188+
.summary("Downloading end")
189+
.body("download end")
190+
.show()
191+
.ok();
192+
}
177193

178194
Ok(())
179195
}

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ async fn main() {
2727

2828
let mut workers = cfg.workers;
2929
if args.workers != 1 {
30-
workers = args.workers as u8;
30+
workers = args.workers;
3131
}
3232
let download_config = dwrs::DownloadConfig {
33-
workers: workers as usize,
33+
workers: workers,
3434
template: cfg.template,
3535
chars: cfg.bar_chars,
3636
continue_download: args.continue_,

0 commit comments

Comments
 (0)