Skip to content

Commit

Permalink
stash
Browse files Browse the repository at this point in the history
multithreading, error handling
  • Loading branch information
mxve committed Nov 28, 2024
1 parent cc1d810 commit 9c82b88
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 215 deletions.
54 changes: 53 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ nanoserde = "0.1.37"
sha1_smol = "1.0.0"
indicatif = "0.17.8"
webpki = "0.22.4"
rayon = "1.8"

[build-dependencies]
winres = "0.1.12"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ INSTALLDIR=/home/pluto/pluto_dir
- ```--backup-restore <backup>``` - Restore backup
- ```--cdn-url``` - Override CDN URL
- ```-e, --exclude <path>``` - Exclude file/folder from update
- ```--threads <number>``` - Number of download threads (default: 2)

### Building from Source
1. Install [Rust](https://rustup.rs/)
Expand Down
4 changes: 4 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ pub struct Args {
// Exclude remote dirs
#[clap(short, long)]
pub exclude: Vec<String>,

/// Number of download threads
#[clap(long, default_value = "2")]
pub threads: usize,
}

pub fn get() -> Args {
Expand Down
41 changes: 23 additions & 18 deletions src/http.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
use colored::*;
use std::{fs, io::Write, path::Path, str};
use std::{fs, io::Write, path::Path};

pub fn get_body(url: &str) -> Vec<u8> {
pub fn get_body(url: &str) -> Result<Vec<u8>, String> {
let mut res: Vec<u8> = Vec::new();
http_req::request::get(url, &mut res).unwrap_or_else(|error| {
panic!("\n\n{}:\n{:?}", "Error".bright_red(), error);
});

res
http_req::request::get(url, &mut res)
.map_err(|e| format!("Failed to download from {}: {:#?}", url, e))?;
Ok(res)
}

pub fn get_body_string(url: &str) -> String {
String::from_utf8(get_body(url)).unwrap()
pub fn get_body_string(url: &str) -> Result<String, String> {
let body = get_body(url)?;
String::from_utf8(body).map_err(|e| format!("Failed to parse response as UTF-8: {:#?}", e))
}

pub fn download_file(url: &str, file_path: &Path) {
let body = get_body(url);
pub fn download_file(url: &str, file_path: &Path) -> Result<(), String> {
let body = get_body(url)?;

let parent = file_path
.parent()
.ok_or_else(|| format!("Failed to get parent directory of {}", file_path.display()))?;

fs::create_dir_all(parent)
.map_err(|e| format!("Failed to create directory {}: {:#?}", parent.display(), e))?;

fs::File::create(file_path)
.map_err(|e| format!("Failed to create file {}: {:#?}", file_path.display(), e))?
.write_all(&body)
.map_err(|e| format!("Failed to write to file {}: {:#?}", file_path.display(), e))?;

let mut f = fs::File::create(file_path).unwrap_or_else(|error| {
panic!("\n\n{}:\n{:?}", "Error".bright_red(), error);
});
f.write_all(&body).unwrap_or_else(|error| {
panic!("\n\n{}:\n{:?}", "Error".bright_red(), error);
});
Ok(())
}
Loading

0 comments on commit 9c82b88

Please sign in to comment.