read_url/cache/
blocking.rs1use super::{
2 super::{errors::*, url::*},
3 cache::*,
4};
5
6use {
7 kutil::std::error::*,
8 std::{fs::*, io, sync::*},
9 tracing::*,
10};
11
12impl UrlCache {
13 pub fn file_from(&self, url: &UrlRef, prefix: &str) -> Result<(PathBufRef, bool), UrlError> {
18 let key = url.to_string();
19
20 let mut files = self.files.lock()?;
21 match files.get(&key) {
22 Some(path) => {
23 info!("existing file: {}", path.clone().lock()?.display());
24 Ok((path.clone(), true))
25 }
26
27 None => {
28 let path = self.new_path(prefix)?;
29
30 info!("downloading to file (blocking): {}", path.display());
31 let mut reader = io::BufReader::new(url.open()?);
32 let mut file = io::BufWriter::new(File::create_new(path.clone()).with_path(path.clone())?);
33 io::copy(&mut reader, &mut file)?;
34
35 info!("new file: {}", path.display());
36 let path = Arc::new(Mutex::new(path));
37 files.insert(key, path.clone());
38 Ok((path, false))
39 }
40 }
41 }
42}