images: make MediaCache hold MediaCacheType
Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
@@ -13,8 +13,8 @@ use std::time::{Duration, Instant, SystemTime};
|
|||||||
|
|
||||||
use hex::ToHex;
|
use hex::ToHex;
|
||||||
use sha2::Digest;
|
use sha2::Digest;
|
||||||
use std::path::PathBuf;
|
|
||||||
use std::path::{self};
|
use std::path::{self};
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
use tracing::warn;
|
use tracing::warn;
|
||||||
|
|
||||||
pub type MediaCacheValue = Promise<Result<TexturedImage>>;
|
pub type MediaCacheValue = Promise<Result<TexturedImage>>;
|
||||||
@@ -223,6 +223,7 @@ pub struct ImageFrame {
|
|||||||
pub struct MediaCache {
|
pub struct MediaCache {
|
||||||
pub cache_dir: path::PathBuf,
|
pub cache_dir: path::PathBuf,
|
||||||
url_imgs: MediaCacheMap,
|
url_imgs: MediaCacheMap,
|
||||||
|
pub cache_type: MediaCacheType,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
|
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
|
||||||
@@ -232,10 +233,12 @@ pub enum MediaCacheType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl MediaCache {
|
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 {
|
Self {
|
||||||
cache_dir,
|
cache_dir,
|
||||||
url_imgs: HashMap::new(),
|
url_imgs: HashMap::new(),
|
||||||
|
cache_type,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -369,8 +372,8 @@ impl Images {
|
|||||||
/// path to directory to place [`MediaCache`]s
|
/// path to directory to place [`MediaCache`]s
|
||||||
pub fn new(path: path::PathBuf) -> Self {
|
pub fn new(path: path::PathBuf) -> Self {
|
||||||
Self {
|
Self {
|
||||||
static_imgs: MediaCache::new(path.join(MediaCache::rel_dir(MediaCacheType::Image))),
|
static_imgs: MediaCache::new(&path, MediaCacheType::Image),
|
||||||
gifs: MediaCache::new(path.join(MediaCache::rel_dir(MediaCacheType::Gif))),
|
gifs: MediaCache::new(&path, MediaCacheType::Gif),
|
||||||
urls: UrlMimes::new(UrlCache::new(path.join(UrlCache::rel_dir()))),
|
urls: UrlMimes::new(UrlCache::new(path.join(UrlCache::rel_dir()))),
|
||||||
gif_states: Default::default(),
|
gif_states: Default::default(),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ use notedeck::{
|
|||||||
use poll_promise::Promise;
|
use poll_promise::Promise;
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
use std::path;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use std::path::{self, Path};
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::sync::mpsc::SyncSender;
|
use std::sync::mpsc::SyncSender;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
@@ -356,19 +356,19 @@ pub enum ImageType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn fetch_img(
|
pub fn fetch_img(
|
||||||
img_cache: &MediaCache,
|
img_cache_path: &Path,
|
||||||
ctx: &egui::Context,
|
ctx: &egui::Context,
|
||||||
url: &str,
|
url: &str,
|
||||||
imgtyp: ImageType,
|
imgtyp: ImageType,
|
||||||
cache_type: MediaCacheType,
|
cache_type: MediaCacheType,
|
||||||
) -> Promise<Result<TexturedImage, notedeck::Error>> {
|
) -> Promise<Result<TexturedImage, notedeck::Error>> {
|
||||||
let key = MediaCache::key(url);
|
let key = MediaCache::key(url);
|
||||||
let path = img_cache.cache_dir.join(key);
|
let path = img_cache_path.join(key);
|
||||||
|
|
||||||
if path.exists() {
|
if path.exists() {
|
||||||
fetch_img_from_disk(ctx, url, &path, cache_type)
|
fetch_img_from_disk(ctx, url, &path, cache_type)
|
||||||
} else {
|
} 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
|
// TODO: fetch image from local cache
|
||||||
@@ -468,7 +468,7 @@ fn render_media_cache(
|
|||||||
let m_cached_promise = cache.map().get(url);
|
let m_cached_promise = cache.map().get(url);
|
||||||
|
|
||||||
if m_cached_promise.is_none() {
|
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);
|
cache.map_mut().insert(url.to_owned(), res);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -479,7 +479,7 @@ fn render_media_cache(
|
|||||||
Some(Err(err)) => {
|
Some(Err(err)) => {
|
||||||
let err = err.to_string();
|
let err = err.to_string();
|
||||||
let no_pfp = crate::images::fetch_img(
|
let no_pfp = crate::images::fetch_img(
|
||||||
cache,
|
&cache.cache_dir,
|
||||||
ui.ctx(),
|
ui.ctx(),
|
||||||
notedeck::profile::no_pfp_url(),
|
notedeck::profile::no_pfp_url(),
|
||||||
ImageType::Profile(128),
|
ImageType::Profile(128),
|
||||||
|
|||||||
Reference in New Issue
Block a user