diff options
author | Adam T. Carpenter <atc@53hor.net> | 2020-10-31 10:44:31 -0400 |
---|---|---|
committer | Adam T. Carpenter <atc@53hor.net> | 2020-10-31 10:44:31 -0400 |
commit | 050e40c03900827057ab5db2f6bbe971a6408fda (patch) | |
tree | caa3a97208d077e2206f735f9ecd6618b4ac9ebf /dichroism/src/image_service.rs | |
parent | 3e1eadbbfdca1b2c0cb32ba4c8e1160a60e0ccb8 (diff) | |
download | theglassyladies-050e40c03900827057ab5db2f6bbe971a6408fda.tar.xz theglassyladies-050e40c03900827057ab5db2f6bbe971a6408fda.zip |
blocking on photo set generation since it hits the fs
Diffstat (limited to 'dichroism/src/image_service.rs')
-rw-r--r-- | dichroism/src/image_service.rs | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/dichroism/src/image_service.rs b/dichroism/src/image_service.rs index b33d5c8..3a31e04 100644 --- a/dichroism/src/image_service.rs +++ b/dichroism/src/image_service.rs @@ -2,7 +2,6 @@ use crate::config::CONFIG_INSTANCE; use crate::constants::{PHOTO_BASE_XY, PHOTO_FULLSIZE_XY, PHOTO_THUMBNAIL_XY}; use crate::error::DichroismError; use crate::models::{Photo, PhotoSet}; -use crate::result::Result; use base64::decode; use image::imageops::FilterType; use image::DynamicImage; @@ -17,13 +16,12 @@ static DATA_URI_RE: Lazy<Regex> = Lazy::new(|| { .expect("Couldn't parse data URI Regex.") }); -// TODO: should be async so server threads don't block on FS access -pub fn generate_photo_set(uri: &str) -> Result<PhotoSet> { +pub fn generate_photo_set(uri: &str) -> Result<PhotoSet, DichroismError> { let data = DATA_URI_RE .captures(uri) - .ok_or(DichroismError::UriDataExtract)? + .ok_or_else(|| DichroismError::UriDataExtract("Failed to parse URI".to_string()))? .name("data") - .ok_or(DichroismError::UriDataExtract)? + .ok_or_else(|| DichroismError::UriDataExtract("Failed to extract data".to_string()))? .as_str(); let original = image::load_from_memory(&decode(data)?)?; let fullsize = original.resize(PHOTO_FULLSIZE_XY, PHOTO_FULLSIZE_XY, FilterType::Lanczos3); @@ -48,7 +46,7 @@ pub fn generate_photo_set(uri: &str) -> Result<PhotoSet> { }) } -pub fn generate_photo(image: &DynamicImage) -> Result<Photo> { +fn generate_photo(image: &DynamicImage) -> Result<Photo, DichroismError> { let base_name = Uuid::new_v3(&Uuid::NAMESPACE_OID, &image.to_bytes()) .to_hyphenated() .to_string(); @@ -59,8 +57,12 @@ pub fn generate_photo(image: &DynamicImage) -> Result<Photo> { let id = path .file_name() - .ok_or(DichroismError::ImageWrite)? + .ok_or_else(|| { + DichroismError::ImageWrite("Error extracting filename from path".to_string()) + })? .to_str() - .ok_or(DichroismError::ImageWrite)?; + .ok_or_else(|| { + DichroismError::ImageWrite("Error converting filename to slice".to_string()) + })?; Ok(Photo::new(String::from(id))) } |