//! Assets API - Manage photos and videos use std::path::Path; use crate::{ Client, error::{ImmichError, Result}, models::{AssetId, AssetResponse, AssetUploadResponse, DeleteAssetsRequest}, }; /// API for managing assets (photos and videos) #[derive(Debug, Clone)] pub struct AssetsApi { client: Client, } impl AssetsApi { /// Create a new assets API instance pub const fn new(client: Client) -> Self { Self { client } } /// List assets with optional filters pub fn list(&self) -> ListAssetsBuilder { ListAssetsBuilder::new(self.client.clone()) } /// Get a single asset by ID pub fn get(&self, id: AssetId) -> GetAssetBuilder { GetAssetBuilder::new(self.client.clone(), id) } /// Upload a new asset pub fn upload(&self) -> UploadAssetBuilder { UploadAssetBuilder::new(self.client.clone()) } /// Delete assets pub fn delete(&self) -> DeleteAssetsBuilder { DeleteAssetsBuilder::new(self.client.clone()) } } /// Builder for listing assets #[derive(Debug)] pub struct ListAssetsBuilder { client: Client, album_id: Option, is_favorite: Option, is_trashed: Option, } impl ListAssetsBuilder { /// Create a new list builder const fn new(client: Client) -> Self { Self { client, album_id: None, is_favorite: None, is_trashed: None, } } /// Filter by album ID pub fn with_album_id(mut self, album_id: AssetId) -> Self { self.album_id = Some(album_id); self } /// Filter by favorite status pub fn is_favorite(mut self, favorite: bool) -> Self { self.is_favorite = Some(favorite); self } /// Filter by trash status pub fn is_trashed(mut self, trashed: bool) -> Self { self.is_trashed = Some(trashed); self } /// Execute the request pub async fn execute(self) -> Result> { let mut req = self.client.get("/assets"); if let Some(album_id) = self.album_id { req = req.query(&[("albumId", album_id.to_string())]); } if let Some(is_favorite) = self.is_favorite { req = req.query(&[("isFavorite", is_favorite.to_string())]); } if let Some(is_trashed) = self.is_trashed { req = req.query(&[("isTrashed", is_trashed.to_string())]); } let response = self.client.execute(req.build()?).await?; let assets: Vec = response.json().await?; Ok(assets) } } /// Builder for getting a single asset #[derive(Debug)] pub struct GetAssetBuilder { client: Client, id: AssetId, } impl GetAssetBuilder { /// Create a new get builder const fn new(client: Client, id: AssetId) -> Self { Self { client, id } } /// Execute the request pub async fn execute(self) -> Result { let path = format!("/assets/{}", self.id); let req = self.client.get(&path); let response = self.client.execute(req.build()?).await?; let asset: AssetResponse = response.json().await?; Ok(asset) } } /// Builder for uploading an asset #[derive(Debug)] pub struct UploadAssetBuilder { client: Client, file_path: Option, device_asset_id: Option, device_id: Option, is_favorite: bool, } impl UploadAssetBuilder { /// Create a new upload builder const fn new(client: Client) -> Self { Self { client, file_path: None, device_asset_id: None, device_id: None, is_favorite: false, } } /// Set the file path to upload pub fn file(mut self, path: impl AsRef) -> Self { self.file_path = Some(path.as_ref().to_string_lossy().to_string()); self } /// Set the device asset ID (required) pub fn device_asset_id(mut self, id: impl Into) -> Self { self.device_asset_id = Some(id.into()); self } /// Set the device ID (required) pub fn device_id(mut self, id: impl Into) -> Self { self.device_id = Some(id.into()); self } /// Mark as favorite pub fn favorite(mut self) -> Self { self.is_favorite = true; self } /// Execute the upload pub async fn execute(self) -> Result { let file_path = self .file_path .ok_or_else(|| ImmichError::Validation("File path is required".to_string()))?; // Read file let file_content = tokio::fs::read(&file_path).await?; let file_name = std::path::Path::new(&file_path) .file_name() .ok_or_else(|| ImmichError::Validation("Invalid file path".to_string()))? .to_string_lossy(); // Build multipart form let mut form = reqwest::multipart::Form::new().part( "assetData", reqwest::multipart::Part::bytes(file_content).file_name(file_name.to_string()), ); if let Some(device_asset_id) = self.device_asset_id { form = form.text("deviceAssetId", device_asset_id); } if let Some(device_id) = self.device_id { form = form.text("deviceId", device_id); } form = form.text("isFavorite", self.is_favorite.to_string()); let req = self.client.post("/assets").multipart(form); let response = self.client.execute(req.build()?).await?; let result: AssetUploadResponse = response.json().await?; Ok(result) } } /// Builder for deleting assets #[derive(Debug)] pub struct DeleteAssetsBuilder { client: Client, ids: Vec, } impl DeleteAssetsBuilder { /// Create a new delete builder const fn new(client: Client) -> Self { Self { client, ids: Vec::new(), } } /// Add an asset ID to delete pub fn id(mut self, id: AssetId) -> Self { self.ids.push(id); self } /// Add multiple asset IDs to delete pub fn ids(mut self, ids: impl IntoIterator) -> Self { self.ids.extend(ids); self } /// Execute the deletion pub async fn execute(self) -> Result<()> { if self.ids.is_empty() { return Err(ImmichError::Validation( "At least one asset ID is required".to_string(), )); } let body = DeleteAssetsRequest { ids: self.ids, force: false, }; let req = self.client.delete("/assets").json(&body); let _response = self.client.execute(req.build()?).await?; Ok(()) } }