2019-08-23 20:33:18 +02:00
|
|
|
use reqwest::header::{CONTENT_TYPE, USER_AGENT};
|
2019-09-22 02:06:00 +02:00
|
|
|
use reqwest::Client;
|
2019-10-23 00:33:22 +02:00
|
|
|
use std::collections::HashMap;
|
2019-08-23 11:08:38 +02:00
|
|
|
use std::time::Duration;
|
2019-09-29 23:15:49 +02:00
|
|
|
use utils::{data_to_dataurl, is_data_url};
|
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-08-23 20:24:45 +02:00
|
|
|
url: &str,
|
|
|
|
as_dataurl: bool,
|
2019-10-01 05:58:09 +02:00
|
|
|
mime: &str,
|
2019-08-23 20:33:18 +02:00
|
|
|
opt_user_agent: &str,
|
2019-08-27 05:17:36 +02:00
|
|
|
opt_silent: bool,
|
|
|
|
opt_insecure: bool,
|
2019-10-01 05:58:09 +02:00
|
|
|
) -> Result<(String, String), reqwest::Error> {
|
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-10-23 00:33:22 +02:00
|
|
|
if cache.contains_key(&url.to_string()) {
|
|
|
|
// url is in cache
|
|
|
|
if !opt_silent {
|
|
|
|
eprintln!("[ {} ] (from cache)", &url);
|
|
|
|
}
|
|
|
|
let data = cache.get(&url.to_string()).unwrap();
|
|
|
|
Ok((data.to_string(), url.to_string()))
|
|
|
|
} else {
|
|
|
|
// url not in cache, we request it
|
|
|
|
let client = Client::builder()
|
|
|
|
.timeout(Duration::from_secs(10))
|
|
|
|
.danger_accept_invalid_certs(opt_insecure)
|
|
|
|
.build()?;
|
|
|
|
let mut response = client.get(url).header(USER_AGENT, opt_user_agent).send()?;
|
2019-08-24 19:33:24 +02:00
|
|
|
|
2019-10-23 00:33:22 +02:00
|
|
|
if !opt_silent {
|
|
|
|
if url == response.url().as_str() {
|
|
|
|
eprintln!("[ {} ]", &url);
|
|
|
|
} else {
|
|
|
|
eprintln!("[ {} -> {} ]", &url, &response.url().as_str());
|
|
|
|
}
|
2019-08-25 17:41:30 +02:00
|
|
|
}
|
2019-08-23 05:17:15 +02:00
|
|
|
|
2019-10-23 00:33:22 +02:00
|
|
|
if as_dataurl {
|
|
|
|
// 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
|
|
|
|
};
|
|
|
|
let dataurl = data_to_dataurl(&mimetype, &data);
|
|
|
|
// insert in cache
|
|
|
|
cache.insert(response.url().to_string(), dataurl.to_string());
|
|
|
|
Ok((dataurl, response.url().to_string()))
|
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
|
|
|
|
cache.insert(response.url().to_string(), content.clone());
|
|
|
|
Ok((content, response.url().to_string()))
|
|
|
|
}
|
2019-08-23 05:17:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|