use std::{
env,
fs::create_dir,
io::Cursor,
path::{Path, PathBuf},
time::Duration,
};
use anyhow::Result;
use reqwest::header::USER_AGENT;
pub const TAXDMP_URL: &str = "https://ruhr-uni-bochum.sciebo.de/s/dUPkrKiV80137S9/download";
pub async fn get_taxdmp_zip() -> Result<PathBuf> {
if let Some(taxdmp_zip_path) = env::var_os("TAXDMP_ZIP_PATH") {
return Ok(Path::new(taxdmp_zip_path.to_str().unwrap()).to_path_buf());
}
let taxdmp_zip_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("taxdmp_for_unit_tests.zip");
if !taxdmp_zip_path.parent().unwrap().is_dir() {
create_dir(taxdmp_zip_path.parent().unwrap()).unwrap();
}
if taxdmp_zip_path.is_file() {
return Ok(taxdmp_zip_path);
}
let client = reqwest::Client::new();
let request = client
.get(TAXDMP_URL)
.header(
USER_AGENT,
format!(
"Unit test from '{}' here, apologies for the download overhead.",
env!("CARGO_PKG_REPOSITORY")
),
)
.timeout(Duration::from_secs(1200)); let response = request.send().await?;
let mut file = std::fs::File::create(&taxdmp_zip_path)?;
let mut content = Cursor::new(response.bytes().await?);
std::io::copy(&mut content, &mut file)?;
Ok(taxdmp_zip_path)
}