image
This commit is contained in:
parent
6c6ee93fbf
commit
4c0769199c
4 changed files with 48 additions and 10 deletions
35
src/item.rs
35
src/item.rs
|
@ -28,20 +28,37 @@ pub struct Item {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
/// Category of the Item
|
/// Category of the Item
|
||||||
pub category: Option<String>,
|
pub category: Option<String>,
|
||||||
|
/// Image
|
||||||
|
pub image_path: Option<String>,
|
||||||
/// The variants of an Item.
|
/// The variants of an Item.
|
||||||
/// Each key of the `HashMap<_>` is the ID of a variant and contains a `Variant`
|
/// Each key of the `HashMap<_>` is the ID of a variant and contains a `Variant`
|
||||||
pub variants: HashMap<String, Variant>,
|
pub variants: HashMap<String, Variant>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_image(path: &std::path::Path) -> Option<String> {
|
||||||
|
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 {
|
impl Item {
|
||||||
/// Creates a new `Item` from a parsed markdown document
|
/// Creates a new `Item` from a parsed markdown document
|
||||||
pub fn new(doc: &mdq::Document) -> Self {
|
pub fn new(doc: &mdq::Document) -> Self {
|
||||||
let id = std::path::Path::new(&doc.path)
|
let path = std::path::Path::new(&doc.path);
|
||||||
.file_stem()
|
|
||||||
.unwrap()
|
let id = path.file_stem().unwrap().to_str().unwrap().to_string();
|
||||||
.to_str()
|
|
||||||
.unwrap()
|
let image_path = get_image(path);
|
||||||
.to_string();
|
|
||||||
|
|
||||||
let category = doc
|
let category = doc
|
||||||
.frontmatter
|
.frontmatter
|
||||||
|
@ -86,6 +103,7 @@ impl Item {
|
||||||
id,
|
id,
|
||||||
name,
|
name,
|
||||||
category,
|
category,
|
||||||
|
image_path,
|
||||||
variants,
|
variants,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -131,6 +149,11 @@ impl Item {
|
||||||
|
|
||||||
json!({
|
json!({
|
||||||
"uuid": self.id,
|
"uuid": self.id,
|
||||||
|
"image": if self.image_path.is_some() {
|
||||||
|
Some(format!("/item/{}/image", self.id))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
"name": self.name,
|
"name": self.name,
|
||||||
"category": self.category,
|
"category": self.category,
|
||||||
"variants": variants
|
"variants": variants
|
||||||
|
|
|
@ -74,7 +74,6 @@ macro_rules! get_locations {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub static FLOW_INFO: OnceCell<JSONStore<FlowInfo>> = OnceCell::const_new();
|
pub static FLOW_INFO: OnceCell<JSONStore<FlowInfo>> = OnceCell::const_new();
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
|
|
|
@ -9,6 +9,7 @@ use std::str::FromStr;
|
||||||
pub use demand::*;
|
pub use demand::*;
|
||||||
pub use error::*;
|
pub use error::*;
|
||||||
pub use location::*;
|
pub use location::*;
|
||||||
|
use rocket::fs::NamedFile;
|
||||||
use rocket::post;
|
use rocket::post;
|
||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
@ -62,6 +63,17 @@ pub fn item_route(
|
||||||
Ok(item.api_json())
|
Ok(item.api_json())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[get("/item/<item_id>/image")]
|
||||||
|
pub async fn item_image_route(item_id: &str, itemdb: &State<ItemDB>) -> Option<NamedFile> {
|
||||||
|
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
|
/// Returns all variants of an Item
|
||||||
#[get("/item/<item_id>/variants")]
|
#[get("/item/<item_id>/variants")]
|
||||||
pub fn item_variants_page(
|
pub fn item_variants_page(
|
||||||
|
|
|
@ -44,7 +44,7 @@ pub struct Variant {
|
||||||
/// Days until expiry
|
/// Days until expiry
|
||||||
pub expiry: Option<i64>,
|
pub expiry: Option<i64>,
|
||||||
/// Associated barcodes
|
/// Associated barcodes
|
||||||
pub barcodes: Option<Vec<i64>>
|
pub barcodes: Option<Vec<i64>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Variant {
|
impl Variant {
|
||||||
|
@ -72,8 +72,12 @@ impl Variant {
|
||||||
.get("expiry")
|
.get("expiry")
|
||||||
.map(|x| x.as_i64().unwrap()),
|
.map(|x| x.as_i64().unwrap()),
|
||||||
barcodes: json.as_mapping().unwrap().get("barcodes").map(|x| {
|
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()
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue