Downloads now use temp folders.
This commit is contained in:
parent
2339b4db45
commit
23fb297200
@ -47,37 +47,37 @@ fn download_into(dir: &str, file_title: &str, url: &str) -> Result<String> {
|
|||||||
"unkown"
|
"unkown"
|
||||||
};
|
};
|
||||||
|
|
||||||
// Construct the download path.
|
// Construct a temp file to save desired content.
|
||||||
let filename = format!("{}.{}", file_title, ext);
|
let tempdir = TempDir::new_in(dir, "")?;
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
// TODO: do a mime-type check after the file is downloaded to be sure.
|
let out_file = format!(
|
||||||
return save_io(dir, &filename, &mut resp, ct_len);
|
"{}/{}.part",
|
||||||
|
tempdir.path().to_str().unwrap(),
|
||||||
|
rng.gen::<usize>()
|
||||||
|
);
|
||||||
|
|
||||||
|
save_io(&out_file, &mut resp, ct_len)?;
|
||||||
|
|
||||||
|
// Construct the desired path.
|
||||||
|
let target = format!("{}/{}.{}", dir, file_title, ext);
|
||||||
|
// Rename/move the tempfile into a permanent place.
|
||||||
|
rename(out_file, &target)?;
|
||||||
|
info!("Downloading of {} completed succesfully.", &target);
|
||||||
|
return Ok(target);
|
||||||
}
|
}
|
||||||
// Ok(String::from(""))
|
// Ok(String::from(""))
|
||||||
panic!("Bad request response.");
|
panic!("Bad request response.");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn save_io(
|
fn save_io(file: &str, resp: &mut reqwest::Response, content_lenght: Option<u64>) -> Result<()> {
|
||||||
target_dir: &str,
|
info!("Downloading into: {}", file);
|
||||||
filename: &str,
|
|
||||||
resp: &mut reqwest::Response,
|
|
||||||
content_lenght: Option<u64>,
|
|
||||||
) -> Result<String> {
|
|
||||||
info!("Downloading into: {}", target_dir);
|
|
||||||
let chunk_size = match content_lenght {
|
let chunk_size = match content_lenght {
|
||||||
Some(x) => x as usize / 99,
|
Some(x) => x as usize / 99,
|
||||||
None => 1024 as usize, // default chunk size
|
None => 1024 as usize, // default chunk size
|
||||||
};
|
};
|
||||||
|
|
||||||
let tempdir = TempDir::new(target_dir)?;
|
let mut writer = BufWriter::new(File::create(&file)?);
|
||||||
let mut rng = rand::thread_rng();
|
|
||||||
|
|
||||||
let out_file = format!(
|
|
||||||
"{}/{}.part",
|
|
||||||
tempdir.path().to_str().unwrap(),
|
|
||||||
rng.gen::<usize>()
|
|
||||||
);
|
|
||||||
let mut writer = BufWriter::new(File::create(&out_file)?);
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let mut buffer = vec![0; chunk_size];
|
let mut buffer = vec![0; chunk_size];
|
||||||
@ -90,10 +90,7 @@ fn save_io(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let target = format!("{}/{}", target_dir, filename);
|
Ok(())
|
||||||
rename(out_file, &target)?;
|
|
||||||
info!("Downloading of {} completed succesfully.", &target);
|
|
||||||
Ok(target)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_download_folder(pd_title: &str) -> Result<String> {
|
pub fn get_download_folder(pd_title: &str) -> Result<String> {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user