flashkraft_core/domain/
image_info.rs1use std::path::PathBuf;
7
8#[derive(Debug, Clone)]
10pub struct ImageInfo {
11 pub path: PathBuf,
13 pub name: String,
15 pub size_mb: f64,
17}
18
19impl ImageInfo {
20 pub fn size_bytes(&self) -> u64 {
22 (self.size_mb * 1_048_576.0) as u64
23 }
24
25 pub fn from_path(path: PathBuf) -> Self {
35 let name = path
36 .file_name()
37 .and_then(|n| n.to_str())
38 .unwrap_or("Unknown")
39 .to_string();
40
41 let size_mb = path
42 .metadata()
43 .map(|m| m.len() as f64 / (1024.0 * 1024.0))
44 .unwrap_or(0.0);
45
46 Self {
47 path,
48 name,
49 size_mb,
50 }
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57 use std::fs::File;
58 use std::io::Write;
59
60 #[test]
61 fn test_image_info_from_path() {
62 let temp_dir = std::env::temp_dir();
64 let test_path = temp_dir.join("test_image.img");
65
66 if let Ok(mut file) = File::create(&test_path) {
68 let _ = file.write_all(&[0u8; 1024 * 1024]); }
70
71 let image_info = ImageInfo::from_path(test_path.clone());
72
73 assert_eq!(image_info.name, "test_image.img");
74 assert_eq!(image_info.path, test_path);
75 assert!((image_info.size_mb - 1.0).abs() < 0.1);
77
78 let _ = std::fs::remove_file(test_path);
80 }
81
82 #[test]
83 fn test_image_info_nonexistent_file() {
84 let path = PathBuf::from("/nonexistent/test.img");
85 let image_info = ImageInfo::from_path(path);
86
87 assert_eq!(image_info.name, "test.img");
88 assert_eq!(image_info.size_mb, 0.0);
89 }
90}