Skip to content

Commit

Permalink
Add MIME type to get_image_metadata (#2409)
Browse files Browse the repository at this point in the history
  • Loading branch information
clarfonthey authored and Keats committed Jun 20, 2024
1 parent 13682e1 commit aa81986
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
4 changes: 3 additions & 1 deletion components/imageproc/src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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") }
}
}

Expand All @@ -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()),
}
}
}
Expand Down
23 changes: 19 additions & 4 deletions components/imageproc/tests/resize_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,31 +136,46 @@ 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")
}
);
}

#[test]
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") }
);
}

#[test]
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")
}
);
}

#[test]
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")
}
);
}

Expand Down
8 changes: 8 additions & 0 deletions components/templates/src/global_fns/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,26 +269,34 @@ 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();
args.insert("path".to_string(), to_value("/static/gutenberg.jpg").unwrap());
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();
args.insert("path".to_string(), to_value("content/gutenberg.jpg").unwrap());
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();
args.insert("path".to_string(), to_value("@/gutenberg.jpg").unwrap());
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 changes: 1 addition & 1 deletion docs/content/documentation/templates/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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="...") %}
Expand Down

0 comments on commit aa81986

Please sign in to comment.