summaryrefslogtreecommitdiff
path: root/dichroism/src/models/photo.rs
diff options
context:
space:
mode:
authorAdam T. Carpenter <atc@53hor.net>2020-10-22 17:44:38 -0400
committerAdam T. Carpenter <atc@53hor.net>2020-10-22 17:44:38 -0400
commit99dabd3f2f81ffcf0b6f2b59e13ebb4502b2ccac (patch)
tree15e19324332173405fc7544af2c17195d7b05ff1 /dichroism/src/models/photo.rs
parent76a782599b4ecc4ecb9b0ce7acc6420ed9e1ec8e (diff)
downloadtheglassyladies-99dabd3f2f81ffcf0b6f2b59e13ebb4502b2ccac.tar.xz
theglassyladies-99dabd3f2f81ffcf0b6f2b59e13ebb4502b2ccac.zip
Added product migration, better organization of DTOs, Entities, and
Domain Models. Also made config loading/photo generation easier.
Diffstat (limited to 'dichroism/src/models/photo.rs')
-rw-r--r--dichroism/src/models/photo.rs24
1 files changed, 23 insertions, 1 deletions
diff --git a/dichroism/src/models/photo.rs b/dichroism/src/models/photo.rs
index 2e8b3bf..dee6288 100644
--- a/dichroism/src/models/photo.rs
+++ b/dichroism/src/models/photo.rs
@@ -1,5 +1,27 @@
+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 id: i32,
pub path: String,
}
+
+impl Photo {
+ pub fn from_image(image: &DynamicImage) -> Result<Self> {
+ 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(),
+ })
+ }
+}