use image::DynamicImage;
use tokio::{
fs::{self, File},
io::AsyncReadExt,
};
pub async fn parse_image_url(url_unparsed: &str) -> Result<DynamicImage, anyhow::Error> {
let url = if let Ok(url) = url::Url::parse(url_unparsed) {
url
} else if File::open(url_unparsed).await.is_ok() {
url::Url::from_file_path(std::path::absolute(url_unparsed)?)
.map_err(|_| anyhow::anyhow!("Could not parse file path: {}", url_unparsed))?
} else {
url::Url::parse(&format!("data:image/png;base64,{}", url_unparsed))
.map_err(|_| anyhow::anyhow!("Could not parse as base64 data: {}", url_unparsed))?
};
let bytes = if url.scheme() == "http" || url.scheme() == "https" {
match reqwest::get(url.clone()).await {
Ok(http_resp) => http_resp.bytes().await?.to_vec(),
Err(e) => anyhow::bail!(e),
}
} else if url.scheme() == "file" {
let path = url
.to_file_path()
.map_err(|_| anyhow::anyhow!("Could not parse file path: {}", url))?;
if let Ok(mut f) = File::open(&path).await {
let metadata = fs::metadata(&path).await?;
let mut buffer = vec![0; metadata.len() as usize];
f.read_exact(&mut buffer).await?;
buffer
} else {
anyhow::bail!("Could not open file at path: {}", url);
}
} else if url.scheme() == "data" {
let data_url = data_url::DataUrl::process(url.as_str())?;
data_url.decode_to_vec()?.0
} else {
anyhow::bail!("Unsupported URL scheme: {}", url.scheme());
};
Ok(image::load_from_memory(&bytes)?)
}
#[cfg(test)]
mod tests {
use image::GenericImageView;
use super::*;
#[tokio::test]
async fn test_parse_image_url() {
let url = "https://www.rust-lang.org/logos/rust-logo-32x32.png";
let image = parse_image_url(url).await.unwrap();
assert_eq!(image.dimensions(), (32, 32));
let url = "http://www.rust-lang.org/logos/rust-logo-32x32.png";
let image = parse_image_url(url).await.unwrap();
assert_eq!(image.dimensions(), (32, 32));
let url = "resources/rust-logo-32x32.png";
let image = parse_image_url(url).await.unwrap();
assert_eq!(image.dimensions(), (32, 32));
let absolute_path = std::path::absolute(url).unwrap();
let url = format!("file://{}", absolute_path.as_os_str().to_str().unwrap());
let image = parse_image_url(&url).await.unwrap();
assert_eq!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9+4JKskZg4a3v0
IPussxSdTGNw2jrJD7Zcpmg2iaKr9LrRqMpLWLsE8DrDQ1UXA15HZDppDq5p0Zum6TbUBmq4LJsO
+JEOro6j0xQjyrMzWNCs1/4Y210a21+El0blvdU+NwZCeFGNuQGRe4APgCotFWA+YtwK1d3QiAb/
j9EujBmXKL9U69UscRUncfaJE5Dd112G4RT1hmZpQuYM3+UJRYBoE8QYMA40gmrrKIKGCNaSaMHU
iQfvaeYBQBiG7LTKEgxndI/przbii001lR7HqnVXphmU3EdCFHLjEI1ojKQWyonQI4pGT72Rv2OE
r7qtrAaMYBiy1xpLMClSTk/Nm/6EZtAHUms3y1BKzvCHZESEMVqnXkRyHwjIAxbdLEnsqcBJTILz
xjApU46pnBe8fwF4pb+/8Ywv/DK9zbCKsfWXUBhf+A1dOX00S+xfgc3L3dmKWSn7iklDjthxbSaH
c7YCVIAfi6JYn5bHjTHTGmurQJXJ8C/um928G9zK4gAAAABJRU5ErkJggg==
";
let image = parse_image_url(url).await.unwrap();
assert_eq!(image.dimensions(), (32, 32));
let url = format!("data:image/png;base64,{}", url);
let image = parse_image_url(&url).await.unwrap();
assert_eq!(image.dimensions(), (32, 32));
}
}