use crate::config::CONFIG_INSTANCE; use crate::error::DichroismError; use crate::result::Result; use image::DynamicImage; use std::path::PathBuf; use uuid::Uuid; #[derive(Debug, Queryable, Serialize, Clone)] pub struct Photo { pub path: String, } impl Photo { pub fn from_path(path: String) -> Self { Self { path } } pub fn from_image(image: &DynamicImage) -> Result { let base_name = Uuid::new_v3(&Uuid::NAMESPACE_OID, &image.to_bytes()) .to_hyphenated() .to_string(); let mut path = PathBuf::from(&CONFIG_INSTANCE.img_root); path.push(base_name); path.set_extension("jpg"); image.save(&path)?; Ok(Self { path: path.to_str().ok_or(DichroismError::ImageWrite)?.to_string(), }) } }