2020-02-12 07:51:26 +01:00
|
|
|
use crate::utils::{clean_url, data_to_data_url, is_data_url};
|
2020-01-07 05:22:28 +01:00
|
|
|
use reqwest::blocking::Client;
|
2019-12-10 03:13:25 +01:00
|
|
|
use reqwest::header::CONTENT_TYPE;
|
2019-10-23 00:33:22 +02:00
|
|
|
use std::collections::HashMap;
|
2019-08-23 05:17:15 +02:00
|
|
|
|
2019-08-23 20:24:45 +02:00
|
|
|
pub fn retrieve_asset(
|
2019-10-23 00:33:22 +02:00
|
|
|
cache: &mut HashMap<String, String>,
|
2019-12-10 03:13:25 +01:00
|
|
|
client: &Client,
|
2019-08-23 20:24:45 +02:00
|
|
|
url: &str,
|
2020-02-12 07:51:26 +01:00
|
|
|
as_data_url: bool,
|
2019-10-01 05:58:09 +02:00
|
|
|
mime: &str,
|
2019-08-27 05:17:36 +02:00
|
|
|
opt_silent: bool,
|
2019-10-01 05:58:09 +02:00
|
|
|
) -> Result<(String, String), reqwest::Error> {
|
2019-12-12 03:13:11 +01:00
|
|
|
let cache_key = clean_url(&url);
|
|
|
|
|
2019-08-24 05:06:06 +02:00
|
|
|
if is_data_url(&url).unwrap() {
|
2019-10-01 05:58:09 +02:00
|
|
|
Ok((url.to_string(), url.to_string()))
|
2019-08-23 05:17:15 +02:00
|
|
|
} else {
|
2019-12-12 03:13:11 +01:00
|
|
|
if cache.contains_key(&cache_key) {
|
2019-10-23 00:33:22 +02:00
|
|
|
// url is in cache
|
|
|
|
if !opt_silent {
|
2019-12-11 07:17:00 +01:00
|
|
|
eprintln!("{} (from cache)", &url);
|
2019-10-23 00:33:22 +02:00
|
|
|
}
|
2019-12-12 03:13:11 +01:00
|
|
|
let data = cache.get(&cache_key).unwrap();
|
2019-10-23 00:33:22 +02:00
|
|
|
Ok((data.to_string(), url.to_string()))
|
|
|
|
} else {
|
|
|
|
// url not in cache, we request it
|
2019-12-10 03:13:25 +01:00
|
|
|
let mut response = client.get(url).send()?;
|
2020-01-07 05:22:28 +01:00
|
|
|
let res_url = response.url().to_string();
|
2019-08-24 19:33:24 +02:00
|
|
|
|
2019-10-23 00:33:22 +02:00
|
|
|
if !opt_silent {
|
2020-01-03 23:49:26 +01:00
|
|
|
if url == res_url {
|
2019-12-11 07:17:00 +01:00
|
|
|
eprintln!("{}", &url);
|
2019-10-23 00:33:22 +02:00
|
|
|
} else {
|
2020-01-07 05:22:28 +01:00
|
|
|
eprintln!("{} -> {}", &url, &res_url);
|
2019-10-23 00:33:22 +02:00
|
|
|
}
|
2019-08-25 17:41:30 +02:00
|
|
|
}
|
2019-08-23 05:17:15 +02:00
|
|
|
|
2020-01-07 05:22:28 +01:00
|
|
|
let new_cache_key = clean_url(&res_url);
|
2019-12-12 03:13:11 +01:00
|
|
|
|
2020-02-12 07:51:26 +01:00
|
|
|
if as_data_url {
|
2019-10-23 00:33:22 +02:00
|
|
|
// Convert response into a byte array
|
|
|
|
let mut data: Vec<u8> = vec![];
|
|
|
|
response.copy_to(&mut data)?;
|
2019-08-23 05:17:15 +02:00
|
|
|
|
2019-10-23 00:33:22 +02:00
|
|
|
// Attempt to obtain MIME type by reading the Content-Type header
|
|
|
|
let mimetype = if mime == "" {
|
|
|
|
response
|
|
|
|
.headers()
|
|
|
|
.get(CONTENT_TYPE)
|
|
|
|
.and_then(|header| header.to_str().ok())
|
|
|
|
.unwrap_or(&mime)
|
|
|
|
} else {
|
|
|
|
mime
|
|
|
|
};
|
2020-02-12 07:51:26 +01:00
|
|
|
let data_url = data_to_data_url(&mimetype, &data);
|
2019-10-23 00:33:22 +02:00
|
|
|
// insert in cache
|
2020-02-12 07:51:26 +01:00
|
|
|
cache.insert(new_cache_key, data_url.clone());
|
|
|
|
Ok((data_url, res_url))
|
2019-08-23 20:24:45 +02:00
|
|
|
} else {
|
2019-10-23 00:33:22 +02:00
|
|
|
let content = response.text().unwrap();
|
|
|
|
// insert in cache
|
2019-12-12 03:13:11 +01:00
|
|
|
cache.insert(new_cache_key, content.clone());
|
2020-01-07 05:22:28 +01:00
|
|
|
Ok((content, res_url))
|
2019-10-23 00:33:22 +02:00
|
|
|
}
|
2019-08-23 05:17:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|