Download images when clicking on them
This commit is contained in:
55
src/api.rs
55
src/api.rs
@@ -24,6 +24,7 @@ pub struct Api {
|
||||
client: immich_sdk::Client,
|
||||
buckets: Mutex<HashMap<TimeBucketKey, Arc<TimeBucket>>>,
|
||||
thumbnails: Mutex<CacheMap<AssetId, Arc<AssetThumbnail>>>,
|
||||
assets: Mutex<CacheMap<AssetId, Arc<AssetThumbnail>>>,
|
||||
}
|
||||
|
||||
fn serialize_thumbnail(_id: &AssetId, thumbnail: &Arc<AssetThumbnail>) -> Vec<u8> {
|
||||
@@ -61,12 +62,50 @@ fn deserialize_thumbnail(&id: &AssetId, bytes: &[u8]) -> anyhow::Result<Arc<Asse
|
||||
|
||||
impl Api {
|
||||
pub fn new(client: immich_sdk::Client) -> Arc<Self> {
|
||||
let client_ = client.clone();
|
||||
let asset_fetcher = AsyncFnFetcher::new(move |asset_id: &AssetId| {
|
||||
let client = client_.clone();
|
||||
let asset_id = asset_id.clone();
|
||||
tokio::spawn(async move {
|
||||
let asset = client
|
||||
.assets()
|
||||
.download(asset_id)
|
||||
.edited()
|
||||
.execute()
|
||||
.await
|
||||
.context(anyhow!("Failed to get asset {asset_id}"))?;
|
||||
|
||||
let asset = asset
|
||||
.decode()
|
||||
.context(anyhow!("Failed to decode asset {asset_id}"))?
|
||||
.into_rgba8();
|
||||
|
||||
let pixel_buffer = SharedPixelBuffer::<Rgba8Pixel>::clone_from_slice(
|
||||
&asset,
|
||||
asset.width(),
|
||||
asset.height(),
|
||||
);
|
||||
|
||||
Ok(Arc::new(AssetThumbnail {
|
||||
id: asset_id,
|
||||
thumbnail: pixel_buffer,
|
||||
}))
|
||||
})
|
||||
});
|
||||
let asset_map = CacheMap::new(
|
||||
Arc::new(asset_fetcher) as Arc<dyn Fetcher<Arc<AssetThumbnail>, Key = AssetId>>,
|
||||
"assets",
|
||||
serialize_thumbnail,
|
||||
deserialize_thumbnail,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
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
|
||||
let asset = client
|
||||
.assets()
|
||||
.thumbnail(asset_id)
|
||||
.size(immich_sdk::AssetMediaSize::Thumbnail)
|
||||
@@ -74,7 +113,7 @@ impl Api {
|
||||
.await
|
||||
.context(anyhow!("Failed to get asset thumbnail for {asset_id}"))?;
|
||||
|
||||
let thumbnail = response
|
||||
let thumbnail = asset
|
||||
.decode()
|
||||
.context(anyhow!("Failed to decode asset thumbnail for {asset_id}"))?
|
||||
.into_rgba8();
|
||||
@@ -91,7 +130,7 @@ impl Api {
|
||||
}))
|
||||
})
|
||||
});
|
||||
let cache_map = CacheMap::new(
|
||||
let thumbnail_map = CacheMap::new(
|
||||
Arc::new(thumbnail_fetcher) as Arc<dyn Fetcher<Arc<AssetThumbnail>, Key = AssetId>>,
|
||||
"thumbnails",
|
||||
serialize_thumbnail,
|
||||
@@ -102,7 +141,8 @@ impl Api {
|
||||
Arc::new(Self {
|
||||
client,
|
||||
buckets: Default::default(),
|
||||
thumbnails: Mutex::new(cache_map),
|
||||
thumbnails: Mutex::new(thumbnail_map),
|
||||
assets: Mutex::new(asset_map),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -220,4 +260,11 @@ impl Api {
|
||||
.await
|
||||
.context(anyhow!("Failed to fetch thumbnail for {asset_id:?}"))
|
||||
}
|
||||
|
||||
pub async fn get_asset(&self, asset_id: AssetId) -> anyhow::Result<Arc<AssetThumbnail>> {
|
||||
let fetch = self.assets.lock().unwrap().get(asset_id);
|
||||
fetch
|
||||
.await
|
||||
.context(anyhow!("Failed to fetch asset {asset_id:?}"))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user