Add image decoding support with image crate
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -50,6 +50,10 @@ pub enum ImmichError {
|
||||
#[error("File error: {0}")]
|
||||
File(#[from] std::io::Error),
|
||||
|
||||
/// Image decoding error
|
||||
#[error("Image decoding error: {0}")]
|
||||
Image(String),
|
||||
|
||||
/// Unknown error
|
||||
#[error("Unknown error: {0}")]
|
||||
Unknown(String),
|
||||
|
||||
Reference in New Issue
Block a user