Add MIME type to get_image_metadata (#2409)

This commit is contained in:
Clar Fon 2024-02-29 07:46:29 -05:00 committed by Vincent Prouillet
parent 13682e1342
commit aa81986580
4 changed files with 31 additions and 6 deletions

View file

@ -37,11 +37,12 @@ pub struct ImageMetaResponse {
pub width: u32,
pub height: u32,
pub format: Option<&'static str>,
pub mime: Option<&'static str>,
}
impl ImageMetaResponse {
pub fn new_svg(width: u32, height: u32) -> Self {
Self { width, height, format: Some("svg") }
Self { width, height, format: Some("svg"), mime: Some("text/svg+xml") }
}
}
@ -51,6 +52,7 @@ impl From<ImageMeta> for ImageMetaResponse {
width: im.size.0,
height: im.size.1,
format: im.format.and_then(|f| f.extensions_str().first()).copied(),
mime: im.format.map(|f| f.to_mime_type()),
}
}
}

View file

@ -136,7 +136,12 @@ fn resize_image_webp_jpg() {
fn read_image_metadata_jpg() {
assert_eq!(
image_meta_test("jpg.jpg"),
ImageMetaResponse { width: 300, height: 380, format: Some("jpg") }
ImageMetaResponse {
width: 300,
height: 380,
format: Some("jpg"),
mime: Some("image/jpeg")
}
);
}
@ -144,7 +149,7 @@ fn read_image_metadata_jpg() {
fn read_image_metadata_png() {
assert_eq!(
image_meta_test("png.png"),
ImageMetaResponse { width: 300, height: 380, format: Some("png") }
ImageMetaResponse { width: 300, height: 380, format: Some("png"), mime: Some("image/png") }
);
}
@ -152,7 +157,12 @@ fn read_image_metadata_png() {
fn read_image_metadata_svg() {
assert_eq!(
image_meta_test("svg.svg"),
ImageMetaResponse { width: 300, height: 300, format: Some("svg") }
ImageMetaResponse {
width: 300,
height: 300,
format: Some("svg"),
mime: Some("text/svg+xml")
}
);
}
@ -160,7 +170,12 @@ fn read_image_metadata_svg() {
fn read_image_metadata_webp() {
assert_eq!(
image_meta_test("webp.webp"),
ImageMetaResponse { width: 300, height: 380, format: Some("webp") }
ImageMetaResponse {
width: 300,
height: 380,
format: Some("webp"),
mime: Some("image/webp")
}
);
}

View file

@ -269,6 +269,8 @@ mod tests {
let data = static_fn.call(&args).unwrap().as_object().unwrap().clone();
assert_eq!(data["height"], to_value(380).unwrap());
assert_eq!(data["width"], to_value(300).unwrap());
assert_eq!(data["format"], to_value("jpg").unwrap());
assert_eq!(data["mime"], to_value("image/jpeg").unwrap());
// 2. a call to something in `static` with an absolute path is handled currently the same as the above
let mut args = HashMap::new();
@ -276,6 +278,8 @@ mod tests {
let data = static_fn.call(&args).unwrap().as_object().unwrap().clone();
assert_eq!(data["height"], to_value(380).unwrap());
assert_eq!(data["width"], to_value(300).unwrap());
assert_eq!(data["format"], to_value("jpg").unwrap());
assert_eq!(data["mime"], to_value("image/jpeg").unwrap());
// 3. a call to something in `content` with a relative path
let mut args = HashMap::new();
@ -283,6 +287,8 @@ mod tests {
let data = static_fn.call(&args).unwrap().as_object().unwrap().clone();
assert_eq!(data["height"], to_value(380).unwrap());
assert_eq!(data["width"], to_value(300).unwrap());
assert_eq!(data["format"], to_value("jpg").unwrap());
assert_eq!(data["mime"], to_value("image/jpeg").unwrap());
// 4. a call to something in `content` with a @/ path corresponds to
let mut args = HashMap::new();
@ -290,5 +296,7 @@ mod tests {
let data = static_fn.call(&args).unwrap().as_object().unwrap().clone();
assert_eq!(data["height"], to_value(380).unwrap());
assert_eq!(data["width"], to_value(300).unwrap());
assert_eq!(data["format"], to_value("jpg").unwrap());
assert_eq!(data["mime"], to_value("image/jpeg").unwrap());
}
}

View file

@ -305,7 +305,7 @@ It can take the following arguments:
- `path`: mandatory, see [File Searching Logic](@/documentation/templates/overview.md#file-searching-logic) for details
- `allow_missing`: optional, `true` or `false`, defaults to `false`. Whether a missing file should raise an error or not.
The method returns a map containing `width`, `height` and `format` (the lowercased value as string).
The method returns a map containing `width`, `height`, `format`, and `mime`. The `format` returned is the most common file extension for the file format, which may not match the one used for the image.
```jinja2
{% set meta = get_image_metadata(path="...") %}