Skip to content

Commit

Permalink
Merge pull request #3 from Blutsh/fb-custom-output-dir
Browse files Browse the repository at this point in the history
Added custom output
  • Loading branch information
Blutsh authored May 13, 2024
2 parents 167ed24 + f7bd217 commit 554f104
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
Binary file removed esp32_datasheet_en.pdf
Binary file not shown.
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ struct Cli {
#[arg(short, long, value_name = "30", value_parser = validate_duration)]
duration: Option<String>,

/// Define an output directory for the downloaded files
#[arg(short, long, value_name = "output")]
output: Option<String>,

/// Enable verbose mode
#[arg(short, long)]
verbose: bool,
Expand Down Expand Up @@ -66,7 +70,7 @@ fn main() -> Result<(), SwishError> {
let swissfiles = Swissfiles::new_remotefiles(&arg, cli.password.as_deref())?;

//Download the files
swissfiles.download(None)?;
swissfiles.download(cli.output.map(PathBuf::from).as_ref())?;

return Ok(());
}
Expand Down
16 changes: 14 additions & 2 deletions src/swissfiles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,19 @@ impl Swissfiles {
})
}

pub fn download(&self, custom_out_path: Option<&Path>) -> Result<(), SwishError> {
pub fn download(&self, custom_out_path: Option<&PathBuf>) -> Result<(), SwishError> {

// Create the directory if it doesn't exist or use the current directory
let out_path = match custom_out_path {
Some(path) => {
if !path.exists() {
Some(std::fs::create_dir_all(path)?);
}
Some(path)
}
None => None,
};

for file in &self.files {
match file {
Swissfile::Local(_) => {
Expand All @@ -155,7 +167,7 @@ impl Swissfiles {
}
Swissfile::Remote(remote_swissfile) => {
// Call download method on RemoteSwissfile
remote_swissfile.download(custom_out_path)?;
remote_swissfile.download(out_path)?;
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/swissfiles/swissfile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
use serde_json::json;
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};
use std::path::PathBuf;
use std::{fmt, path::Path};

const SWISSTRANSFER_API: &str = "https://www.swisstransfer.com/api";
Expand Down Expand Up @@ -71,7 +72,6 @@ impl LocalSwissfile {
easy2.perform()?;
}
Ok(())

}

fn build_chunked_upload_url(&self, chunk: &Chunk) -> String {
Expand Down Expand Up @@ -181,11 +181,14 @@ impl RemoteSwissfile {
Ok(token)
}

pub fn download(&self, custom_out_path: Option<&Path>) -> Result<(), SwishError> {
pub fn download(&self, custom_out_path: Option<&PathBuf>) -> Result<(), SwishError> {
log::debug!("Downloading {} from {}", self.name, self.url.clone());
let out_path = custom_out_path
.unwrap_or_else(|| Path::new("."))
.join(&self.name);
// Dereference the PathBuf if it exists
let out_path = match custom_out_path {
Some(path) => path.join(&self.name),
None => PathBuf::from(".").join(&self.name),
};

let out_path = out_path.to_str().unwrap();
let file = std::fs::File::create(&out_path)?;
let url = self.url.clone();
Expand All @@ -200,10 +203,9 @@ impl RemoteSwissfile {

// we are not sure but we can assume that this is the error x)
Err(SwishError::DownloadNumberExceeded)
},
}
_ => Ok(()),
}

}
}

Expand Down

0 comments on commit 554f104

Please sign in to comment.