Skip to content

Commit

Permalink
Merge pull request #1267 from jqnatividad/download_file_refactor
Browse files Browse the repository at this point in the history
Download file helper refactor
  • Loading branch information
jqnatividad authored Aug 31, 2023
2 parents ee49990 + e7e68a8 commit 4a040d8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
21 changes: 9 additions & 12 deletions src/cmd/geocode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,24 +594,21 @@ async fn load_engine(geocode_index_file: PathBuf, progressbar: &ProgressBar) ->

if index_file.exists() {
// load existing local index
if !progressbar.is_hidden() {
progressbar.println(format!(
"Loading existing Geonames index from {}",
index_file.display()
));
}
progressbar.println(format!(
"Loading existing Geonames index from {}",
index_file.display()
));
} else {
// initial load, download index file from qsv releases
if !progressbar.is_hidden() {
progressbar.println(format!(
"Downloading default Geonames index for qsv {QSV_VERSION} release..."
));
}
progressbar.println(format!(
"Downloading default Geonames index for qsv {QSV_VERSION} release..."
));

util::download_file(
&format!(
"https://github.com/jqnatividad/qsv/releases/download/{QSV_VERSION}/qsv-{QSV_VERSION}-geocode-index.bincode"
),
&geocode_index_file.to_string_lossy(),
geocode_index_file.clone(),
!progressbar.is_hidden(),
None,
None,
Expand Down
7 changes: 4 additions & 3 deletions src/cmd/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,18 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
Some(uri) => {
if Url::parse(&uri).is_ok() && uri.starts_with("http") {
// its a remote file, download it first
let temp_download_path = temp_download.path().to_str().unwrap().to_string();

let future = util::download_file(
&uri,
&temp_download_path,
temp_download.path().to_path_buf(),
false,
args.flag_user_agent,
args.flag_timeout,
None,
);
tokio::runtime::Runtime::new()?.block_on(future)?;
// safety: temp_download is a NamedTempFile, so we know it can be converted to a
// string
let temp_download_path = temp_download.path().to_str().unwrap().to_string();
Some(temp_download_path)
} else {
// its a local file
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/snappy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,9 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
Some(uri) => {
let path = if Url::parse(uri).is_ok() && uri.starts_with("http") {
// its a remote file, download it first
let temp_download_path = temp_download.path().to_str().unwrap().to_string();

let future = util::download_file(
uri,
&temp_download_path,
temp_download.path().to_path_buf(),
args.flag_progressbar && !args.cmd_check && !args.flag_quiet,
args.flag_user_agent,
Some(args.flag_timeout),
Expand All @@ -121,6 +119,8 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
},
);
tokio::runtime::Runtime::new()?.block_on(future)?;
// safety: temp_download is a NamedTempFile, so we know that it can be converted
let temp_download_path = temp_download.path().to_str().unwrap().to_string();
temp_download_path
} else {
// its a local file
Expand Down
4 changes: 2 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ pub fn decompress_snappy_file(
/// if sample_size is Some, it will be used as the number of bytes to download
pub async fn download_file(
url: &str,
path: &str,
path: PathBuf,
show_progress: bool,
custom_user_agent: Option<String>,
download_timeout: Option<u16>,
Expand Down Expand Up @@ -1368,7 +1368,7 @@ pub async fn download_file(
let sample_size = sample_size.unwrap_or(0);

// download chunks
let mut file = File::create(path).map_err(|_| format!("Failed to create file '{path}'"))?;
let mut file = File::create(path)?;
let mut downloaded: u64 = 0;
let mut stream = res.bytes_stream();

Expand Down

0 comments on commit 4a040d8

Please sign in to comment.