Skip to content

Commit 61878ed

Browse files
clarfontheyberdandy
authored andcommitted
Add MIME type to get_image_metadata (getzola#2409)
1 parent 3cf6961 commit 61878ed

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

components/imageproc/src/meta.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ pub struct ImageMetaResponse {
3737
pub width: u32,
3838
pub height: u32,
3939
pub format: Option<&'static str>,
40+
pub mime: Option<&'static str>,
4041
}
4142

4243
impl ImageMetaResponse {
4344
pub fn new_svg(width: u32, height: u32) -> Self {
44-
Self { width, height, format: Some("svg") }
45+
Self { width, height, format: Some("svg"), mime: Some("text/svg+xml") }
4546
}
4647
}
4748

@@ -51,6 +52,7 @@ impl From<ImageMeta> for ImageMetaResponse {
5152
width: im.size.0,
5253
height: im.size.1,
5354
format: im.format.and_then(|f| f.extensions_str().first()).copied(),
55+
mime: im.format.map(|f| f.to_mime_type()),
5456
}
5557
}
5658
}

components/imageproc/tests/resize_image.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -136,31 +136,46 @@ fn resize_image_webp_jpg() {
136136
fn read_image_metadata_jpg() {
137137
assert_eq!(
138138
image_meta_test("jpg.jpg"),
139-
ImageMetaResponse { width: 300, height: 380, format: Some("jpg") }
139+
ImageMetaResponse {
140+
width: 300,
141+
height: 380,
142+
format: Some("jpg"),
143+
mime: Some("image/jpeg")
144+
}
140145
);
141146
}
142147

143148
#[test]
144149
fn read_image_metadata_png() {
145150
assert_eq!(
146151
image_meta_test("png.png"),
147-
ImageMetaResponse { width: 300, height: 380, format: Some("png") }
152+
ImageMetaResponse { width: 300, height: 380, format: Some("png"), mime: Some("image/png") }
148153
);
149154
}
150155

151156
#[test]
152157
fn read_image_metadata_svg() {
153158
assert_eq!(
154159
image_meta_test("svg.svg"),
155-
ImageMetaResponse { width: 300, height: 300, format: Some("svg") }
160+
ImageMetaResponse {
161+
width: 300,
162+
height: 300,
163+
format: Some("svg"),
164+
mime: Some("text/svg+xml")
165+
}
156166
);
157167
}
158168

159169
#[test]
160170
fn read_image_metadata_webp() {
161171
assert_eq!(
162172
image_meta_test("webp.webp"),
163-
ImageMetaResponse { width: 300, height: 380, format: Some("webp") }
173+
ImageMetaResponse {
174+
width: 300,
175+
height: 380,
176+
format: Some("webp"),
177+
mime: Some("image/webp")
178+
}
164179
);
165180
}
166181

components/templates/src/global_fns/images.rs

+8
Original file line numberDiff line numberDiff line change
@@ -269,26 +269,34 @@ mod tests {
269269
let data = static_fn.call(&args).unwrap().as_object().unwrap().clone();
270270
assert_eq!(data["height"], to_value(380).unwrap());
271271
assert_eq!(data["width"], to_value(300).unwrap());
272+
assert_eq!(data["format"], to_value("jpg").unwrap());
273+
assert_eq!(data["mime"], to_value("image/jpeg").unwrap());
272274

273275
// 2. a call to something in `static` with an absolute path is handled currently the same as the above
274276
let mut args = HashMap::new();
275277
args.insert("path".to_string(), to_value("/static/gutenberg.jpg").unwrap());
276278
let data = static_fn.call(&args).unwrap().as_object().unwrap().clone();
277279
assert_eq!(data["height"], to_value(380).unwrap());
278280
assert_eq!(data["width"], to_value(300).unwrap());
281+
assert_eq!(data["format"], to_value("jpg").unwrap());
282+
assert_eq!(data["mime"], to_value("image/jpeg").unwrap());
279283

280284
// 3. a call to something in `content` with a relative path
281285
let mut args = HashMap::new();
282286
args.insert("path".to_string(), to_value("content/gutenberg.jpg").unwrap());
283287
let data = static_fn.call(&args).unwrap().as_object().unwrap().clone();
284288
assert_eq!(data["height"], to_value(380).unwrap());
285289
assert_eq!(data["width"], to_value(300).unwrap());
290+
assert_eq!(data["format"], to_value("jpg").unwrap());
291+
assert_eq!(data["mime"], to_value("image/jpeg").unwrap());
286292

287293
// 4. a call to something in `content` with a @/ path corresponds to
288294
let mut args = HashMap::new();
289295
args.insert("path".to_string(), to_value("@/gutenberg.jpg").unwrap());
290296
let data = static_fn.call(&args).unwrap().as_object().unwrap().clone();
291297
assert_eq!(data["height"], to_value(380).unwrap());
292298
assert_eq!(data["width"], to_value(300).unwrap());
299+
assert_eq!(data["format"], to_value("jpg").unwrap());
300+
assert_eq!(data["mime"], to_value("image/jpeg").unwrap());
293301
}
294302
}

docs/content/documentation/templates/overview.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ It can take the following arguments:
305305
- `path`: mandatory, see [File Searching Logic](@/documentation/templates/overview.md#file-searching-logic) for details
306306
- `allow_missing`: optional, `true` or `false`, defaults to `false`. Whether a missing file should raise an error or not.
307307

308-
The method returns a map containing `width`, `height` and `format` (the lowercased value as string).
308+
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.
309309

310310
```jinja2
311311
{% set meta = get_image_metadata(path="...") %}

0 commit comments

Comments
 (0)