diff --git a/src/item.rs b/src/item.rs index f08f72e..9fdba58 100644 --- a/src/item.rs +++ b/src/item.rs @@ -28,20 +28,37 @@ pub struct Item { pub name: String, /// Category of the Item pub category: Option, + /// Image + pub image_path: Option, /// The variants of an Item. /// Each key of the `HashMap<_>` is the ID of a variant and contains a `Variant` pub variants: HashMap, } +pub fn get_image(path: &std::path::Path) -> Option { + let parent = path.parent()?; + let file_name = path.file_stem()?.to_str()?; + + for ext in ["jpg", "jpeg", "webp", "png"] { + let mut img_file = parent.to_path_buf(); + img_file.push(&format!("{file_name}.{ext}")); + + if img_file.exists() { + return Some(img_file.display().to_string()); + } + } + + None +} + impl Item { /// Creates a new `Item` from a parsed markdown document pub fn new(doc: &mdq::Document) -> Self { - let id = std::path::Path::new(&doc.path) - .file_stem() - .unwrap() - .to_str() - .unwrap() - .to_string(); + let path = std::path::Path::new(&doc.path); + + let id = path.file_stem().unwrap().to_str().unwrap().to_string(); + + let image_path = get_image(path); let category = doc .frontmatter @@ -86,6 +103,7 @@ impl Item { id, name, category, + image_path, variants, } } @@ -131,6 +149,11 @@ impl Item { json!({ "uuid": self.id, + "image": if self.image_path.is_some() { + Some(format!("/item/{}/image", self.id)) + } else { + None + }, "name": self.name, "category": self.category, "variants": variants diff --git a/src/main.rs b/src/main.rs index bcd321f..1a63e15 100644 --- a/src/main.rs +++ b/src/main.rs @@ -74,7 +74,6 @@ macro_rules! get_locations { }; } - pub static FLOW_INFO: OnceCell> = OnceCell::const_new(); #[macro_export] diff --git a/src/routes/item/mod.rs b/src/routes/item/mod.rs index 0985e06..cc149a0 100644 --- a/src/routes/item/mod.rs +++ b/src/routes/item/mod.rs @@ -9,6 +9,7 @@ use std::str::FromStr; pub use demand::*; pub use error::*; pub use location::*; +use rocket::fs::NamedFile; use rocket::post; use rocket::serde::json::Json; use serde::Deserialize; @@ -62,6 +63,17 @@ pub fn item_route( Ok(item.api_json()) } +#[get("/item//image")] +pub async fn item_image_route(item_id: &str, itemdb: &State) -> Option { + let item = itemdb.get_item(item_id)?; + + if let Some(img_path) = &item.image_path { + return Some(NamedFile::open(img_path).await.ok()?); + } + + None +} + /// Returns all variants of an Item #[get("/item//variants")] pub fn item_variants_page( diff --git a/src/variant.rs b/src/variant.rs index 82a3400..be78064 100644 --- a/src/variant.rs +++ b/src/variant.rs @@ -44,7 +44,7 @@ pub struct Variant { /// Days until expiry pub expiry: Option, /// Associated barcodes - pub barcodes: Option> + pub barcodes: Option>, } impl Variant { @@ -72,8 +72,12 @@ impl Variant { .get("expiry") .map(|x| x.as_i64().unwrap()), barcodes: json.as_mapping().unwrap().get("barcodes").map(|x| { - x.as_sequence().unwrap().into_iter().map(|x| x.as_i64().unwrap()).collect() - }) + x.as_sequence() + .unwrap() + .into_iter() + .map(|x| x.as_i64().unwrap()) + .collect() + }), } }