profile picture image cache

coding from a plane so this is helping alot with PFPs
This commit is contained in:
William Casarin
2024-02-16 15:42:21 -08:00
parent 1c16ddf9af
commit 87f385b683
8 changed files with 174 additions and 51 deletions

57
src/imgcache.rs Normal file
View File

@@ -0,0 +1,57 @@
use crate::{Error, Result};
use egui::TextureHandle;
use poll_promise::Promise;
use egui::ColorImage;
use hex;
use std::borrow::Cow;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fs::File;
use std::hash::{Hash, Hasher};
use std::io;
use std::path;
use tokio::fs;
pub type ImageCacheValue = Promise<Result<TextureHandle>>;
pub type ImageCacheMap = HashMap<String, ImageCacheValue>;
pub struct ImageCache {
pub cache_dir: path::PathBuf,
url_imgs: ImageCacheMap,
}
impl ImageCache {
pub fn new(cache_dir: path::PathBuf) -> Self {
Self {
cache_dir,
url_imgs: HashMap::new(),
}
}
pub fn write(cache_dir: &path::Path, url: &str, data: ColorImage) -> Result<()> {
let file_path = cache_dir.join(&Self::key(url));
let file = File::options().write(true).create(true).open(file_path)?;
let encoder = image::codecs::webp::WebPEncoder::new_lossless(file);
encoder.encode(
data.as_raw(),
data.size[0] as u32,
data.size[1] as u32,
image::ColorType::Rgba8,
);
Ok(())
}
pub fn key(url: &str) -> String {
base32::encode(base32::Alphabet::Crockford, url.as_bytes())
}
pub fn map(&self) -> &ImageCacheMap {
&self.url_imgs
}
pub fn map_mut(&mut self) -> &mut ImageCacheMap {
&mut self.url_imgs
}
}