summaryrefslogtreecommitdiff
path: root/dichroism/src/models
diff options
context:
space:
mode:
authorAdam T. Carpenter <atc@53hor.net>2020-10-22 22:09:46 -0400
committerAdam T. Carpenter <atc@53hor.net>2020-10-22 22:09:46 -0400
commit5cb0869b94fc8b9aff564055af9a60046de7b028 (patch)
tree022cd909467d10747e9aa36d9455d47ea90b6c97 /dichroism/src/models
parent99dabd3f2f81ffcf0b6f2b59e13ebb4502b2ccac (diff)
downloadtheglassyladies-5cb0869b94fc8b9aff564055af9a60046de7b028.tar.xz
theglassyladies-5cb0869b94fc8b9aff564055af9a60046de7b028.zip
Skipped separate photo table; working handler for get all products
(lacking cats)
Diffstat (limited to 'dichroism/src/models')
-rw-r--r--dichroism/src/models/mod.rs6
-rw-r--r--dichroism/src/models/new_photo.rs6
-rw-r--r--dichroism/src/models/new_photo_data.rs95
-rw-r--r--dichroism/src/models/new_photo_set.rs8
-rw-r--r--dichroism/src/models/photo.rs4
-rw-r--r--dichroism/src/models/photo_set.rs25
6 files changed, 13 insertions, 131 deletions
diff --git a/dichroism/src/models/mod.rs b/dichroism/src/models/mod.rs
index e0fc3bd..529a9bc 100644
--- a/dichroism/src/models/mod.rs
+++ b/dichroism/src/models/mod.rs
@@ -1,13 +1,7 @@
-mod new_photo;
-mod new_photo_data;
-mod new_photo_set;
mod photo;
mod photo_set;
mod product;
-pub use new_photo::*;
-pub use new_photo_data::*;
-pub use new_photo_set::*;
pub use photo::*;
pub use photo_set::*;
pub use product::*;
diff --git a/dichroism/src/models/new_photo.rs b/dichroism/src/models/new_photo.rs
deleted file mode 100644
index d348e95..0000000
--- a/dichroism/src/models/new_photo.rs
+++ /dev/null
@@ -1,6 +0,0 @@
-use crate::schema::photos;
-#[table_name = "photos"]
-#[derive(Debug, Insertable)]
-pub struct NewPhoto {
- pub path: String,
-}
diff --git a/dichroism/src/models/new_photo_data.rs b/dichroism/src/models/new_photo_data.rs
deleted file mode 100644
index 19a6557..0000000
--- a/dichroism/src/models/new_photo_data.rs
+++ /dev/null
@@ -1,95 +0,0 @@
-use super::NewPhoto;
-use super::NewPhotoSet;
-use crate::error::DichroismError;
-use crate::result::Result;
-use base64::decode;
-use image::imageops::FilterType;
-use image::DynamicImage;
-use image::GenericImageView;
-use regex::Regex;
-use std::path::PathBuf;
-use uuid::Uuid;
-
-use once_cell::sync::Lazy;
-static DATA_URI_RE: Lazy<Regex> = Lazy::new(|| {
- Regex::new("^data:image/(png|jpeg);base64,(?P<data>.+)")
- .expect("Couldn't parse data URI Regex.")
-});
-
-pub struct NewPhotoSetData {
- original: DynamicImage, // original, just for safe-keeping
- fullsize: DynamicImage, // full-size, "zoomed" view
- base: DynamicImage, // basic viewing
- thumbnail: DynamicImage, // tiny, square thumbnail
-}
-
-impl NewPhotoSetData {
- pub fn from_data_uri(uri: &str) -> Result<Self> {
- let data = DATA_URI_RE
- .captures(uri)
- .ok_or(DichroismError::UriDataExtract)?
- .name("data")
- .ok_or(DichroismError::UriDataExtract)?
- .as_str();
- let original = image::load_from_memory(&decode(data)?)?;
- let fullsize = original.resize(1000, 1000, FilterType::Lanczos3);
- let base = original.resize(640, 640, FilterType::Lanczos3);
-
- let (width, height) = original.dimensions();
- let thumbnail = if width > height {
- let offset = (width - height) / 2;
- original.crop_imm(offset, 0, width - offset * 2, height)
- } else {
- let offset = (height - width) / 2;
- original.crop_imm(0, offset, width, height - offset * 2)
- }
- .resize(300, 300, FilterType::Lanczos3);
-
- Ok(NewPhotoSetData {
- original,
- fullsize,
- base,
- thumbnail,
- })
- }
-
- pub fn commit(self, prefix: &str) -> Result<NewPhotoSet> {
- Ok(NewPhotoSet {
- original: self.commit_single(prefix, &self.original)?,
- fullsize: self.commit_single(prefix, &self.fullsize)?,
- base: self.commit_single(prefix, &self.base)?,
- thumbnail: self.commit_single(prefix, &self.thumbnail)?,
- })
- }
-
- fn commit_single(&self, prefix: &str, image: &DynamicImage) -> Result<NewPhoto> {
- let base_name = Uuid::new_v3(&Uuid::NAMESPACE_OID, &image.to_bytes())
- .to_hyphenated()
- .to_string();
- let mut path = PathBuf::new();
- path.push(prefix);
- path.push(base_name);
- path.set_extension("jpg");
- image.save(&path)?;
-
- Ok(NewPhoto {
- path: path.to_str().ok_or(DichroismError::ImageWrite)?.to_string(),
- })
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- const TEST_DATA_URI: &str = include_str!("../unit_test_data/img_data_uri.txt");
- const TEST_DATA_BASE64: &str = include_str!("../unit_test_data/test_data_base64.txt");
-
- #[test]
- fn test_gen_product_images() {
- NewPhotoSetData::from_data_uri(TEST_DATA_URI.trim())
- .unwrap()
- .commit(".")
- .unwrap();
- }
-}
diff --git a/dichroism/src/models/new_photo_set.rs b/dichroism/src/models/new_photo_set.rs
deleted file mode 100644
index e907592..0000000
--- a/dichroism/src/models/new_photo_set.rs
+++ /dev/null
@@ -1,8 +0,0 @@
-use super::NewPhoto;
-
-pub struct NewPhotoSet {
- pub original: NewPhoto,
- pub fullsize: NewPhoto,
- pub base: NewPhoto,
- pub thumbnail: NewPhoto,
-}
diff --git a/dichroism/src/models/photo.rs b/dichroism/src/models/photo.rs
index dee6288..6069085 100644
--- a/dichroism/src/models/photo.rs
+++ b/dichroism/src/models/photo.rs
@@ -11,6 +11,10 @@ pub struct Photo {
}
impl Photo {
+ pub fn from_path(path: String) -> Self {
+ Self { path }
+ }
+
pub fn from_image(image: &DynamicImage) -> Result<Self> {
let base_name = Uuid::new_v3(&Uuid::NAMESPACE_OID, &image.to_bytes())
.to_hyphenated()
diff --git a/dichroism/src/models/photo_set.rs b/dichroism/src/models/photo_set.rs
index 84fe0e1..7187c65 100644
--- a/dichroism/src/models/photo_set.rs
+++ b/dichroism/src/models/photo_set.rs
@@ -21,6 +21,15 @@ pub struct PhotoSet {
}
impl PhotoSet {
+ pub fn from_photos(original: Photo, fullsize: Photo, base: Photo, thumbnail: Photo) -> Self {
+ Self {
+ original,
+ fullsize,
+ base,
+ thumbnail,
+ }
+ }
+
pub fn from_data_uri(uri: &str) -> Result<Self> {
let data = DATA_URI_RE
.captures(uri)
@@ -50,19 +59,3 @@ impl PhotoSet {
})
}
}
-
-#[cfg(test)]
-mod tests {
- //use super::*;
-
- const TEST_DATA_URI: &str = include_str!("../unit_test_data/img_data_uri.txt");
- const TEST_DATA_BASE64: &str = include_str!("../unit_test_data/test_data_base64.txt");
-
- #[test]
- fn test_gen_product_images() {
- // PhotoSet::from_data_uri(TEST_DATA_URI.trim())
- // .unwrap()
- // .commit(".")
- // .unwrap();
- }
-}