Some checks failed
Integration Tests / integration-test (push) Failing after 6s
- Fix AssetUploadStatus serialization to use snake_case (created/duplicate/rejected) - Fix AssetOrder serialization to use lowercase (asc/desc) - Add file_created_at/file_modified_at to UploadAssetBuilder - Fix AddAssetsBuilder response type to Vec<Value> instead of AlbumResponse - Fix ServerAbout model - version is String not ServerVersion struct - Update upload_photos.rs to handle duplicate responses correctly - Update server_info.rs to display new ServerAbout fields - Update AGENTS.md with new example list
84 lines
2.4 KiB
Rust
84 lines
2.4 KiB
Rust
//! Example: Upload photos to Immich
|
|
//!
|
|
//! This example uses environment variables for configuration:
|
|
//! - IMMICH_URL: The Immich server URL (defaults to http://localhost:2283)
|
|
//! - IMMICH_API_KEY: Your API key (required)
|
|
//!
|
|
//! It also reads photos from test-data/sample-photos/ directory.
|
|
|
|
use immich_sdk::Client;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
// Configure the client from environment variables
|
|
let url = std::env::var("IMMICH_URL").unwrap_or_else(|_| "http://localhost:2283".to_string());
|
|
let api_key =
|
|
std::env::var("IMMICH_API_KEY").expect("IMMICH_API_KEY environment variable not set");
|
|
|
|
// Create a client
|
|
let client = Client::from_url(&url)?.with_api_key(api_key);
|
|
|
|
// Use test data photos
|
|
let photo_dir = std::path::Path::new("test-data/sample-photos");
|
|
let photos: Vec<_> = std::fs::read_dir(photo_dir)?
|
|
.filter_map(|e| e.ok())
|
|
.map(|e| e.path())
|
|
.filter(|p| p.extension().map(|e| e == "jpg").unwrap_or(false))
|
|
.collect();
|
|
|
|
if photos.is_empty() {
|
|
println!("No photos found in test-data/sample-photos/");
|
|
return Ok(());
|
|
}
|
|
|
|
// Upload first photo
|
|
let photo_path = &photos[0];
|
|
|
|
// Upload the photo
|
|
println!("Uploading: {}", photo_path.display());
|
|
|
|
let result = client
|
|
.assets()
|
|
.upload()
|
|
.file(photo_path)
|
|
.device_asset_id("my-photo-001")
|
|
.device_id("my-computer")
|
|
.favorite()
|
|
.execute()
|
|
.await?;
|
|
|
|
match result.status {
|
|
immich_sdk::models::AssetUploadStatus::Created => {
|
|
println!("Successfully uploaded! Asset ID: {:?}", result.id);
|
|
}
|
|
immich_sdk::models::AssetUploadStatus::Duplicate => {
|
|
println!("Photo already exists (duplicate)");
|
|
}
|
|
_ => {
|
|
println!("Upload status: {:?}", result.status);
|
|
}
|
|
}
|
|
|
|
// Create an album and add the uploaded asset
|
|
// Note: For duplicates, the ID is returned even though status is duplicate
|
|
if let Some(asset_id) = result.id {
|
|
let album = client
|
|
.albums()
|
|
.create()
|
|
.name("Uploaded Photos")
|
|
.execute()
|
|
.await?;
|
|
|
|
let _add_result = client
|
|
.albums()
|
|
.add_assets(album.id)
|
|
.asset_ids([asset_id])
|
|
.execute()
|
|
.await?;
|
|
|
|
println!("Added to album: {}", album.album_name);
|
|
}
|
|
|
|
Ok(())
|
|
}
|