feat: image metadata reading - width, height, format, file_size with fixture test
This commit is contained in:
parent
af58a35b0a
commit
8166878bf8
@ -10,3 +10,4 @@ infer = "0.16"
|
|||||||
mime_guess = "2"
|
mime_guess = "2"
|
||||||
comrak = "0.29"
|
comrak = "0.29"
|
||||||
uuid = { version = "1", features = ["v4"] }
|
uuid = { version = "1", features = ["v4"] }
|
||||||
|
image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp", "gif"] }
|
||||||
|
|||||||
@ -0,0 +1,77 @@
|
|||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use image::GenericImageView;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::error::DocumentError;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct ImageMeta {
|
||||||
|
pub width: u32,
|
||||||
|
pub height: u32,
|
||||||
|
pub format: String,
|
||||||
|
pub file_size: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read image metadata from a file path.
|
||||||
|
pub fn read_image_meta(file_path: &str) -> Result<ImageMeta, DocumentError> {
|
||||||
|
let path = Path::new(file_path);
|
||||||
|
|
||||||
|
let file_size = std::fs::metadata(file_path)
|
||||||
|
.map(|m| m.len())?;
|
||||||
|
|
||||||
|
let format = path
|
||||||
|
.extension()
|
||||||
|
.and_then(|e| e.to_str())
|
||||||
|
.unwrap_or("unknown")
|
||||||
|
.to_lowercase();
|
||||||
|
|
||||||
|
let img = image::open(file_path).map_err(|e| {
|
||||||
|
DocumentError::ParseError(format!("image decode failed: {e}"))
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let (width, height) = img.dimensions();
|
||||||
|
|
||||||
|
Ok(ImageMeta {
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
format,
|
||||||
|
file_size,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_read_image_meta_not_found() {
|
||||||
|
let result = read_image_meta("/nonexistent/img.png");
|
||||||
|
assert!(result.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_read_fixture_png() {
|
||||||
|
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/../../fixtures/images/test-red.png");
|
||||||
|
let meta = read_image_meta(path).unwrap();
|
||||||
|
assert_eq!(meta.width, 1);
|
||||||
|
assert_eq!(meta.height, 1);
|
||||||
|
assert_eq!(meta.format, "png");
|
||||||
|
assert!(meta.file_size > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_image_meta_serde() {
|
||||||
|
let meta = ImageMeta {
|
||||||
|
width: 800,
|
||||||
|
height: 600,
|
||||||
|
format: "png".to_string(),
|
||||||
|
file_size: 12345,
|
||||||
|
};
|
||||||
|
let json = serde_json::to_string(&meta).unwrap();
|
||||||
|
let parsed: ImageMeta = serde_json::from_str(&json).unwrap();
|
||||||
|
assert_eq!(parsed.width, 800);
|
||||||
|
assert_eq!(parsed.height, 600);
|
||||||
|
assert_eq!(parsed.format, "png");
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
fixtures/images/test-red.png
Normal file
BIN
fixtures/images/test-red.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 69 B |
Loading…
x
Reference in New Issue
Block a user