From 3e1eadbbfdca1b2c0cb32ba4c8e1160a60e0ccb8 Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Sat, 31 Oct 2020 10:14:31 -0400 Subject: All basic functionality implemented. --- dichroism/src/models/dbid.rs | 4 +++ dichroism/src/models/photo.rs | 32 +++++----------------- dichroism/src/models/photo_set.rs | 57 +++------------------------------------ dichroism/src/models/product.rs | 7 ++--- 4 files changed, 17 insertions(+), 83 deletions(-) create mode 100644 dichroism/src/models/dbid.rs (limited to 'dichroism/src/models') diff --git a/dichroism/src/models/dbid.rs b/dichroism/src/models/dbid.rs new file mode 100644 index 0000000..80eee58 --- /dev/null +++ b/dichroism/src/models/dbid.rs @@ -0,0 +1,4 @@ +pub enum Dbid { + Stored(i32), + NotStored, +} diff --git a/dichroism/src/models/photo.rs b/dichroism/src/models/photo.rs index 8c1435e..e24a691 100644 --- a/dichroism/src/models/photo.rs +++ b/dichroism/src/models/photo.rs @@ -1,34 +1,14 @@ -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)] +#[derive(Debug, Clone)] pub struct Photo { - pub filename: String, + pub id: String, } impl Photo { - pub fn from_filename(filename: String) -> Self { - Self { filename } + pub fn new(id: String) -> Self { + Self { id } } - 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)?; - - let filename = path - .file_name() - .ok_or(DichroismError::ImageWrite)? - .to_str() - .ok_or(DichroismError::ImageWrite)?; - Ok(Self::from_filename(String::from(filename))) + pub fn filename(&self) -> String { + format!("{}.jpg", self.id) } } diff --git a/dichroism/src/models/photo_set.rs b/dichroism/src/models/photo_set.rs index 7187c65..f2c7677 100644 --- a/dichroism/src/models/photo_set.rs +++ b/dichroism/src/models/photo_set.rs @@ -1,61 +1,10 @@ -use super::photo::Photo; -use crate::constants::{PHOTO_BASE_XY, PHOTO_FULLSIZE_XY, PHOTO_THUMBNAIL_XY}; -use crate::error::DichroismError; -use crate::result::Result; -use base64::decode; -use image::imageops::FilterType; -use image::GenericImageView; -use once_cell::sync::Lazy; -use regex::Regex; - -static DATA_URI_RE: Lazy = Lazy::new(|| { - Regex::new("^data:image/(png|jpeg);base64,(?P.+)") - .expect("Couldn't parse data URI Regex.") -}); +use super::Photo; +#[derive(Debug, Clone)] pub struct PhotoSet { + pub id: Option, pub original: Photo, // original, just for safe-keeping pub fullsize: Photo, // full-size, "zoomed" view pub base: Photo, // basic viewing pub thumbnail: Photo, // tiny, square thumbnail } - -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 { - 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(PHOTO_FULLSIZE_XY, PHOTO_FULLSIZE_XY, FilterType::Lanczos3); - let base = original.resize(PHOTO_BASE_XY, PHOTO_BASE_XY, 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(PHOTO_THUMBNAIL_XY, PHOTO_THUMBNAIL_XY, FilterType::Lanczos3); - - Ok(Self { - original: Photo::from_image(&original)?, - fullsize: Photo::from_image(&fullsize)?, - base: Photo::from_image(&base)?, - thumbnail: Photo::from_image(&thumbnail)?, - }) - } -} diff --git a/dichroism/src/models/product.rs b/dichroism/src/models/product.rs index be0a5ec..4a3d782 100644 --- a/dichroism/src/models/product.rs +++ b/dichroism/src/models/product.rs @@ -1,11 +1,12 @@ use super::PhotoSet; +#[derive(Debug, Clone)] pub struct Product { - pub id: u32, + pub id: Option, pub name: String, pub description: String, - pub cents: u32, - pub quantity: u32, + pub cents: i32, + pub quantity: i32, pub featured: bool, pub photo_set: PhotoSet, pub category: String, -- cgit v1.2.3