From 9becb6e3a7cabc9d4ca25f28c324d44516bbb1f2 Mon Sep 17 00:00:00 2001 From: Adrian Malacoda Date: Mon, 4 Dec 2017 01:22:39 -0600 Subject: [PATCH] add way to supply downloaded file name, expose whether it's cached in return value --- src/lib.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4307f1b..1f515de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -170,21 +170,34 @@ impl Downloader { } } - pub fn get(&self, url: &str) -> PathBuf { + pub fn get(&self, url: &str) -> DownloaderResult { + let filename = url.get((url.rfind('/').expect("not a valid url") + 1..)).expect("failed to parse filename"); + self.get_to(url, filename) + } + + pub fn get_to(&self, url: &str, filename: &str) -> DownloaderResult { if !self.cache.exists() { create_dir_all(self.cache.as_path()).expect("failed to create cache directory"); } - let filename = url.get((url.rfind('/').expect("not a valid url") + 1..)).expect("failed to parse filename"); let mut out_path = self.cache.clone(); out_path.push(filename); - if !out_path.exists() { + let was_cached = out_path.exists(); + if !was_cached { let mut result = reqwest::get(url).expect("failed to download url"); let mut file = File::create(out_path.as_path()).expect("failed to open file"); copy(&mut result, &mut file).expect("failed to write file"); } - out_path + DownloaderResult { + out_path: out_path, + was_cached: was_cached + } } } + +pub struct DownloaderResult { + pub out_path: PathBuf, + pub was_cached: bool +}