summaryrefslogtreecommitdiff
path: root/dichroism/src/image_service.rs
diff options
context:
space:
mode:
Diffstat (limited to 'dichroism/src/image_service.rs')
-rw-r--r--dichroism/src/image_service.rs18
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)))
}