|
1 | 1 | use errors::{anyhow, Context, Result};
|
| 2 | +use libs::avif_parse::read_avif; |
2 | 3 | use libs::image::ImageReader;
|
3 | 4 | use libs::image::{ImageFormat, ImageResult};
|
4 | 5 | use libs::svg_metadata::Metadata as SvgMetadata;
|
5 | 6 | use serde::Serialize;
|
6 | 7 | use std::ffi::OsStr;
|
| 8 | +use std::fs::File; |
| 9 | +use std::io::BufReader; |
7 | 10 | use std::path::Path;
|
8 | 11 |
|
9 | 12 | /// Size and format read cheaply with `image`'s `Reader`.
|
@@ -44,6 +47,10 @@ impl ImageMetaResponse {
|
44 | 47 | pub fn new_svg(width: u32, height: u32) -> Self {
|
45 | 48 | Self { width, height, format: Some("svg"), mime: Some("text/svg+xml") }
|
46 | 49 | }
|
| 50 | + |
| 51 | + pub fn new_avif(width: u32, height: u32) -> Self { |
| 52 | + Self { width, height, format: Some("avif"), mime: Some("image/avif") } |
| 53 | + } |
47 | 54 | }
|
48 | 55 |
|
49 | 56 | impl From<ImageMeta> for ImageMetaResponse {
|
@@ -75,6 +82,15 @@ pub fn read_image_metadata<P: AsRef<Path>>(path: P) -> Result<ImageMetaResponse>
|
75 | 82 | // this is not a typo, this returns the correct values for width and height.
|
76 | 83 | .map(|(h, w)| ImageMetaResponse::new_svg(w as u32, h as u32))
|
77 | 84 | }
|
| 85 | + "avif" => { |
| 86 | + let avif_data = |
| 87 | + read_avif(&mut BufReader::new(File::open(path)?)).with_context(err_context)?; |
| 88 | + let meta = avif_data.primary_item_metadata()?; |
| 89 | + return Ok(ImageMetaResponse::new_avif( |
| 90 | + meta.max_frame_width.get(), |
| 91 | + meta.max_frame_height.get(), |
| 92 | + )); |
| 93 | + } |
78 | 94 | _ => ImageMeta::read(path).map(ImageMetaResponse::from).with_context(err_context),
|
79 | 95 | }
|
80 | 96 | }
|
0 commit comments