Cache thumbnails to disk
This commit is contained in:
122
src/api.rs
122
src/api.rs
@@ -1,29 +1,108 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
io::Cursor,
|
||||
iter::repeat,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use anyhow::{Context as _, anyhow};
|
||||
use image::{
|
||||
DynamicImage, EncodableLayout,
|
||||
codecs::webp::{WebPDecoder, WebPEncoder},
|
||||
};
|
||||
use immich_sdk::{AssetId, AssetVisibility};
|
||||
use slint::{Rgba8Pixel, SharedPixelBuffer};
|
||||
|
||||
use crate::thumbhash::thumbhashes_to_pixels;
|
||||
use crate::{
|
||||
cachemap::{AsyncFnFetcher, CacheMap, Fetcher},
|
||||
thumbhash::thumbhashes_to_pixels,
|
||||
};
|
||||
|
||||
pub type TimeBucketKey = String;
|
||||
|
||||
pub struct Api {
|
||||
client: immich_sdk::Client,
|
||||
buckets: Mutex<HashMap<TimeBucketKey, Arc<TimeBucket>>>,
|
||||
thumbnails: Mutex<HashMap<AssetId, Arc<AssetThumbnail>>>,
|
||||
thumbnails: Mutex<CacheMap<AssetId, Arc<AssetThumbnail>>>,
|
||||
}
|
||||
|
||||
fn serialize_thumbnail(_id: &AssetId, thumbnail: &Arc<AssetThumbnail>) -> Vec<u8> {
|
||||
let image: &SharedPixelBuffer<Rgba8Pixel> = &thumbnail.thumbnail;
|
||||
|
||||
let mut webp = vec![];
|
||||
WebPEncoder::new_lossless(&mut webp)
|
||||
.encode(
|
||||
image.as_bytes(),
|
||||
image.width(),
|
||||
image.height(),
|
||||
image::ExtendedColorType::Rgba8,
|
||||
)
|
||||
.expect("width and height matches image.as_bytes().len()");
|
||||
|
||||
webp
|
||||
}
|
||||
fn deserialize_thumbnail(&id: &AssetId, bytes: &[u8]) -> anyhow::Result<Arc<AssetThumbnail>> {
|
||||
let image = WebPDecoder::new(Cursor::new(bytes))
|
||||
.and_then(DynamicImage::from_decoder)
|
||||
.context("Failed to decode image")?
|
||||
.to_rgba8();
|
||||
|
||||
let image = SharedPixelBuffer::<Rgba8Pixel>::clone_from_slice(
|
||||
image.as_bytes(),
|
||||
image.width(),
|
||||
image.height(),
|
||||
);
|
||||
|
||||
Ok(Arc::new(AssetThumbnail {
|
||||
id,
|
||||
thumbnail: image,
|
||||
}))
|
||||
}
|
||||
|
||||
impl Api {
|
||||
pub fn new(client: immich_sdk::Client) -> Arc<Self> {
|
||||
let client_ = client.clone();
|
||||
let thumbnail_fetcher = AsyncFnFetcher::new(move |asset_id: &AssetId| {
|
||||
let client = client_.clone();
|
||||
let asset_id = asset_id.clone();
|
||||
tokio::spawn(async move {
|
||||
let response = client
|
||||
.assets()
|
||||
.thumbnail(asset_id)
|
||||
.size(immich_sdk::AssetMediaSize::Thumbnail)
|
||||
.execute()
|
||||
.await
|
||||
.context(anyhow!("Failed to get asset thumbnail for {asset_id}"))?;
|
||||
|
||||
let thumbnail = response
|
||||
.decode()
|
||||
.context(anyhow!("Failed to decode asset thumbnail for {asset_id}"))?
|
||||
.into_rgba8();
|
||||
|
||||
let pixel_buffer = SharedPixelBuffer::<Rgba8Pixel>::clone_from_slice(
|
||||
&thumbnail,
|
||||
thumbnail.width(),
|
||||
thumbnail.height(),
|
||||
);
|
||||
|
||||
Ok(Arc::new(AssetThumbnail {
|
||||
id: asset_id,
|
||||
thumbnail: pixel_buffer,
|
||||
}))
|
||||
})
|
||||
});
|
||||
let cache_map = CacheMap::new(
|
||||
Arc::new(thumbnail_fetcher) as Arc<dyn Fetcher<Arc<AssetThumbnail>, Key = AssetId>>,
|
||||
"thumbnails",
|
||||
serialize_thumbnail,
|
||||
deserialize_thumbnail,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
Arc::new(Self {
|
||||
client,
|
||||
buckets: Default::default(),
|
||||
thumbnails: Default::default(),
|
||||
thumbnails: Mutex::new(cache_map),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -136,40 +215,9 @@ impl Api {
|
||||
&self,
|
||||
asset_id: AssetId,
|
||||
) -> anyhow::Result<Arc<AssetThumbnail>> {
|
||||
if let Some(thumbnail) = self.thumbnails.lock().unwrap().get(&asset_id).cloned() {
|
||||
return Ok(thumbnail);
|
||||
}
|
||||
|
||||
let response = self
|
||||
.client
|
||||
.assets()
|
||||
.thumbnail(asset_id)
|
||||
.size(immich_sdk::AssetMediaSize::Thumbnail)
|
||||
.execute()
|
||||
let fetch = self.thumbnails.lock().unwrap().get(asset_id);
|
||||
fetch
|
||||
.await
|
||||
.context(anyhow!("Failed to get asset thumbnail for {asset_id}"))?;
|
||||
|
||||
let thumbnail = response
|
||||
.decode()
|
||||
.context(anyhow!("Failed to decode asset thumbnail for {asset_id}"))?
|
||||
.into_rgba8();
|
||||
|
||||
let pixel_buffer = SharedPixelBuffer::<Rgba8Pixel>::clone_from_slice(
|
||||
&thumbnail,
|
||||
thumbnail.width(),
|
||||
thumbnail.height(),
|
||||
);
|
||||
|
||||
let thumbnail = Arc::new(AssetThumbnail {
|
||||
id: asset_id,
|
||||
thumbnail: pixel_buffer,
|
||||
});
|
||||
|
||||
self.thumbnails
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(asset_id, Arc::clone(&thumbnail));
|
||||
|
||||
Ok(thumbnail)
|
||||
.context(anyhow!("Failed to fetch thumbnail for {asset_id:?}"))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user