Initial commit
This commit is contained in:
190
src/api.rs
Normal file
190
src/api.rs
Normal file
@@ -0,0 +1,190 @@
|
||||
use std::{collections::HashMap, iter::repeat, sync::Arc};
|
||||
|
||||
use anyhow::{Context as _, anyhow};
|
||||
use immich_sdk::{AssetId, AssetVisibility};
|
||||
use kameo::{
|
||||
Actor,
|
||||
prelude::{Context, Message},
|
||||
};
|
||||
use slint::{Rgba8Pixel, SharedPixelBuffer};
|
||||
|
||||
use crate::thumbhash::thumbhashes_to_pixels;
|
||||
|
||||
pub type TimeBucketKey = String;
|
||||
|
||||
#[derive(Actor)]
|
||||
pub struct Api {
|
||||
client: immich_sdk::Client,
|
||||
buckets: HashMap<TimeBucketKey, Arc<TimeBucket>>,
|
||||
thumbnails: HashMap<AssetId, Arc<AssetThumbnail>>, // TODO
|
||||
}
|
||||
|
||||
impl Api {
|
||||
pub fn new(client: immich_sdk::Client) -> Self {
|
||||
Self {
|
||||
client,
|
||||
buckets: Default::default(),
|
||||
thumbnails: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GetTimeBuckets;
|
||||
pub struct TimeBucketRef {
|
||||
pub key: TimeBucketKey,
|
||||
pub count: usize,
|
||||
}
|
||||
|
||||
pub struct GetTimeBucket {
|
||||
pub time_bucket: TimeBucketKey,
|
||||
}
|
||||
|
||||
pub struct TimeBucket {
|
||||
pub key: TimeBucketKey,
|
||||
pub entries: Arc<[TimeBucketEntry]>,
|
||||
}
|
||||
|
||||
pub struct TimeBucketEntry {
|
||||
pub id: AssetId,
|
||||
pub is_favorite: bool,
|
||||
pub is_image: bool,
|
||||
pub is_trashed: bool,
|
||||
pub ratio: f64,
|
||||
pub thumbhash: Option<SharedPixelBuffer<Rgba8Pixel>>,
|
||||
pub visibility: AssetVisibility,
|
||||
}
|
||||
|
||||
pub struct GetAssetThumbnail {
|
||||
pub id: AssetId,
|
||||
}
|
||||
|
||||
pub struct AssetThumbnail {
|
||||
pub id: AssetId,
|
||||
pub thumbnail: SharedPixelBuffer<Rgba8Pixel>,
|
||||
}
|
||||
|
||||
impl Message<GetTimeBuckets> for Api {
|
||||
type Reply = anyhow::Result<Arc<[TimeBucketRef]>>;
|
||||
|
||||
async fn handle(
|
||||
&mut self,
|
||||
_msg: GetTimeBuckets,
|
||||
_ctx: &mut Context<Self, Self::Reply>,
|
||||
) -> Self::Reply {
|
||||
let buckets = self
|
||||
.client
|
||||
.timeline()
|
||||
.buckets()
|
||||
.execute()
|
||||
.await
|
||||
.context(anyhow!("Failed to fetch list of time buckets",))
|
||||
.inspect_err(|e| {
|
||||
tracing::error!("{e:?}");
|
||||
})?;
|
||||
|
||||
Ok(buckets
|
||||
.into_iter()
|
||||
.map(|b| TimeBucketRef {
|
||||
key: b.time_bucket,
|
||||
count: b.count.try_into().expect("count is non-negative"),
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
}
|
||||
|
||||
impl Message<GetTimeBucket> for Api {
|
||||
type Reply = anyhow::Result<Arc<TimeBucket>>;
|
||||
|
||||
async fn handle(
|
||||
&mut self,
|
||||
msg: GetTimeBucket,
|
||||
_ctx: &mut Context<Self, Self::Reply>,
|
||||
) -> Self::Reply {
|
||||
if let Some(time_bucket) = self.buckets.get(&msg.time_bucket).cloned() {
|
||||
return Ok(time_bucket);
|
||||
}
|
||||
|
||||
let bucket = self
|
||||
.client
|
||||
.timeline()
|
||||
.bucket(&msg.time_bucket)
|
||||
.execute()
|
||||
.await
|
||||
.context(anyhow!(
|
||||
"Failed to fetch time bucket {:?}",
|
||||
&msg.time_bucket
|
||||
))
|
||||
.inspect_err(|e| {
|
||||
tracing::error!("{e:?}");
|
||||
})?;
|
||||
|
||||
let thumbhashes = bucket.thumbhash.into_iter().flatten();
|
||||
|
||||
let thumbhashes = thumbhashes_to_pixels(thumbhashes)
|
||||
.context("Failed to decode thumbhashes")?
|
||||
.into_iter()
|
||||
.chain(repeat(None));
|
||||
|
||||
let entries = bucket
|
||||
.id
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.zip(thumbhashes)
|
||||
.map(|((i, id), thumbhash)| {
|
||||
TimeBucketEntry {
|
||||
id,
|
||||
is_favorite: bucket.is_favorite.get(i).copied().unwrap_or(false),
|
||||
is_image: bucket.is_image.get(i).copied().unwrap_or(true),
|
||||
is_trashed: bucket.is_trashed.get(i).copied().unwrap_or(false),
|
||||
thumbhash,
|
||||
ratio: 1.0, // TODO
|
||||
visibility: bucket.visibility.get(i).cloned().unwrap(), // TODO: no unwrap
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let bucket = Arc::new(TimeBucket {
|
||||
key: msg.time_bucket,
|
||||
entries,
|
||||
});
|
||||
|
||||
self.buckets.insert(bucket.key.clone(), bucket.clone());
|
||||
|
||||
Ok(bucket)
|
||||
}
|
||||
}
|
||||
|
||||
impl Message<GetAssetThumbnail> for Api {
|
||||
type Reply = anyhow::Result<Arc<AssetThumbnail>>;
|
||||
|
||||
async fn handle(
|
||||
&mut self,
|
||||
msg: GetAssetThumbnail,
|
||||
_ctx: &mut Context<Self, Self::Reply>,
|
||||
) -> Self::Reply {
|
||||
let response = self
|
||||
.client
|
||||
.assets()
|
||||
.thumbnail(msg.id)
|
||||
.size(immich_sdk::AssetMediaSize::Thumbnail)
|
||||
.execute()
|
||||
.await
|
||||
.context(anyhow!("Failed to get asset thumbnail for {}", msg.id))?;
|
||||
|
||||
let thumbnail = response
|
||||
.decode()
|
||||
.context(anyhow!("Failed to decode asset thumbnail for {}", msg.id))?
|
||||
.into_rgba8();
|
||||
|
||||
let pixel_buffer = SharedPixelBuffer::<Rgba8Pixel>::clone_from_slice(
|
||||
&thumbnail,
|
||||
thumbnail.width(),
|
||||
thumbnail.height(),
|
||||
);
|
||||
|
||||
Ok(Arc::new(AssetThumbnail {
|
||||
id: msg.id,
|
||||
thumbnail: pixel_buffer,
|
||||
}))
|
||||
}
|
||||
}
|
||||
218
src/main.rs
Normal file
218
src/main.rs
Normal file
@@ -0,0 +1,218 @@
|
||||
// Prevent console window in addition to Slint window in Windows release builds when, e.g., starting the app via file manager. Ignored on other platforms.
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
use clap::Parser;
|
||||
use immich_sdk::AssetId;
|
||||
use kameo::actor::{ActorRef, Spawn};
|
||||
use slint::{
|
||||
ComponentHandle as _, Model, ModelRc, SharedPixelBuffer, SharedString, ToSharedString,
|
||||
VecModel, Weak,
|
||||
};
|
||||
use tracing::Level;
|
||||
|
||||
use crate::{
|
||||
api::{Api, GetAssetThumbnail, GetTimeBucket, GetTimeBuckets, TimeBucketKey},
|
||||
ui::AppWindow,
|
||||
};
|
||||
|
||||
mod api;
|
||||
mod thumbhash;
|
||||
|
||||
mod ui {
|
||||
slint::include_modules!();
|
||||
}
|
||||
|
||||
#[derive(clap::Parser)]
|
||||
struct Opt {
|
||||
#[clap(long, env = "IMMICH_BASE_URL")]
|
||||
pub immich_base_url: String,
|
||||
|
||||
#[clap(long, env = "IMMICH_API_KEY")]
|
||||
pub immich_api_key: String,
|
||||
}
|
||||
|
||||
// enum ApiReq<M: Send + 'static>
|
||||
// where
|
||||
// Api: Message<M>,
|
||||
// {
|
||||
// AskRequest(
|
||||
// AskRequest<
|
||||
// 'static,
|
||||
// Api,
|
||||
// M,
|
||||
// kameo::request::WithoutRequestTimeout,
|
||||
// kameo::request::WithoutRequestTimeout,
|
||||
// >,
|
||||
// ),
|
||||
// PendingReply(PendingReply<M, <Api as Message<M>>::Reply>),
|
||||
// }
|
||||
|
||||
// enum ApiReqs {
|
||||
// GetBuckets(ApiReq<GetTimeBuckets>),
|
||||
// GetBucket(ApiReq<GetTimeBucket>),
|
||||
// }
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let opt = Opt::parse();
|
||||
|
||||
tracing_subscriber::fmt()
|
||||
.with_max_level(Level::DEBUG)
|
||||
.init();
|
||||
|
||||
let runtime = tokio::runtime::Runtime::new().unwrap();
|
||||
let _rt_guard = runtime.enter();
|
||||
|
||||
let immich_config =
|
||||
immich_sdk::Config::new(opt.immich_base_url).with_api_key(opt.immich_api_key);
|
||||
let api = Api::new(immich_sdk::Client::new(immich_config).unwrap());
|
||||
let api_ = Api::spawn(api);
|
||||
|
||||
let app = ui::AppWindow::new()?;
|
||||
|
||||
let app_weak = app.as_weak();
|
||||
let api = api_.clone();
|
||||
tokio::spawn(async move {
|
||||
let Ok(buckets) = api.ask(GetTimeBuckets).await else {
|
||||
return;
|
||||
};
|
||||
|
||||
for bucket in buckets.iter() {
|
||||
load_bucket(bucket.key.clone(), app_weak.clone(), api.clone());
|
||||
}
|
||||
|
||||
let _ = app_weak.upgrade_in_event_loop(move |app| {
|
||||
let preview_image = slint::Image::from_rgb8(SharedPixelBuffer::clone_from_slice(
|
||||
&[0x33, 0x33, 0x33], // a single dark gray pixel
|
||||
1,
|
||||
1,
|
||||
));
|
||||
|
||||
let global = app.global::<ui::Global>();
|
||||
let buckets = buckets
|
||||
.into_iter()
|
||||
.map(|bucket| ui::ImageBucket {
|
||||
key: bucket.key.to_shared_string(),
|
||||
title: bucket.key.to_shared_string(), // TODO: format
|
||||
count: bucket.count as i32,
|
||||
previews: ModelRc::new(
|
||||
(0..bucket.count)
|
||||
.map(|_| ui::ImagePreview {
|
||||
asset_id: SharedString::new(),
|
||||
image: preview_image.clone(),
|
||||
kind: ui::PreviewKind::None,
|
||||
})
|
||||
.collect::<VecModel<_>>(),
|
||||
),
|
||||
})
|
||||
.collect::<VecModel<_>>();
|
||||
global.set_image_buckets(ModelRc::new(buckets));
|
||||
});
|
||||
});
|
||||
|
||||
// FIXME: visibility-based callbacks are broken
|
||||
// let app_weak = app.as_weak();
|
||||
// let api = api_.clone();
|
||||
// global.on_bucket_view_state(move |key, new_state| {
|
||||
// eprintln!("{key} => {new_state:?}");
|
||||
|
||||
// // TODO
|
||||
// if new_state == ui::ViewState::Hidden {
|
||||
// return;
|
||||
// }
|
||||
// });
|
||||
|
||||
app.run()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn load_bucket(time_bucket: TimeBucketKey, app_weak: Weak<AppWindow>, api: ActorRef<Api>) {
|
||||
tokio::spawn(async move {
|
||||
let Ok(api_bucket) = api
|
||||
.ask(GetTimeBucket {
|
||||
time_bucket: time_bucket.clone(),
|
||||
})
|
||||
.await
|
||||
else {
|
||||
return;
|
||||
};
|
||||
|
||||
let _ = app_weak.upgrade_in_event_loop(move |app| {
|
||||
let global = app.global::<ui::Global>();
|
||||
|
||||
let buckets = global.get_image_buckets();
|
||||
let buckets = buckets
|
||||
.as_any()
|
||||
.downcast_ref::<VecModel<ui::ImageBucket>>()
|
||||
.unwrap();
|
||||
|
||||
let Some(i) = buckets.iter().position(|b| &b.key == &time_bucket) else {
|
||||
return;
|
||||
};
|
||||
let mut bucket = buckets.row_data(i).expect("i is in the list");
|
||||
bucket.previews = ModelRc::new(
|
||||
api_bucket
|
||||
.entries
|
||||
.iter()
|
||||
.map(|entry| ui::ImagePreview {
|
||||
asset_id: entry.id.to_shared_string(), // slint doesn't have a uuid type
|
||||
kind: ui::PreviewKind::Thumbhash,
|
||||
// TODO: don't unwrap
|
||||
image: entry
|
||||
.thumbhash
|
||||
.clone()
|
||||
.map(slint::Image::from_rgba8)
|
||||
.unwrap(),
|
||||
})
|
||||
.collect::<VecModel<_>>(),
|
||||
);
|
||||
buckets.set_row_data(i, bucket);
|
||||
|
||||
for entry in api_bucket.entries.iter() {
|
||||
load_thumbnail(time_bucket.clone(), entry.id, app.as_weak(), api.clone());
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn load_thumbnail(
|
||||
time_bucket: TimeBucketKey,
|
||||
id: AssetId,
|
||||
app_weak: Weak<AppWindow>,
|
||||
api: ActorRef<Api>,
|
||||
) {
|
||||
tokio::spawn(async move {
|
||||
tracing::debug!("Fetching thumbnail for {id}");
|
||||
let thumbnail = match api.ask(GetAssetThumbnail { id }).await {
|
||||
Ok(thumbnail) => thumbnail,
|
||||
Err(e) => {
|
||||
tracing::error!("{e:?}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let _ = app_weak.upgrade_in_event_loop(move |app| {
|
||||
let global = app.global::<ui::Global>();
|
||||
|
||||
let buckets = global.get_image_buckets();
|
||||
let buckets = buckets
|
||||
.as_any()
|
||||
.downcast_ref::<VecModel<ui::ImageBucket>>()
|
||||
.unwrap();
|
||||
|
||||
let Some(i) = buckets.iter().position(|b| &b.key == &time_bucket) else {
|
||||
return;
|
||||
};
|
||||
let bucket = buckets.row_data(i).expect("i is in the list");
|
||||
|
||||
let id_str = id.to_string();
|
||||
let Some(i) = bucket.previews.iter().position(|p| &p.asset_id == &id_str) else {
|
||||
return;
|
||||
};
|
||||
let mut preview = bucket.previews.row_data(i).expect("i is in the list");
|
||||
preview.image = slint::Image::from_rgba8(thumbnail.thumbnail.clone());
|
||||
preview.kind = ui::PreviewKind::Thumbnail;
|
||||
bucket.previews.set_row_data(i, preview);
|
||||
});
|
||||
});
|
||||
}
|
||||
23
src/thumbhash.rs
Normal file
23
src/thumbhash.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use anyhow::anyhow;
|
||||
use base64::{Engine as _, prelude::BASE64_STANDARD};
|
||||
use slint::{Rgba8Pixel, SharedPixelBuffer};
|
||||
|
||||
pub fn thumbhashes_to_pixels(
|
||||
thumbhashes: impl IntoIterator<Item = Option<impl AsRef<str>>>,
|
||||
) -> anyhow::Result<Vec<Option<SharedPixelBuffer<Rgba8Pixel>>>> {
|
||||
thumbhashes
|
||||
.into_iter()
|
||||
.map(|thash| {
|
||||
let Some(thash) = thash else {
|
||||
return Ok(None);
|
||||
};
|
||||
let thash = thash.as_ref();
|
||||
let thash = BASE64_STANDARD.decode(thash)?;
|
||||
let (w, h, preview) = thumbhash::thumb_hash_to_rgba(&thash)
|
||||
.map_err(|e| anyhow!("Invalid thumbhash: {e:?}"))?;
|
||||
Ok(Some(SharedPixelBuffer::clone_from_slice(
|
||||
&preview, w as u32, h as u32,
|
||||
)))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
Reference in New Issue
Block a user