images: make MediaCache hold MediaCacheType

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2025-04-29 11:05:08 -04:00
parent 7f01f3623d
commit d204db4b29
2 changed files with 13 additions and 10 deletions

View File

@@ -13,8 +13,8 @@ use std::time::{Duration, Instant, SystemTime};
use hex::ToHex;
use sha2::Digest;
use std::path::PathBuf;
use std::path::{self};
use std::path::{Path, PathBuf};
use tracing::warn;
pub type MediaCacheValue = Promise<Result<TexturedImage>>;
@@ -223,6 +223,7 @@ pub struct ImageFrame {
pub struct MediaCache {
pub cache_dir: path::PathBuf,
url_imgs: MediaCacheMap,
pub cache_type: MediaCacheType,
}
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
@@ -232,10 +233,12 @@ pub enum MediaCacheType {
}
impl MediaCache {
pub fn new(cache_dir: path::PathBuf) -> Self {
pub fn new(parent_dir: &Path, cache_type: MediaCacheType) -> Self {
let cache_dir = parent_dir.join(Self::rel_dir(cache_type));
Self {
cache_dir,
url_imgs: HashMap::new(),
cache_type,
}
}
@@ -369,8 +372,8 @@ impl Images {
/// path to directory to place [`MediaCache`]s
pub fn new(path: path::PathBuf) -> Self {
Self {
static_imgs: MediaCache::new(path.join(MediaCache::rel_dir(MediaCacheType::Image))),
gifs: MediaCache::new(path.join(MediaCache::rel_dir(MediaCacheType::Gif))),
static_imgs: MediaCache::new(&path, MediaCacheType::Image),
gifs: MediaCache::new(&path, MediaCacheType::Gif),
urls: UrlMimes::new(UrlCache::new(path.join(UrlCache::rel_dir()))),
gif_states: Default::default(),
}

View File

@@ -9,8 +9,8 @@ use notedeck::{
use poll_promise::Promise;
use std::collections::VecDeque;
use std::io::Cursor;
use std::path;
use std::path::PathBuf;
use std::path::{self, Path};
use std::sync::mpsc;
use std::sync::mpsc::SyncSender;
use std::thread;
@@ -356,19 +356,19 @@ pub enum ImageType {
}
pub fn fetch_img(
img_cache: &MediaCache,
img_cache_path: &Path,
ctx: &egui::Context,
url: &str,
imgtyp: ImageType,
cache_type: MediaCacheType,
) -> Promise<Result<TexturedImage, notedeck::Error>> {
let key = MediaCache::key(url);
let path = img_cache.cache_dir.join(key);
let path = img_cache_path.join(key);
if path.exists() {
fetch_img_from_disk(ctx, url, &path, cache_type)
} else {
fetch_img_from_net(&img_cache.cache_dir, ctx, url, imgtyp, cache_type)
fetch_img_from_net(img_cache_path, ctx, url, imgtyp, cache_type)
}
// TODO: fetch image from local cache
@@ -468,7 +468,7 @@ fn render_media_cache(
let m_cached_promise = cache.map().get(url);
if m_cached_promise.is_none() {
let res = crate::images::fetch_img(cache, ui.ctx(), url, img_type, cache_type);
let res = crate::images::fetch_img(&cache.cache_dir, ui.ctx(), url, img_type, cache_type);
cache.map_mut().insert(url.to_owned(), res);
}
@@ -479,7 +479,7 @@ fn render_media_cache(
Some(Err(err)) => {
let err = err.to_string();
let no_pfp = crate::images::fetch_img(
cache,
&cache.cache_dir,
ui.ctx(),
notedeck::profile::no_pfp_url(),
ImageType::Profile(128),