immich-tools/src/actions/create_album.rs

68 lines
1.5 KiB
Rust

use chrono::Utc;
use color_eyre::eyre::Result;
use log::info;
use uuid::Uuid;
use crate::{
context::Context,
models::{album::Album, asset::Asset},
types::CreateAlbumDto,
};
use super::action::Action;
pub struct CreateAlbum {
name: String,
assets: Vec<Asset>,
}
#[derive(Debug, Default)]
pub struct CreateAlbumArgs {
pub name: String,
pub assets: Vec<Asset>,
}
impl Action for CreateAlbum {
type Input = CreateAlbumArgs;
type Output = Album;
fn new(input: Self::Input) -> Self {
Self {
name: input.name,
assets: input.assets,
}
}
fn describe(&self) -> String {
format!(
"Creating album {} with {} assets",
self.name,
self.assets.len()
)
}
async fn execute(&self, ctx: &Context) -> Result<Self::Output> {
info!("{}", self.describe());
if !ctx.dry_run {
Ok(ctx
.client
.create_album(&CreateAlbumDto {
album_name: self.name.clone(),
asset_ids: self.assets.iter().map(|asset| asset.id).collect(),
album_users: vec![],
description: None,
})
.await?
.clone()
.into())
} else {
Ok(Album {
id: Uuid::new_v4(),
name: self.name.clone(),
assets_count: self.assets.len() as u32,
updated_at: Utc::now(),
})
}
}
}