arfur_build/
file.rs

1use std::{io::Cursor, path::PathBuf};
2
3use tracing::trace;
4
5pub struct DownloadableFile<'a> {
6    url: &'a str,
7}
8
9impl<'a> DownloadableFile<'a> {
10    pub fn new(url: &'a str) -> Self {
11        Self { url }
12    }
13
14    pub async fn get(&self) -> PathBuf {
15        let path = PathBuf::new()
16            .join("/tmp/arfur/")
17            .join(seahash::hash(self.url.as_bytes()).to_string());
18
19        if path.exists() {
20            // We already have it cached.
21            trace!("{path:?} was already cached.");
22            return path;
23        } else {
24            // We don't have this path. Download it and save it.
25            trace!("{path:?} was not found, downloading...");
26
27            let zipped = reqwest::get(self.url).await.unwrap().bytes().await.unwrap();
28            let _unzipped = zip_extract::extract(Cursor::new(zipped), &path, false).unwrap();
29
30            trace!("Extracted {path:?} succesfully.");
31
32            return path;
33        }
34    }
35}