Add image decoding support with image crate

This commit is contained in:
Joakim Hulthe
2026-04-06 08:30:37 +00:00
parent 037f9a5782
commit e5de095ef8
5 changed files with 750 additions and 36 deletions

View File

@@ -1,5 +1,6 @@
//! Assets API - Manage photos and videos
use std::io::Cursor;
use std::path::Path;
use crate::{
@@ -44,6 +45,37 @@ impl ThumbnailResponse {
pub fn into_data(self) -> bytes::Bytes {
self.data
}
/// Decode the image data into a DynamicImage
///
/// # Errors
/// Returns an error if the content type is not supported or if decoding fails
///
/// # Example
/// ```rust,ignore
/// use immich_sdk::Client;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::from_url("https://immich.example.com")?;
/// let asset_id = "your-asset-id".parse()?;
/// let response = client.assets().thumbnail(asset_id).execute().await?;
/// let image = response.decode()?;
/// println!("Image size: {}x{}", image.width(), image.height());
/// # Ok(())
/// # }
/// ```
pub fn decode(&self) -> Result<image::DynamicImage> {
// Get image format from content type
let format = image::ImageFormat::from_mime_type(&self.content_type)
.ok_or_else(|| ImmichError::Image(
format!("Unsupported content type: {}", self.content_type)
))?;
// Create reader with the format and decode
image::ImageReader::with_format(Cursor::new(&self.data), format)
.decode()
.map_err(|e| ImmichError::Image(e.to_string()))
}
}
/// API for managing assets (photos and videos)